Hmm, I've actually noticed an interesting "side effect". The save is working well, although it seems to be doing an awful lot of inserts. Looks to me like it completely recreates the Adult to ChildRelation relationship each time (rather than just updating the records that need to be.
Below, I create a new child, and add a relation to an existing adult:
Code:
session=HibernateSessionFactory.getInstance().getSessionFactory().getCurrentSession();
tx=session.beginTransaction();
Child child=new Child();
child.setName("Lexi");
Adult adult=(Adult) session.load(Adult.class,new Integer(1));
ChildRelation cr=new ChildRelation();
cr.setChild(child);
cr.setRelation("Friend");
adult.getRelated_kids().add(cr);
session.saveOrUpdate(child);
session.flush();
tx.commit();
This results in the recreation of all ChildRelations in the Set (even though I just added one):
Code:
Creating session factory from XML
Hibernate: select adult0_.ADULT_ID as ADULT1_0_0_, adult0_.name as name0_0_ from ADULT adult0_ where adult0_.ADULT_ID=?
Hibernate: select related_ki0_.ADULT_ID as ADULT1_1_, related_ki0_.CHILD_ID as CHILD2_1_, related_ki0_.relation as relation1_, child1_.CHILD_ID as CHILD1_2_0_, child1_.name as name2_0_ from ADULT_CHILD related_ki0_ left outer join CHILD child1_ on related_ki0_.CHILD_ID=child1_.CHILD_ID where related_ki0_.ADULT_ID=?
Hibernate: insert into CHILD (name, CHILD_ID) values (?, default)
Hibernate: values identity_val_local()
Hibernate: delete from ADULT_CHILD where ADULT_ID=? and CHILD_ID=? and relation=?
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: delete from ADULT_CHILD where ADULT_ID=? and CHILD_ID=? and relation=?
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Even more interesting, when I load an adult, it does the same thing:
Code:
session=HibernateSessionFactory.getInstance().getSessionFactory().getCurrentSession();
tx=session.beginTransaction();
adult=(Adult) session.load(Adult.class,new Integer(1));
tx.commit();
Resulting SQL:
Code:
Creating session factory from XML
Hibernate: select adult0_.ADULT_ID as ADULT1_0_0_, adult0_.name as name0_0_ from ADULT adult0_ where adult0_.ADULT_ID=?
Hibernate: select related_ki0_.ADULT_ID as ADULT1_1_, related_ki0_.CHILD_ID as CHILD2_1_, related_ki0_.relation as relation1_, child1_.CHILD_ID as CHILD1_2_0_, child1_.name as name2_0_ from ADULT_CHILD related_ki0_ left outer join CHILD child1_ on related_ki0_.CHILD_ID=child1_.CHILD_ID where related_ki0_.ADULT_ID=?
Hibernate: delete from ADULT_CHILD where ADULT_ID=? and CHILD_ID=? and relation=?
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)
Hibernate: insert into ADULT_CHILD (ADULT_ID, CHILD_ID, relation) values (?, ?, ?)