Saturday, March 22, 2008

Sometimes Toplink's mergeEntity doesn't....

If you use JDeveloper and you want to use a JPA, odds are you'll pick Toplink. Toplink works well for me, but if you use a session facade bean to access your Toplink queries and objects you may have had a run in, like I have in the past, with the mergeEntity() method.

It seems like sometimes this method just will not save the data to the database, which can be very frustrating. I'll list some common causes and remedies below:

1) Toplink will not update values that compose the key pf an object. The only way I know around this is to delete the old object and create a new one.

2) You are calling mergeEntity() against objects created by a ReadAllQuery. In this case, in your session facade bean method you should make sure that you uncomment the code that detaches the results. It will be directly below a couple of comments that should look like these:
// Uncomment the following lines of code if the results from this query will be mutated.
// See SessionFactory.detach() for more information.

3) The read and the merge are using different transactions. Change the code to use the same transaction for reading/updating.

No comments: