简体   繁体   English

一对多关系数据库约束和inverse = true

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

There are two classes A and B and hibernate mappings 有两个类A和B以及休眠映射

<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 BI get the following error: 在数据库上,表B的列A_FK上有一个非null约束和外键约束。当我尝试插入包含BI的A时,出现以下错误:

ORA-01400: cannot insert NULL into ("SCHEMA"."B"."A_FK") ORA-01400:无法将NULL插入(“ SCHEMA”。“ B”。“ A_FK”)

Is it possible to insert this kind of data without having to specify the inverse=true flag? 是否可以在不必指定inverse = true标志的情况下插入此类数据? and the inverse relationship? 和逆关系?

Not without getting rid of the way the id is generated. 并非没有摆脱生成ID的方式。 Can you switch how the Id is generated? 您可以切换ID的生成方式吗?

Converting the problem to a question is half the answer... 将问题转换为问题是答案的一半。

What was missing was the not-null="true" on the key of the set: 缺少的是集合键上的not-null="true"

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM