【Hibernate】项目异常整理

1. More than one row with the given identifier was found: 1

org.springframework.orm.hibernate4.HibernateSystemException: More than one row with the given identifier was found: 1,
for class: com.vanroid.onebuy.entity.Order; nested exception is org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: com.vanroid.onebuy.entity.Order
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:218)

原因:order和stage是一对一的关系。而数据库中又存在多条数据,指向了同一个stage

Order表:
Order表

2. IllegalArgumentException

HQL的queryString 语句不正确,参数的类型不匹配

3. Found shared references to a collection org.hibernate.HibernateException

参考http://stackoverflow.com/questions/1692871/found-shared-references-to-a-collection-org-hibernate-hibernateexception

Hibernate shows this error when you attempt to persist more than one
entity instance sharing thesame collection reference (i.e. the
collection identity in contrast with collection equality).

http://www.coderanch.com/t/531381/ORM/databases/Complete-Reproducing-shared-references-collection

The problem is that Hibernate supplies implementation of the
collection interfaces, for instance Set. You cannot set a collection
of entity A to a collection instance that belongs to another entity.
Assume that you have a one-to-many relation from entities A and B to
some kind items. If you try to move the items from a collection in
entity A to a collection in entity B, you will have to remove the
items from the collection of entity A before adding them to the
collection of entity B. The order of these operations is significant.
If the association is many-to-many, this will not be a problem.

总结就是:BeanUtil.copyProperties()的方式复制一个实体bean的时候,如果这个实体bean有一个集合属性,因为这个方法只是做了一个浅拷贝,所以新拷贝的实体跟旧的实体引用了同一个集合,而这在hibernate中是不允许的,所以报错。

解决方法:
拷贝后,新建一个集合,将原来的集合元素添加进去,并赋值给新拷贝的实体。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值