Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.flush();
// at this point, the set on parent has now been replaced with a PersistentSet...
PersistentSet children = ( PersistentSet ) parent.getChildren();
assertFalse( children.add( child ) );
assertFalse( children.isDirty() );
assertFalse( children.remove( otherChild ) );
assertFalse( children.isDirty() );
HashSet otherSet = new HashSet();
otherSet.add( child );
assertFalse( children.addAll( otherSet ) );
assertFalse( children.isDirty() );
assertFalse( children.retainAll( otherSet ) );
assertFalse( children.isDirty() );
otherSet = new HashSet();
otherSet.add( otherChild );
assertFalse( children.removeAll( otherSet ) );
assertFalse( children.isDirty() );
assertTrue( children.retainAll( otherSet ));
assertTrue( children.isDirty() );
assertTrue( children.isEmpty() );
children.clear();
session.delete( child );
assertTrue( children.isDirty() );
session.flush();
children.clear();
assertFalse( children.isDirty() );
session.delete( parent );
session.getTransaction().commit();
session.close();