Monday, March 22, 2010

Hibernate org.hibernate.NonUniqueObjectException

You probably got this exception because your are trying to insert or update in a loop with some changes like

for (Record record : recordList) {
record.setName(name[i]);

...
...
session.saveOrUpdate(record);
}

When you have a persisted object and you change a value on it, it becomes dirty and hibernate needs to flush these changes to your persistence layer. So that Hibernate can synchronize the underlying persistent store with persistable state held in memory.
Flush and clean session to fix it, like below.

 for (Record record : recordList) {
record.setName(name[i]);
...
...
session.saveOrUpdate(record);
session.flush();
session.clear();
}
Check out my site seenuonjava.com for more

No comments: