-->
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.  [ 3 posts ] 
Author Message
 Post subject: Empty Set, many to many ass., different instance of java obj
PostPosted: Thu Dec 18, 2008 12:29 pm 
Newbie

Joined: Sun Jul 27, 2008 10:56 am
Posts: 17
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.6

Mapping documents:

Name and version of the database you are using: MySql 5.0.11

Hi all,
my scenario:

many-to-many bidirectional association between A and B

In my app, first I create and persist both objects.
Then I open a dialog to modify object A assigning to it object B, then update db these changes.

public void addB(B b) {
b.addA(this);
bObjects.add(b);
}

Now, when I try to get objects B from A is all OK.
But when I try to get objects A from B it results in a empty set...

My doubts are, that when I open the dialog showing all objects B, I perform a query on DB, resulting in new instances of B objects....and so when i call addB(...) method having a different instance of B object, the collection results right for current instance and wrong for the first created B object.

I would like to know how to make the changes of a instance of B visible to the other instance of B considering that their identifier on DB is the same, and so they are the same entity.

Thanks in advance and best regards
Gamby


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 6:00 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I would like to know how to make the changes of a instance of B visible to the other instance of B considering that their identifier on DB is the same, and so they are the same entity.


This could happen if you are working with more than one Session.
If you are loading the same B with two different sessions you will have two objects representing the same database entity. Changes to one of the objects is not (and can't be) visible in the other object. The solution is to use a single session in the same unit of work. If you do this you are guaranteed to only have one instance of each B entity.


Top
 Profile  
 
 Post subject: Empty Set, many to many ass., different instance of java obj
PostPosted: Thu Dec 18, 2008 6:32 pm 
Newbie

Joined: Sun Jul 27, 2008 10:56 am
Posts: 17
Thanks nordborg,
your explanation is clear, but I don't believe that in my situation I can use the same session, consider my scenario and give me please your advice:

I have a collection of already persisted A objects and they represent my model.
At some point, the user can open a dialog in which he can associate many A objects as he wants to a specific B object.
I initialize that dialog performing a select from A_TABLE, and so, I have a fresh collection of new instances of A objects.
In this case I have a multiplication of java instances of same entity...

Perhaps a solution could be passing to dialog directly the collection of A objects, without retrievin them from DB?

Is there any best practice about using hibernate inside a application with a GUI and a Model?

Thanks again,
best regards
Gamby


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