-->
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: In a parent- child relationship i get an empty child set
PostPosted: Mon Feb 16, 2009 1:41 pm 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Hibernate version: 3.0.5


Hi,

Another problem we have to overcome. Imagine we have two entities A and B, A is the parent entity and B the child (FK to A). If i have the following code and execute it, it prints "EMPTY SET !!!!!!!!!!!!!!!!", which is the explanation for this?:


Code:

// transaction already started

A aObj= new A();
aObj.setBSet(new HashSet<B>());

session.saveOrUpdate(aObj);
session.flush();

B bObj= new B();
bObj.setA(aObj);

session.saveOrUpdate(bObj);
session.flush();

Long idA= aObj.getId();

// transaction already finished

// some other code (for example code from an struts action)

// transaction already started

A aObjReturned= session.get(A.class, idA);

if (aObjReturned.getBSet().size()== 0)
   System.out.println("EMPTY SET !!!!!!!!!!!!!!!!");


// transaction already finished



Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2009 2:31 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
In the example code you are showing it is critical what happens between the last session.flush() and session.get(A.class, idA). Your comments seems to indicate that at least some things happen here... The important thing is that the Session object used in the call to session.get(A.class, idA) is not the same session that was used in the call to session.saveOrUpdate(aObj); .

If it is the same session then it is expected that the set is empty because in the very beginning of your example you are creating a new HashSet but you are not adding the B object to it. Hibernate doesn't do this automatically, you need to do it yourself.

If the session is not the same then the information you have given is not enough to solve the problem. It would for example be useful to see mapping files, etc.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 8:33 am 
Beginner
Beginner

Joined: Tue Dec 19, 2006 12:55 pm
Posts: 27
Hi again,

I have checked it, it's the same session object in both transactions, so you are right.

Thanks a lot again.


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.