Hibernate在SessionFactory创建期间抛出此异常:
org.hibernate.loader.MultipleBagFetchException:无法同时获取多个行李
这是我的测试用例:
Parent.java
@Entity
public Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
// @IndexColumn(name="INDEX_COL") if I had this the problem solve but I retrieve more children than I have, one child is null.
private List<Child> children;
}
Run Code Online (Sandbox Code Playgroud)
Child.java
@Entity
public Child {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Parent parent;
}
Run Code Online (Sandbox Code Playgroud)
这个问题怎么样?我能做什么?
编辑
好吧,我遇到的问题是另一个"父"实体在我父母的内部,我的真实行为是这样的:
Parent.java
@Entity
public Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private AntoherParent anotherParent;
@OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
private List<Child> children;
}
Run Code Online (Sandbox Code Playgroud)
AnotherParent.java
@Entity …Run Code Online (Sandbox Code Playgroud) 可能重复:
Hibernate错误:org.hibernate.NonUniqueObjectException:具有相同标识符值的另一个对象已与会话关联
当我使用DAO.update(userbean)时,session.SaveOrUpdate(e);抛出异常:具有相同标识符值的不同对象已经与会话关联
功能如下:
public E save(E e) {
Session session = null;
try {
session = sessionFactory.openSession();
log.debug("session="+session.hashCode()+" save "+e);
session.SaveOrUpdate(e); //here throws exception
session.flush();
}
catch (Exception e1) {
log.err("Cannot open hibernate session "+ e1.getMessage()+" cause : "+e1.getCause());
e1.printStackTrace();
}
finally { if ( session != null ) session.close(); session = null;}
return e ;
}
Run Code Online (Sandbox Code Playgroud)
userbean是UserBean类的实例
public class UserBean{
private List<GroupBean> groups = new ArrayList<GroupBean> ();
private List<RoleBean> roles = new ArrayList<RoleBean> ();
}
public …Run Code Online (Sandbox Code Playgroud) 我有像这个用户几乎相同的问题.
在我的情况下,我从db加载一个实体,我将此实体转换为DataTransferObject,然后我想编辑一个属性,之后我将其转换回entityObject,然后我更新该实体并且hibernate抛出以下错误:
Hibernate Error: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session
Run Code Online (Sandbox Code Playgroud)
显然,问题是我从db检索的对象具有与我想要更新的对象相同的id(就像它应该的那样)但是那些不是同一个对象!
如何管理?谢谢你的帮助...
我的代码:
我正在尝试更新/插入.它被正确插入,唯一的问题是当我尝试更新它时给出了以下错误消息.
private String sessionType=null;
public String getAccountsDetails(List<Account> accountList) {
Session session = sessionFactory.openSession();
for (Account account : accountList) {
AccountDetails accountDetails = new AccountDetails();
accountDetails.setAccountsId(Long.parseLong(account.getId()));
accountDetails.setAccounttype(account.getAccountType().value());
Query query = session.createQuery("from QBAccounts qba where qba.accountsId=:accId");
List<AccountDetails> queryList = query.setParameter("accId", accountDetails.getAccountsId()).list();
if(queryList.size()>0){
session.update(accountDetails);
}else{
session.save(accountDetails);
}
}
session.flush();
session.beginTransaction().commit();
session.clear();
session.close();
return "Successfully data updated into table";
}
Run Code Online (Sandbox Code Playgroud)
错误:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.trinet.mulesoft.quickbooks.dto.AccountDetails#0]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:638)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246) …Run Code Online (Sandbox Code Playgroud) 有这样的问题.加载2个对象的集合(通过主键,使用标准).然后在循环中迭代它们.处理第一个对象时,距离此循环非常远的地方,是由与循环中的第二个对象相同的主键加载对象.在这里,我看到System.identityHashCode()对于这2个对象是不同的.当从循环处理第二个对象并尝试保存它时,我得到异常:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
Run Code Online (Sandbox Code Playgroud)
试图用简单的对象来模拟这个问题,加载,修改,再次通过PK加载,保存不同的事务传播-s我总是得到相同的对象实例...你能告诉我什么时候可以得到第二个对象实例会话由PK加载?
我的班级Foo有方法:
protected void saveMany(Collection<T> many) {
for(Object one : many) {
one = session.merge(one); // also tried this commented out
session.saveOrUpdate(one); // session.merge(one);
}
Run Code Online (Sandbox Code Playgroud)
试图使用saveOrUpdate并合并,但两者都给了我这个例外:
Error: Invocation of method 'saveMany' in class Foo threw exception class org.hibernate.HibernateException : org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [Bar#581301]
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
边注:
当我截断我的表时,保存部分工作,但是当我填充表时,再次运行此方法,从而更新表,它失败,具有此异常