hi folks,
i'm a beginner with hibernate and i've got the following problem/question on persisting transient object references:
part of my model consists of a simple many-to-many relationship between two Classes User and Group. As far as i understand hibernate the following functionality should be supported:
...
User u = new User(....);
Group g = new Group (...);
u.addGroup(g);
// I'm using the HibernateUtil class from caveatemptor
HibernateUtil.beginTransaction();
HibernateUtil.getSession().save(g);
HibernateUtil.commitTransaction();
...
I create User and Group and add the Group to a java.util.Set (HashSet) property of the User. When i persist the User, i think the Group should automatically be persisted as well (of course i have to use cascade='save')
However, the junit test spits the following exception:
Code:
object references an unsaved transient instance - save the transient instance before flushing: Group
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Group at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216) at org.hibernate.type.EntityType.getIdentifier(EntityType.java:93) at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:57) at
...
Do i really have to persist every single Group first before being able to reference or did i make an error somewhere in the mapping or code ?
BTW: i'm using hibernate 3.02 and mysql 4.02
Here are the association-relevant parts of the two involved mapping files:
UserCode:
...
<set
name="groups"
table="user_group"
lazy="false"
cascade="save-update"
sort="unsorted"
>
<key
column="user_id"
>
</key>
<many-to-many
class="Group"
column="group_id"
outer-join="auto"
/>
</set>
...
Groupthe association on the group side is the inverse part:
Code:
...
<set
name="users"
table="user_group"
lazy="false"
inverse="true"
cascade="save-update"
sort="unsorted"
>
<key
column="group_id"
>
</key>
<many-to-many
class="User"
column="user_id"
outer-join="auto"
/>
</set>
...
thanks for your help