简体   繁体   English

休眠列表映射约束违反

[英]Hibernate list mapping constraint violation

These are my postresql tables : 这些是我的postresql表:

CREATE TABLE "user" (
    "id" int4 NOT NULL,
    "activated" bool NOT NULL DEFAULT True,
    CONSTRAINT "user_pkey" PRIMARY KEY("id")
);

CREATE TABLE "user_data" (
    "id" int4 NOT NULL,
    "user_id" int4 NOT NULL,
    "idx" int4 DEFAULT NULL,
    "first_name" text NOT NULL,
    "last_name" text NOT NULL,
    CONSTRAINT "user_data_pkey" PRIMARY KEY("id")
);

My goal is to have in my "User" hibernate bean a direct reference to user's data, like this List<UserData> . 我的目标是在“用户”休眠bean中直接引用用户数据,例如List<UserData> Here is my hibernate mapping : 这是我的休眠映射:

<hibernate-mapping package="mypackage">
    <class name="User" table="user">
        <id name="id" type="long" column="id">
            <generator class="sequence">
                <param name="sequence">user_id_seq</param>
            </generator>
        </id>

        <property name="activated" column="activated" type="boolean" />

        <list name="data" table="user_data" cascade="all">
            <key column="user_id" />
            <list-index column="idx" />
            <one-to-many class="UserData" />
        </list>
    </class>

    <class name="UserData" table="user_data">
        <id 
            name="id" 
            type="long"
            column="id">
            <generator class="sequence">
                <param name="sequence">user_data</param>
            </generator>
        </id> 
        <many-to-one name="user" column="user_id" class="User" />
        <property name="firstName" column="first_name" type="text" />
        <property name="lastName" column="last_name" type="text" />
    </class>    
</hibernate-mapping>

I ran some tests. 我进行了一些测试。 When I add a new "UserData" to my list and then execute session.saveOrUpdate(myUser) it works, but when I remove an entry from my list and execute the same line, it throws ERROR [JDBCExceptionReporter] ERROR: null value in column "user_id" violates not-null constraint just after telling me that batch update user_data set user_id=null, idx=null where user_id='20' and id='46' was canceled 当我向列表中添加新的“ UserData”然后执行session.saveOrUpdate(myUser)它可以工作,但是当我从列表中删除条目并执行同一行时,它会引发ERROR [JDBCExceptionReporter] ERROR: null value in column "user_id" violates not-null constraint在告诉我batch update user_data set user_id=null, idx=null where user_id='20' and id='46' was canceled后, ERROR [JDBCExceptionReporter] ERROR: null value in column "user_id" violates not-null constraint

What is the right usage of this type of mapping? 这种映射的正确用法是什么? Thank you 谢谢

EDIT: Adding "inverse=true" isn't adapted to my usage because I need my update on user trigger an update on UserData. 编辑:添加“ inverse = true”不适合我的用法,因为我需要我的用户更新触发UserData的更新。 Again, maybe I didn't understand the right usage of this type of usage. 再说一次,也许我不理解这种用法​​的正确用法。

It seems like you are trying to update a user_id=null when it has already a not null constraint. 似乎您正在尝试更新user_id = null时已具有非null约束。 Try to remove it or update with a possible user_id. 尝试将其删除或使用可能的user_id更新。

ERROR: null value in column "user_id" violates not-null constraint just after telling me that batch update user_data set user_id=null, idx=null where user_id='20' and id='46' was canceled 错误:在告诉我批量更新user_data设置user_id = null,idx = null(其中user_id = '20'和id = '46'已取消)后,“ user_id”列中的null值违反了非null约束

Your mapping syntax is well. 您的映射语法很好。

You have to mention the relationship owner in a bi-directional relationship (inverse=true/false). 您必须在双向关系中提及关系所有者(inverse = true / false)。

Default value is false. 默认值为false。 This means that each time you try to update User object it will also try to update UserData list. 这意味着每次您尝试更新User对象时,它也会尝试更新UserData列表。

When you set inverse=true then UserData becomes the owner of the bi-directional relationship and hence it will not trigger an update for UserData list when you try update User. 当您设置inverse = true时,UserData成为双向关系的所有者,因此当您尝试更新User时,它不会触发UserData列表的更新。

<list name="data" table="user_data" cascade="all" inverse="true">
  <key column="user_id" />
  <list-index column="idx" />
  <one-to-many class="UserData" />
</list>

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

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