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: