休眠保存奇怪的行为 [英] Hibernate Save strange behaviour

查看:102
本文介绍了休眠保存奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与String类型有一对多关系的用户对象。我相信他们是简单的映射。类型表包含关联的user_id和变量类型名称,主键'id'基本上是一个计数器。

 < ; class name =Userstable =users> 
< id column =idname =id/>
...
< set name =typestable =typescascade =save-update>
< key column =id/>
<一对多等级=类型/>
< / set>
< / class>

< class name =Typestable =types>
< id column =idname =id/>
< property column =user_idname =user_idtype =integer/>
< property column =typename =typetype =string/>
< / class>

这是我用于添加到数据库的java:

  User u = new User(); 
u.setId(user_id);
...
收藏<类型> t = new HashSet< Types>();
t.add(new Type(auto_incremented_id,user_id,type_name));
u.setTypes(t);

getHibernateTemplate()。saveOrUpdate(u);

当我运行它时,会出现以下错误:

  61468 [http-8080-3] WARN org.hibernate.util.JDBCExceptionReporter  -  SQL错误:1062,SQLState:23000 
61468 [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter - 键'PRIMARY'的重复条目'6'
61468 [http-8080-3] ERROR org.hibernate.event.def.AbstractFlushingEventListener - 无法使数据库状态与会话$同步b $ b org.hibernate.exception.ConstraintViolationException:无法执行JDBC批量更新

当我检查sql,它显示:
$ b

  Hibernate:插入用户(name,id)值(?,?)
Hibernate :插入类型(user_id,type,id)值(?,?,?)
Hibernate:更新类型集id =?其中id =?




  • 为什么Hibernate尝试更新类型的id?



错误提示:重复键入'PRIMARY'的条目'6',但确实没有?我确保每次都增加id。



我记录了进入的信息,添加的类型的ID为7,用户ID为6。难道说Hibernate的user_id是6,并试图更新类型并设置id = 6,其中id = 7?因此,重复的主键错误?



但为什么它会做这么奇怪的事情?有没有办法阻止它更新?




  • 我应该手动设置ID吗?如果不是,那么我应该如何添加这些类型?当我添加一个只有类型字符串且没有ID的类型对象时,它会给出其他错误。


我们已经仔细研究了好几天......

key> 映射 - 它应该是user_id,而不是id。也就是说,你的整个映射对我来说似乎有点奇怪。首先,如果你想让ID自动生成,你应该让Hibernate通过指定适当的生成器来处理:

 
< id列=idname =id>
< generator class =native/>
< / id>

阅读 Hibernate Documentation 有关各种可用选项的生成器。第二,如果你需要的只是一组字符串类型,考虑将它们重新映射到一组元素中,而不是一对多的关系:

>

 
< key column =user_id/>
< element column =typetype =string/>
< / set>

这样您就不需要显式的类型类或映射。即使您确实希望在类型上具有其他属性,您仍然可以将其映射为组件而不是实体。最后,如果由于某些您尚未描述的要求,类型必须是实体,则用户和类型之间的关系是双向的,并且需要这样映射:

 
< key column =user_id/>
<一对多等级=类型/>
< / set>

...
类型映射:
<多对一名称=usercolumn =user_idnot-null =true/>

在后一种情况下,类型必须具有用户类型的用户属性。
此处是一个详细的例子。

I have a user object that has a one-to-many relationship with String types. I believe they are simple mappings. The types table hold the associated user_id and variable type names, with a primary key 'id' that is basically a counter.

<class name="Users" table="users">
    <id column="id" name="id" />
    ...
    <set name="types" table="types" cascade="save-update">
        <key column="id" />
        <one-to-many class="Types" />
    </set>
</class>

<class name="Types" table="types">
    <id column="id" name="id" />
    <property column="user_id" name="user_id" type="integer" />
    <property column="type" name="type" type="string" />
</class>

This is the java I used for adding to the database:

User u = new User();
u.setId(user_id);
...
Collection<Types> t = new HashSet<Types>();
t.add(new Type(auto_incremented_id, user_id, type_name));
u.setTypes(t);

getHibernateTemplate().saveOrUpdate(u);

When I run it, it gives this error:

61468 [http-8080-3] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
61468 [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry '6' for key 'PRIMARY'
61468 [http-8080-3] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

When I check the sql, it shows:

Hibernate: insert into users (name, id) values (?, ?)
Hibernate: insert into types (user_id, type, id) values (?, ?, ?)
Hibernate: update types set id=? where id=?

  • Why does Hibernate try to update the types' id?

The error says: Duplicate entry '6' for key 'PRIMARY', but there really isn't? I made sure the ids are incremented each time. And the users and types are added into the database correctly.

I logged the information going in, and the types added has an id of 7 and a user id of 6. Could it be that Hibernate takes the user_id of 6 and tried to update types and set id=6 where id=7? Therefore the duplicate primary key error?

But why would it do something so strange? Is there a way to stop it from updating?

  • Should I set the id manually? If not, then how should I add the types? It gives other errors when I add a type object that only has a type string in it and no ids.

Thanks guys. Been mulling over it for days...

解决方案

Your biggest problem is incorrect column in the <key> mapping - it should be "user_id", not "id". That said, your whole mapping seems a bit strange to me.

First of all, if you want IDs auto generated you should really let Hibernate take care of that by specifying appropriate generator:

 <id column="id" name="id">
   <generator class="native"/>
 </id>

Read Hibernate Documentation on generators for various available options.

Secondly, if all you need is a set of string types, consider re-mapping them into a collection of elements rather than one-to-many relationship:

 <set name="types" table="types">
    <key column="user_id"/>
    <element column="type" type="string"/>
 </set> 

That way you won't need explicit "Types" class or mapping for it. Even if you do want to have additional attributes on "Types", you can still map it as component rather than entity.

Finally, if "Types" must be an entity due to some requirement you have not described, the relationship between "Users" and "Types" is bi-directional and needs to be mapped as such:

 <set name="types" table="types" inverse="true">
    <key column="user_id"/>
    <one-to-many class="Types"/>
 </set>

 ...
 in Types mapping:
 <many-to-one name="user" column="user_id" not-null="true"/>

In the latter case "Types" would have to have a "user" property of type "Users". Here is a detailed example.

这篇关于休眠保存奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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