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

Bug 336630

Summary: testGetReferenceUsedInUpdate fails if run with eclipselink.weaving.changetracking=false
Product: z_Archived Reporter: Andrei Ilitchev <andrei.ilitchev>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: andrei.ilitchev, eclipselink.orm-inbox, guy.pelletier, peter.krogh, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Suggested patch
none
Corrected patch
none
Final patch none

Description Andrei Ilitchev CLA 2011-02-08 10:43:49 EST
TEST SUITE NAME: org.eclipse.persistence.testing.tests.jpa.advanced.EntityManagerJUnitTestSuite
	TEST NAME: testGetReferenceUsedInUpdate(org.eclipse.persistence.testing.tests.jpa.advanced.EntityManagerJUnitTestSuite)
	##FAILURE##
	RESULT:      Error (failure)
	junit.framework.AssertionFailedError: commit fetched object.
	at junit.framework.Assert.fail(Assert.java:47)
	at org.eclipse.persistence.testing.tests.jpa.advanced.EntityManagerJUnitTestSuite.testGetReferenceUsedInUpdate(EntityManagerJUnitTestSuite.java:8350)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at junit.framework.TestCase.runTest(TestCase.java:168)
	at junit.framework.TestCase.runBare(TestCase.java:134)
	at org.eclipse.persistence.testing.framework.junit.JUnitTestCase.runBare(JUnitTestCase.java:503)
	at junit.framework.TestResult$1.protect(TestResult.java:110)
	at junit.framework.TestResult.runProtected(TestResult.java:128)
	at junit.framework.TestResult.run(TestResult.java:113)
	at junit.framework.TestCase.run(TestCase.java:124)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at junit.framework.TestSuite.runTest(TestSuite.java:232)
	at junit.framework.TestSuite.run(TestSuite.java:227)
	at org.eclipse.persistence.testing.framework.TestExecutor.execute(TestExecutor.java:248)
	at org.eclipse.persistence.testing.framework.TestModel.execute(TestModel.java:208)
	at org.eclipse.persistence.testing.framework.TestExecutor.execute(TestExecutor.java:250)
	at org.eclipse.persistence.testing.framework.TestExecutor.runTest(TestExecutor.java:671)
	at org.eclipse.persistence.testing.framework.ui.SynchronizedTestExecutor.run(SynchronizedTestExecutor.java:61)


The FetchGroup has been triggered, which is wrong.
Comment 1 Peter Krogh CLA 2011-06-20 14:25:42 EDT
bug scrub to 2.3.1
Comment 2 Andrei Ilitchev CLA 2011-08-24 11:43:40 EDT
Created attachment 202095 [details]
Suggested patch

The test creates a reference then uses it as a target of OneToOne with a new object. Commit triggers the reference being read in - only if DeferredChangeTracking is used.

I am convinced that the behaviour of reference should be consistent no matter which change policy is used.

More importantly, if the object corresponding to the reference is not in the db - then exception should be thrown.

The question is whether the reference being referenced by another object should be triggered - even if there is no attempt to access its state (which is anything but pk).

The comment to EntityManager.getReference method leaves the door open for both behaviours:
    /**
     * Get an instance, whose state may be lazily fetched.
     * If the requested instance does not exist in the database,
     * the <code>EntityNotFoundException</code> is thrown when the instance 
     * state is first accessed. (The persistence provider runtime is 
     * permitted to throw the <code>EntityNotFoundException</code> when 
     * <code>getReference</code> is called.)...

The suggested patch assumes that Eclipselink will NOT trigger the reference unless its state is directly accessed.
Alternatively Eclipselink should have risen EntityNotFoundException right away if the object does not exist in the db - but currently its not the case.

The fix is simple - when DeferredChangeTrackingPolicy creates the change set don't set initial version value if the object has a FetchGroup that consists only of the PK (that used to trigger the reference).

EntitymanagerJUnitTestSuite.EntityNotFoundException should not have any special cases depending on which change tracking policy is used.
Comment 3 Andrei Ilitchev CLA 2011-08-30 14:16:41 EDT
Created attachment 202450 [details]
Corrected patch

Corrected patch check for fetchGroup==null to avoid NPE on attempt to use FetchGroupManager when it is null.
Comment 4 Guy Pelletier CLA 2011-09-14 16:03:45 EDT
Assigning to Andrei as he has a patch (and I have not been looking at this bug)
Comment 5 Guy Pelletier CLA 2011-09-23 13:24:38 EDT
Created attachment 203922 [details]
Final patch

Includes Andrei's patch and removal of test case change from bug 337365 for the testGetReferenceUsedInUpdate test.
Comment 6 Guy Pelletier CLA 2011-09-23 14:16:34 EDT
Changes submitted to 2.3.1 and trunk.

Verified by: Gordon Yorke

Tests: testGetReferenceUsedInUpdate test now passes with Andrei's fix and the reversal of the test change made in bug 337365
Comment 7 Eclipse Webmaster CLA 2022-06-09 10:19:34 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink