Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 376814

Summary: Cannot merge: org.eclipse.persistence.exceptions.OptimisticLockException
Product: z_Archived Reporter: Bernard Missing name <bht237>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED INVALID QA Contact:
Severity: major    
Priority: P3 CC: tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
EclipseLink NetBeans project in zip file
none
Hibernate Maven project in zip file none

Description Bernard Missing name CLA 2012-04-15 01:04:13 EDT
Build Identifier: 2.3.0.v20110604-r9504

With the attached testcase, it is not possible to merge a detached entity into the persistence context.

The case is really simple, and I am profoundly perplexed. Attached is also the same code as a Hibernate Maven project which works fine.

BTW it would be very useful to get an EclipseLink Maven Repository. Maven is extremely useful for trying things and reporting test cases - No setup in NetBeans, and easy change of tested version - just a number change in the pom.xml file.



Reproducible: Always

Steps to Reproduce:
For EclipseLink:

- Set up your database with persistence.xml
- Open the attached NetBeans project and run SessionTest.java


For Hibernate:
Open the attached Maven project and run SessionTest.java

Expected result: The test should succeed

Actual result:

Exception [EclipseLink-5010] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [ejb.entity.Agreement@9c] cannot be merged because it has changed or been deleted since it was last read. 
Class> ejb.entity.Agreement
Comment 1 Bernard Missing name CLA 2012-04-15 01:05:17 EDT
Created attachment 214018 [details]
EclipseLink NetBeans project in zip file
Comment 2 Bernard Missing name CLA 2012-04-15 01:06:22 EDT
Created attachment 214019 [details]
Hibernate Maven project in zip file
Comment 3 Tom Ware CLA 2012-04-16 08:47:17 EDT
Here is some info about maven:

http://wiki.eclipse.org/EclipseLink/Maven

There is also an initiative to create an Eclipse Maven repository.  When that becomes available, we plan to make use of it.

In general, I suggest posting issues to either the mailing list or the forum prior to filing a bug.  Doing that will increase the number of people that will see the issue and therefore increase the chance of finding a solution or a workaround.
Comment 4 Tom Ware CLA 2012-04-16 09:27:10 EDT
This exception is being thrown because you are calling merge on an Agreement that has not had a version number set on it.  (since version is an int, this means the value is 0)  An agreement with the same id is in the DB with a higher version number from your earlier persist.  The OptimisicLockException seems correct to me.

The best way to get around this is to copy the version number as well in public Agreement(DetachedAgreement detachedAgreement, EntityManager em)

The difference in behavior between EclipseLink and hibernate can likely be attributed to when the products choose to increment the version field.  EclipseLink will do it on flush.  (I am guessing hibernate wait until commit)  In any case, the advice above should work on both.
Comment 5 Bernard Missing name CLA 2012-04-17 03:46:06 EDT
Thanks Tom for your explanation which makes 100% sense to me.

Hibernate does not fail because it starts the version number with 0 so there is no difference between the persisted and detached versions in this special case.

Sorry, It was only Hibernate's behavior that encouraged me to file an issue.
Comment 6 Eclipse Webmaster CLA 2022-06-09 10:28:05 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink