Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Manged entities problem

This is correct.  Technically you do not need to do the find() just the
merge() as it will find the object.

If the object is in the cache, then the find() will not access the database.

Merge must find the object because,
1 - It does not know if the object is new, or existing (this check can be
configured in EclipseLink however)
Assuming your JDBC code didn't know this either, it would also have to first
select the object, and would not have a cache to hit either.

2 - Without the original object, it does not know what changed.  If nothing
changed, then the merge will not cause an update, so assuming a cache hit on
the find and no changes you get no database access at all with JPA, where as
JDBC would require a select to check if the object was new, and an update of
all fields as it did not know what changed.
If the object was simple, perhaps your JDBC code could just blindly update
every fields, however what if the object is more complex?  What if you have
an Employee that has a list of Projects and a list of PhoneNumbers, how do
you know if a new PhoneNumber or Project was added, or removed.  If you did
not want to issue a select, you would require to delete all the PhoneNumbers
and Projects and then add them back one by one, this would of coarse
possibly violate other constraints, and so on.

If you are not using caching, and really want to avoid the find, you could
just issue a NativeQuery to update the object.  EclipseLink's native API
also has a lower level mechanism using an UpdateObjectQuery to update an
object, but I would not recommend either option, and would recommend just
using the merge().


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Manged-entities-problem-tp20459687p20461352.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top