NHibernate bag map an additional column
I have an NHibernate mapping for a class called MediaStyle which includes this mapping:
<bag name="EngineSteps" table="core.MediaStyleMediaPrintEngineStepAssoc" order-by="Sequence" lazy="true" cascade="none">
<key column="MediaStyleId"/>
<many-to-many class="MediaPrintEngineStep" lazy="proxy" />
</bag>
The class MediaPrintEngineStep is also mapped, and the class contains a property 'Sequence'. Sequence isn't stored on the table MediaPrintEngineStep maps to,开发者_如何学编程 but is contained on the association table used to map EngineSteps to MediaStyles.
Is there a way to have nHibernate map the 'Sequence' column from core.MediaStyleMediaPrintEngineStepAssoc into the empty property on the MediaPrintEngineStep class, or will I have to write additional code to get/set the sequence after it has been mapped? Unfortunately, refactoring the storage structure isn't an option at the moment.
If I do have to write my own code to map it, what's the best way to do so using the fewest database hits?
I found that it was easier to reset the sequence manually when I pulled a list, since the retrieval sorted them by sequence.
Our insert and update run through stored procedures which update the sequence (and set several other flags elsewhere, which is why we chose stored procedures), so changes will get persisted.
In this case it made more sense to reset the sequence after the objects were retrieved (given that we know the start index and other constraints on the sequence) than it did to re-architect part of our underlying object and service layers.
I think you answer is in this post, or look at this excellant article on the nhibernate.info site.
*Edit You are asking how you go about adding properties to the "junction" table that has been defined as a many-to-many relationship and may not want to change the mapping to two many-to-one's. So without giving to much magic away I would either a) look at the duplicate question on S.O (my first link) or b) use LINQ-to-objects (click my second link)....
Sorry but I do not believe you need more help.
精彩评论