0

I'm getting NonUniqueObjectException in hibernate.

There is one Item class, I saved list of Item objects using session.save of hibernate.

Now in the same transaction, I'm trying to update same Items using raw sql query which has join with another table. This gives me NonUniqueObjectException. The two tables I'm joining are unrelated as entities for hibernate, that is, there is no foreign key relation.

So I have 2 questions:

  • First, is there any way of using hql for writing inner join queries in hibernate.
  • Second, how to avoid NonUniqueObjectException.
sarvagya kumar
  • 121
  • 1
  • 8
  • please add your transactional method.. with the queries – Maciej Kowalski Feb 01 '17 at 08:10
  • Refer https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/queryhql.html for inner join (Chapter 14). – user1211 Feb 01 '17 at 08:20
  • Also check http://stackoverflow.com/questions/1074081/hibernate-error-org-hibernate-nonuniqueobjectexception-a-different-object-with, some possible reasons for NonUniqueObjectException – user1211 Feb 01 '17 at 08:21
  • @MaciejKowalski - please see the code snippet String sql = "UPDATE Item INNER JOIN BinItem " + "ON Item.itemId = BinItem.itemId " + "SET Item.itemStatus = :itemStatusParam " + "WHERE BinItem.binId = :binIdParam"; Query query = getSession().createSQLQuery(sql); query.setParameter("itemStatusParam", status.toString()); query.setParameter("binIdParam", binId); return query.executeUpdate(); – sarvagya kumar Feb 01 '17 at 08:54
  • @user1211 - doesn't help since the entities are unrelated. – sarvagya kumar Feb 01 '17 at 08:59

1 Answers1

0

One of the things that is working is that I clear the session before making any raw sql query. Any better approach is welcomed.

sarvagya kumar
  • 121
  • 1
  • 8