Question

I've got an optional many-to-one relationship between two classes. Hibernate translates the property to be optional by setting the foreign keys to null.

My db-schema does not allow the columns to be null. The property to be optional is represented by the default-value of these columns.

<class name="sth.Alpha" ...>
    ....
    <many-to-one name="beta" not-found="ignore" class="sth.Beta" insert="true" update="true">
        <column name="a1/>
        <column name="a2/>
    </many-to-one>
</class>

<class name="sth.Alpha" ...>
    <composite-id>
        <key-property name="b1" type="int">
            <column name="b1" precision="8" scale="0"/>
        </key-property>
        <key-property name="b2" type="int">
            <column name="b2" precision="8" scale="0"/>
        </key-property>
    </composite-id>
</class>

selecting data is no problem because of not-found="ignore" in the may-to-one-tag it will result in a null-beta-object. But if I want to insert an Alpha? with beta set to null. I get an Exception, that it is not possible to insert null to a1 and a2.

I get rid of that problem if I set insert and update to false. But this results in not saving the relationship if it is set.

Database-Schema cannot be changed and Hibernate-version is fixed to 3.5

I would also be happy if you tell me, that it is not possible

Was it helpful?

Solution

how to use 0 instead of null in conjunction with <id unsavedvalue="whatever"> might help

or

other solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top