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

柔情痞子 提交于 2019-12-08 04:09:12

问题


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?


回答1:


Not without getting rid of the way the id is generated. Can you switch how the Id is generated?




回答2:


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>


来源:https://stackoverflow.com/questions/5960739/one-to-many-relationshipt-with-database-constrain-and-inverse-true

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!