oracle-databasehibernateora-01400

one-to-many relationshipt with database constrain and inverse=true


There are two classes A and B and hibernate mappings

<hibernate-mapping  default-lazy="false">
        <class name="A" table="A">
            <id name="id" type="long">  
                <generator class="sequence"><param name="sequence">A_SEQUENCE</param></generator></id>
     <set name="a" cascade="all" inverse="false"  >
            <key><column name="A_FK" not-null="true" /></key>
            <one-to-many class="B" /></set>
   </class>
</hibernate-mapping>

<hibernate-mapping  default-lazy="false">
    <class name="B" table="B">
        <id name="id" type="long"> <column name="ID"/>
            <generator class="sequence"><param name="sequence">B_SEQUENCE</param></generator></id>
       </class>
</hibernate-mapping>

On the database there is a not null contraint and a foreign key constraint on the column A_FK of table B. When I try to insert an A that contains a B I get the following error:

ORA-01400: cannot insert NULL into ("SCHEMA"."B"."A_FK")

Is it possible to insert this kind of data without having to specify the inverse=true flag? and the inverse relationship?


Solution

  • Converting the problem to a question is half the answer...

    What was missing was the not-null="true" on the key of the set:

    <set name="a" cascade="all" inverse="false"  >
            <key not-null="true"><column name="A_FK" not-null="true" /></key>
            <one-to-many class="B" />
    </set>