As in hibernate the order in which updates are executed is not deterministic,
you must flush explicitely in order to avoid potential constraints violations:
Code:
Person p1 = session.load(Person.class,1); (CODE="one")
Person p2 = session.load(Person.class,2);(CODE="two")
p2.setCode("three");
session.update(p2);
session.flush(); // writes the changes to the database without commiting
p1.setCode("two");
session.update(p1); // the following commit will success
Another case is when you delete and recreate the same unique constraint key within a single transaction:
Code:
Person p1 = session.load(Person.class,1); (CODE="one")
session.delete(p1);
session.flush(); // writes the changes to the database without commiting
p2 = new Person("one")
session.persist(p2); // the following commit will success