0

I'm looking at upgrading a legacy application from NHibernate 4 to NHibernate 5 and one of the changes is that now columns cannot be mapped multiple times. However I have an existing mapping which does exactly that and I'm not sure how to resolve it; I need to map the same column for two separate many-to-one relationships from the same entity.

Here is the existing mapping (which obviously works fine with NH 4):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="..." namespace="...">
    <class name="..." table="...">
        <id name="Id">
            <generator class="identity" />
        </id>
        
        <!-- various other properties -->

        <many-to-one name="Board" column="Board_Code" />
        <many-to-one name="Option">
            <column name="Option_Code" />
            <column name="Board_Code" />
        </many-to-one>
    </class>
</hibernate-mapping>

The problem here being the reuse of the Board_Code column.

The column 'Board_Code' has already been added in this SQL builder

It seems to be possible to work around this as detailed in this answer and in this question when both a property and a many-to-one relationship need to be defined. The approach seems to be to just add insert="false" update="false" to the property. However given I'm dealing with two relationships rather than a relationship and a property, I'm not clear on if there's any similar solution (and if so, what the implications would be).

Appreciate any help/advice.

Mark Embling
  • 12,605
  • 8
  • 39
  • 53
  • So you really need Board property in this class? Can't you use `var board = myobj.Option.Board` in the code? – Firo Jun 13 '22 at 12:06
  • Theoretically probably not but it'll make my life a lot easier by keeping it. As I said, this is a (large) legacy application and having to trawl through countless HQL queries and so on to update all instances of Board isn't appealing. But your point is well taken – Mark Embling Jun 17 '22 at 07:36
  • related https://stackoverflow.com/q/6047926/671619 – Firo Jun 17 '22 at 11:49

1 Answers1

1

this might work

<many-to-one name="Board" column="Board_Code" update="false" insert="false"/>
Firo
  • 30,626
  • 4
  • 55
  • 94