My class Foo has method:
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);
}
Tried to use saveOrUpdate and also merge, but both gave me this exception:
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]
How to fix this?
Side note:
When I truncate my table, the saving part works, but when I have the table populated, and run this method again, thus updating the table, it fails, with this exception
merge()did you remember to callsaveOrUpdate()afterwards on the instance returned from merge() and not the instance you passed into merge() as a parameter? See my answer heresession.saveOrUpdate(session.merge(one)), it says :The left-hand side of an assignment must be a variableone = session.merge(one); session.saveOrUpdate(one);