-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Persist transient instances question
PostPosted: Fri May 06, 2005 11:18 am 
Newbie

Joined: Fri May 06, 2005 10:28 am
Posts: 1
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:

User
Code:
...
<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>
...


Group
the 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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.