与数据库约束和inverse = true的一对多关系 [英] one-to-many relationshipt with database constrain and inverse=true

查看:118
本文介绍了与数据库约束和inverse = true的一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个类A和B以及hibernate映射

 < hibernate-mapping default-lazy =false> ; 
< class name =Atable =A>
< id name =idtype =long>
< generator class =sequence>< param name =sequence> A_SEQUENCE< / param>< / generator>< / id>
< set name =acascade =allinverse =false>
< key>< column name =A_FKnot-null =true/>< / key>
<一对多课程=B/>< / set>
< / class>
< / hibernate-mapping>

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

在数据库中,表B的列A_FK上有一个非空的约束和一个外键约束。
当我尝试插入包含BI的A时,出现以下错误:
$ b

ORA-01400:无法将NULL插入(SCHEMA。 B。A_FK)

是否可以插入这种数据而不必指定inverse = true标志?和相反的关系?

解决方案

缺少的是集合中的键 not-null =true

 < set name =acascade =allinverse =false> 
< key not-null =true>< column name =A_FKnot-null =true/>< / key>
<一对多课程=B/>
< / set>


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?

解决方案

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>

这篇关于与数据库约束和inverse = true的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆