-->
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.  [ 4 posts ] 
Author Message
 Post subject: Adding objects to collections belonging to a detached object
PostPosted: Fri Oct 01, 2004 3:19 am 
Newbie

Joined: Wed Sep 22, 2004 4:37 am
Posts: 7
Hibernate version: 2.1.6

I have objects A and B, connected by a many-to-many relationship A_B. I'm using a Swing GUI to manipulate data, i.e. load an object of type A including the relationship to B, display in the GUI, and then allow the user to add and delete relationship objects A_B from the loaded object A. Because of the Swing GUI I am not keeping the sessions open, but rather open and close only when directly interacting with the DB.

Here is what I currently do when adding:
- create a new instance of A_B
- add the corresponding object A and object B to the new instance of A_B
- open a session
- save new instance of A_B
- close the session
- open a session
- refresh the object A (session.refresh) and reload the collection of A_B relationships
- close session
- update GUI, so that the new A_B instance is also displayed as part of the collection of A

What I tried and didn't work:
- after saving the new instance of A_B, "manually" add A_B to the collection in A. When I do this, I will get problems later when continuing to work with A (the problem is when I try to do a session.lock(A, LockMode.NONE) to reattach the object to the session, Hibernate complains about "dirty collection").

Questions:
- what is the proper way to add or delete an object A_B related to A, particularly do I have to "manually" add the new A_B instance to the collection (in my case a Set) managed by A (but without provoking "dirty collection" exceptions later)?
- is there a proper way to reload / re-initialize a collection from the database (just the collection, not the whole object which contains the collection)? In my case the object A might be loaded with several related collections, so doing a full refresh might be rather costly, which is why I'm not really happy with the refresh-approach outlined above.


thanks in advance
Peter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 3:14 am 
Newbie

Joined: Wed Sep 22, 2004 4:37 am
Posts: 7
bumping the thread, hoping to get some input...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 06, 2004 8:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
It depends on how these are mapped bi-directional and whether they are cached. The safest bet is to update all sides of the assocations you have defined to Hibernate. Typically this would mean something like:
Code:
A_B ab = new A_B();
ab.setA(a);
ab.setB(b);
a.getBs.add(ab);
b.getAs.add(ab);



or more succintly from client perspective:
Code:
A_B ab = new A_B(a, b);

// where :

public A_B(A a, B b) {
    this.setA(a);
    this.setB(b);
    a.getBs.add(this);
    b.getAs.add(this);
}


Quote:
- after saving the new instance of A_B, "manually" add A_B to the collection in A.

Specifically, this should be done before saving the new instance of A_B...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 08, 2004 7:13 am 
Newbie

Joined: Wed Sep 22, 2004 4:37 am
Posts: 7
Thanks for the feedback!

steve wrote:
It depends on how these are mapped bi-directional and whether they are cached. The safest bet is to update all sides of the assocations you have defined to Hibernate. Typically this would mean something like:
Code:
A_B ab = new A_B();
ab.setA(a);
ab.setB(b);
a.getBs.add(ab);
b.getAs.add(ab);



So the question boils down to: which objects do I have to save / update after having done the adds. When I only insert ab (the new A_B object), then I will get later the "dirty collection" exception. However, when I update a and b, then even though no attributes have changed for these rows, they will get an incremented version or timestamp, which is a problem for me. Am I doing something wrong?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.