开发者

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?


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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜