Hibernate :Found shared references to a collection
程序员文章站
2022-04-16 08:52:07
...
Session session=getSessionFactory().getCurrentSession();
Question q=(Question)session.get(Question.class, id);
Question_Engineer qe=new Question_Engineer();
qe.setAdminUser(q.getAdminUser());
qe.setContent(q.getContent());
qe.setCreatTime(DateFormat.DateToStringD());
qe.setDepartment(q.getDepartment());
qe.setDoUser(q.getDoUser());
qe.setFiles(files);
qe.setIp(q.getIp());
qe.setModule(module);
qe.setPhone(q.getPhone());
qe.setQuestionId(q.getQuestionId());
qe.setReportTime(q.getReportTime());
qe.setResolve(q.getResolve());
qe.setState(q.getState());
qe.setStatus(q.getStatus());
qe.setTitle(q.getTitle());
qe.setUser(user);
然后又做持久
org.hibernate.Transaction tx = session.beginTransaction();
try{
//存储monitoringLog
session.save(qe);
//刷新缓存
session.flush();
//提交事务
tx.commit();
}catch (Exception e) {
e.printStackTrace();
//异常回滚
tx.rollback();
}finally{
//关闭连接
session.close();
}
就在session.flush(); 抛了异常
遇到这个问题原因是:1.表之间有关联。在复制数据。
3,你用了hibernate,并在复制数据时将要复制的数据对象A的所有属性赋给了对象B。
解决方案:
将那些关联的数据也复制一遍,并更新B的那些关联对象。这些关联数据中的一个或多个为空时也必须更新B中相应的关联对象
于是:
Module module=new Module();
List<UploadFile> files = new ArrayList<UploadFile>();
User user=q.getUser();
try {
BeanUtils.copyProperties(module, q.getModule());
BeanUtils.copyProperties(user, q.getUser());
BeanUtils.copyProperties(files, q.getFiles());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
然后set进去就不会出现该问题
推荐阅读
-
Hibernate :Found shared references to a collection
-
Hibernate JPA —— could not insert 或 detach entity passed to persist 或Found shared references 错误
-
hibernate异常Found shared references to a collection
-
org.hibernate.HibernateException: Found shared references to a collection: com.jinyi.ihome.module.ex...
-
HibernateSystemException: Found shared references to a collection:
-
HibernateException: Found shared references to a collection 解决办法
-
Hibernate :Found shared references to a collection
-
Hibernate异常Found shared references的解决办法