views:

32

answers:

1

Hi there,

am using hibernate merge method, to deal with detached instance from entity, and i thought that the return of this method will be a new fetched instance from database as hibernate saveOrUpdate method, but that wasn't the case, and i think it's logic as it's a detached instance, so is there a better way to return the new instance rather than using findById,

regards,

A: 

The merge methdo copies the state of the passed object to a persistent entity with the same identifier (that is either already associated with the session or will be loaded) and then return a reference to that persistent entity. The object passed as parameter is not attached to the session.

So unless I didn't understand the question, I think you should do something like this:

Foo mergedFoo = session.merge(foo);
Pascal Thivent
yes you get it correct, and your answer is clear, but it's my problem :) , i think the only way to get back the original object with new changes, is by calling findById after merging, thnx sir for your answer, Regards
Amr Faisal
@Amr I don't understand. What's the problem with using `mergedFoo`?
Pascal Thivent
mergedFoo is the new instance, after it's merged, the return isn't updated version oh the object reflecting all database changes
Amr Faisal
@Amr The merged instance is what you'll have in the database after the flush. I don't understand.
Pascal Thivent
ok lets say i've an instance of Y contains set of objects X in the runtime, and then changed attribute A in X to false, and then merged Y, now Y contains the same set of X, even if i have an hbm mapping for X in Y states clause 'where X.A == true', i.e this condition won't be applied to current instance of Y
Amr Faisal