我的代码:
我正在尝试更新/插入。它被正确插入,唯一的问题是当我尝试更新它时给出了以下错误消息。
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";
}
错误:
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)
编辑2:
我用过
session.merge(accountDetails)
没有错误,但它总是将数据插入到db而不是update。
答案 0 :(得分:5)
public String getAccountsDetails(AccountDetails accountDetails) {
System.out.println("accountDetails.accoundId-->"+accountDetails.getAccountsId());
Session session = sessionFactory.openSession();
Query query = session.createQuery("from QBAccounts qba where qba.accountsId=:accId");
List<AccountDetails> queryList = query.setParameter("accId", accountDetails.getAccountsId()).list();
session.close();
Session session2 = sessionFactory.openSession();
// session.saveOrUpdate(accountDetails);
try{
if(queryList.size()>0){
session2.beginTransaction();
/*
* comment below line-50,51 to use update instead of merge, and uncomment line 53
*/
AccountDetails acDet = (AccountDetails) session2.get(AccountDetails.class, new Long(1));//line 50
session2.merge(accountDetails);//line 51
System.out.println("acDet-->"+acDet+" --accountDetails-> "+accountDetails);//line 52
// session2.update(accountDetails);//line 53
}else{
session2.beginTransaction();
session2.save(accountDetails);
}
}catch(DIRException e){
session2.getTransaction().rollback();
System.out.println("Getting Exception : " + e.getLocalizedMessage());
}finally{
session2.getTransaction().commit();
session2.close();
}
return "Successfully data updated into table";
}
从我的dto中删除了@GeneratedValue,因为值来自UI。
以下是好文章,如何使用MERGE/UPDATE
答案 1 :(得分:5)
打开两个会话时出现此错误。 用户的一个会话和另一个对象的会话,该对象的主键是来自User的外键。 这样,两个会话具有相同的主键或标识符。 我在session.update(type)之前通过session.clear解决了这个问题。
public T update(T type) {
Session session = getSession();
session.clear();
session.update( type );
session.flush();
return type;
}
答案 2 :(得分:4)
仅按照lalit的建议尝试使用一个会话。
代码减少如下。
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);
sessionType = "update";
}else{
session.save(accountDetails);
sessionType = "save";
}
}
session.flush();
session.beginTransaction().commit();
session.clear();
session.close();
return "Successfully data updated into table";
}
答案 3 :(得分:1)
很晚才回复,但其他正在搜索相同错误的人可以知道。这对我有用。在提交之前在循环中保存数据时使用session.flush()并在提交之后使用session.clear()。
session.flush();
session.beginTransaction().commit();
session.clear();