nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

NH-1886 - When having a discriminator and a many-to-one association on the same column in a superclass insertion fails

Open nhibernate-bot opened this issue 7 years ago • 0 comments

rberens created an issue — :

When having a discrimator and a many-to-one association on the same column in a superclass insertion fails. Consider the following example:

<class name="PaymentType" table="PAYMENT_TYPE"/>
    <id name="Id" type="Int64" column="PAYMENT_TYPE_ID">
        <generator class="native"/>
    </id>
    <property name="Name" column="NAME"/>
</class>

<class name="IPayment" table="PAYMENT">
    <id name="Id" type="Int64" column="PAYMENT_ID">
        <generator class="native"/>
    </id>
    <discriminator column="PAYMENT_TYPE_ID" type="Int64"/>
    <many-to-one name="PaymentType" table="PAYMENT_TYPE" column="PAYMENT_TYPE_ID"/>
    <property name="Amount" column="AMOUNT"/>
    ...
    <subclass name="CreditCardPayment" discriminator-value="CREDIT">
        ...
    </subclass>
    <subclass name="CashPayment" discriminator-value="CASH">
        ...
    </subclass>
    <subclass name="ChequePayment" discriminator-value="CHEQUE">
        ...
    </subclass>
</class>

As you can see the column PAYMENT_TYPE_ID is used twice: both in the discriminator and many-to-one association. This is obviously wrong and when trying to insert a CreditCardPayment the insertion fails in the Dehydrate method in the AbstractEntityPersister, when trying to assign the id. This is because too many parameters are counted (PaymentTypeId is counted twice and therefore the id index is one too high and violates the array bounds. As said before having the PAYMENT_TYPE_ID twice is obviously wrong. It is not detected however when reading the mapping file and therefore very hard to debug. It took me hours of debugging and examining the NHibernate sources to find out, whereas a simple message during reading of the mapping file would have made it clear in a second.

Note that there may be more of such combinations, where a column can be specified twice. Therefore a general check after having read the mapping file could save a lot of time.

nhibernate-bot avatar Oct 12 '17 12:10 nhibernate-bot