0

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?

Jeff
  • 2,835
  • 3
  • 41
  • 69
  • possible duplicate of [nhibernate many-to-many mapping - additional column in the mapping table?](http://stackoverflow.com/questions/889513/nhibernate-many-to-many-mapping-additional-column-in-the-mapping-table) – Vadim May 11 '11 at 16:50
  • @Vadim: believe it or not, I did search for answers first, and didn't find that question. I don't know that I'd consider this an exact duplicate, however. – Jeff May 11 '11 at 17:16

2 Answers2

0

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.

Community
  • 1
  • 1
Rippo
  • 22,117
  • 14
  • 78
  • 117
  • Perhaps you could summarize the article from nhforge? It doesn't show up well at all in Firefox, and the article could disappear at any time, making half of your answer meaningless to future users. – Jeff May 11 '11 at 17:17
  • Erh, maybe use IE? Be better than me copying pasting whole page here! – Rippo May 11 '11 at 17:22
  • I've read it in Chrome. It would still improve your answer greatly to have a summary, hitting the high points of the article, in the event that it disappears. – Jeff May 11 '11 at 17:45
  • Well to be honest you have asked a duplicate question and my first link points you to it. – Rippo May 11 '11 at 19:36
0

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.

Jeff
  • 2,835
  • 3
  • 41
  • 69