Quote:
Code between sessionFactory.openSession() and session.close():
Transaction tx = session.beginTransaction();
Set contacts = company.getContacts();
contacts.add(contact);
company.setContacts(contacts);
session.update(company);
tx.commit();
I have also used company.getContacts().add(contact); and I got the same exception
If the above is correct, then contact is created/retrieved outside of the session that you are beginning. You have to synchronise the contact with the current session before using it with objects loaded in this session, e.g. with session.lock() or session.merge().