今天在写项目的时候爆了这个错误
rpd] 2019-07-13 16:08:31 ERROR GlobalExceptionHandler: Found shared references to a collection: com.rpdgd.freemarket.business.ship.domain.FreeMarketShipPlan.shipWeighDetail; nested exception is org.hibernate.HibernateException: Found shared references to a collection: com.rpdgd.freemarket.business.ship.domain.FreeMarketShipPlan.shipWeighDetail
org.springframework.orm.hibernate3.HibernateSystemException: Found shared references to a collection: com.rpdgd.freemarket.business.ship.domain.FreeMarketShipPlan.shipWeighDetail; nested exception is org.hibernate.HibernateException: Found shared references to a collection: com.rpdgd.freemarket.business.ship.domain.FreeMarketShipPlan.shipWeighDetail
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:690)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:103)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:155)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:192)
at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:150)
at com.gwqm.base.basedao.GenericEntityDao.query(GenericEntityDao.java:87)
at com.gwqm.base.basedao.GenericDAO.query(GenericDAO.java:92)
at com.gwqm.base.service.impl.BaseServiceImpl.query(BaseServiceImpl.java:131)
at com.rpdgd.freemarket.business.order.service.impl.FreeMarketOrderItemServiceImpl.getBySrcOrderItemId(FreeMarketOrderItemServiceImpl.java:84)
at com.rpdgd.freemarket.business.order.service.impl.FreeMarketOrderItemServiceImpl$$FastClassBySpringCGLIB$$3317ce71.invoke(<generated>)
炸一看,莫名其妙, 通过上网查了一下 。说是 不能引用同一个对象。
看了一下这个 https://blog.csdn.net/yucharlie/article/details/75646053
开始我还以为 是 因为我 两个 entity 关联了 同一个对象的问题。
于是 接着看 其他的https://blog.csdn.net/Yoga0301/article/details/80468149
说是 BeanUtil.copyPropertis 的用法问题
查了一下 出错代码的位置
FreeMarketShipPlan srcPlan = new FreeMarketShipPlan();
com.rongpd.util.BeanUtils.copyProperties(plan_db, srcPlan);
然后 这个对象 里面 :
/**
* 关联磅重明细
*/
@OneToMany(mappedBy = "shipPlan", fetch = FetchType.LAZY)
@Where(clause="disabled='"+DISABLE_NORMAL+"'")
private List<FreeMarketShipWeighDetail> shipWeighDetail;
public List<FreeMarketShipWeighDetail> getShipWeighDetail() {
return shipWeighDetail;
}
所以将这个集合 给 复制 copy 给了新的对象。 不能共享同一个集合, 这么回事
解决办法,如果新对象不需要这个 关联 集合, 那么设置为空即可
srcPlan.setShipWeighDetail(null);
就没有问题了。 否则就 新建一个 集合, 将 被复制的对象的集合数据 内容 给 新的集合,
新集合再 放入 新的对象里面即可