-->
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: Merging lazy fetched children
PostPosted: Fri Aug 25, 2006 5:37 am 
Newbie

Joined: Fri Aug 25, 2006 4:57 am
Posts: 4
Need help with Hibernate? Read this first:
I have a problem with merging a lazily fetched collection of objects.
Let's say we have a class 'A' with with a java.util.List of 'B'-objects. Since I use Hibernate annotations I have something like this:

Code:
@OneToMany(fetch = FetchType.LAZY, targetEntity=B.class, cascade = CascadeType.MERGE)
   public List<B> getBs() {
      return bList;
   }


Lets assume I change some properties of the A's children. But then I decide to undo these changes by getting an equal A-object from database in a different session and merge this object with the A-object hold in the first session:

Code:
Session s1 = sessions.openSession();
Transaction tx1 = session.beginTransaction();

int aId = 1234;
A a = (A) session.get(A.class, new Long(aId));

for(B currentB : a.getBs)
   currentB.setProperty("Something different than saved in database");

tx.commit;

Session s2 = sessions.openSession();
Transaction tx2 = session.beginTransaction();

A newA = (A) session.get(A.class, new Long(aId));
tx2.commit();

s1.merge(newA);


So against my assumption all B-children have not been reset with the database-values. But when I change the fetch-mode to 'EAGER' ...

Code:
@OneToMany(fetch = FetchType.EAGER, targetEntity=B.class, cascade = CascadeType.MERGE)
   public List<B> getBs() {
      return bList;
   }


... everything works fine and b.getPropery() gives me the value stored in the database!

Can anybody help me merging child-objects without switching to eager-fetching?
Code:
Code:
Code:


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