The following code tries to insert an Item object in db using Spring + Hibernate. An element has an Integer id field as a primary key, as well as a name column that obeys a unique constraint (simplified example).
I know that the element identifier is null (the element is temporary), however the insertion may still fail due to the unique restriction in the name field.
try { getHibernateTemplate().save(myItem); getHibernateTemplate().flush(); // exception thrown here if the name is not unique } catch (DataIntegrityViolationException e) { Item itemFromDb = (Item) getHibernateTemplate() .find("from Item item where item.name = ?", myItem.getName()).get(0); // // copy some properties from myItem to itemFromDb // getHibernateTemplate.update (itemFromDb) }
I need this code to run in a loop for many elements, all in one transaction, so I am trying to release an update if the insertion does not work.
However, after the insert failed, the hibernate session is in a weird state, and when I issue a select statement to get the element from db, an original exception is thrown.
I tried using session.evict() after the insert failed, but to no avail. Any idea?
This is what I see on the console after the selection. Hibernate does not work in the select statement, but still prints information about the previous insert statement.
WARN [http-8080-1] hibernate.util.JDBCExceptionReporter (JDBCExceptionReporter.java:77) - SQL Error: 2627, SQLState: 23000 ERROR [http-8080-1] hibernate.util.JDBCExceptionReporter (JDBCExceptionReporter.java:78) - Violation of UNIQUE KEY constraint 'unq_Item_name'. Cannot insert duplicate key in object 'items'. ERROR [http-8080-1] event.def.AbstractFlushingEventListener (AbstractFlushingEventListener.java:301) - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not insert: [com.sample.Item] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
PS please don't tell me about saveOrUpdate sleep mode, I know what it does.
source share