-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problem with multiple relationships between two objects
PostPosted: Wed Jan 07, 2004 6:00 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
We have two objects that have a rather complex relationship. One is the User object, the other is called a Proof. The proof has a many to one relationship with the User as a field called creator. The user has two sets of proofs. One is proofs that the user created (a one-to-many relation), and the other is proofs that the user can see (a many-to-many relation). The problem is that with the many-to-many relationship, nothing gets persisted. If I add a proof to the user's pendingProofs set, it won't stay there. No row is created in the pendingProofz table. If I check the User object for the existence of the proof in the pendingProof set after adding it, it appears to be there, but the change isn't persisted at all. The table structure seems correct. I can't figure this out at all.

Here are the relevant parts to the mapping docs:

User.hbm.xml:
<set name="proofs" cascade="all" inverse="true" lazy="true">
<key column="creator_id" />
<one-to-many class="mc.Proof"/>
</set>

<set name="pendingProofs" lazy="true" table="pendingProofz">
<key column="user_id" />
<many-to-many class="mc.Proof" column="proof_id"/>
</set>


Proof.hbm.xml:
<many-to-one name="creator" class="md.user.User" column="creator_id" />


I apologize if I didn't explain this well, include enough information, or missed a pertinent part in the documentation. I will provide any more info and will gladly read through relevant parts of the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 8:45 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
1. I would add cascade="save-update" to the <set>.
2. Didn't you forget to commit transaction ?

Show code which inserts proof into set and log Hibernate generates.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 11:40 am 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
I added the cascade to the many-to-many set.

The code I use to insert the proof into the set is as follows:

User user = (User)getSession().load(md.user.User.class, (Long) iterator.next());
getProof().getLatestVersion().getUsers().add(user);
user.getPendingProofs().add(getProof());
commitContinue();


When I do that now, I get a TransientObjectException:
The following error has been reported:

java.lang.Exception: net.sf.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: md.user.User


We use a custom set of classes to take care of commits and db connections. The three classes we use are HibernateMonitor (a tapestry extension of sorts), HibernateEngine, and HibernateSessionSource. You can see the source for them here:
http://thechrisproject.com/prog/

Thanks for your reply!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:26 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Quote:
getProof().getLatestVersion().getUsers().add(user);
user.getPendingProofs().add(getProof());


Errr... do you have <set> from Proof to User??? Your mapping does not reflect this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:28 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
dimas wrote:
Quote:
getProof().getLatestVersion().getUsers().add(user);
user.getPendingProofs().add(getProof());


Errr... do you have <set> from Proof to User??? Your mapping does not reflect this.


No, Proofs contain Versions, which contain sets of users. If you think that is relevant, I will post the mapping docs for the Version as well.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 12:41 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Does all of this happens within single session or you are loading some objects in one session and use them in another?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 1:37 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
dimas wrote:
Does all of this happens within single session or you are loading some objects in one session and use them in another?


The proof is loaded in a different session and then set on this page before it is activated. The User is loaded in the session that is got at the beginning of the code I posted.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 2:09 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I thimk it is the problem...

http://forum.hibernate.org/viewtopic.ph ... eassociate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 4:08 pm 
Beginner
Beginner

Joined: Tue Sep 23, 2003 10:03 am
Posts: 26
Location: madison, wi
I think that was it. Someone else solved the problem by just passing the Proof's id to the page and reloading it there, which I guess assures that it is all done in the same session. 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.  [ 9 posts ] 

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.