flush triggers load when using enableDirtyTracking

Description

When bytecode enhancement (enableDirtyTracking) is activated, a flush triggers a load on every proxy instance.

The problems seems to be that the proxy is "around" the ManagedEntity implementation (which is this case is the not-loaded entity). Therefore the proxy is accessed to check the entity state. This access causes the load.

Find attached a sample application. You can trigger the problem by running "mvn clean test". When enableDirtyTracking is deactivated, the problem does not occur and the "Child"-entity is not loaded (see also SQL statements).

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 2016-07-04 00:21:30,448 DEBUG org.hibernate.SQL - select parent0_.id as id1_1_0_, parent0_.child_id as child_id2_1_0_ from Parent parent0_ where parent0_.id=? 2016-07-04 00:21:30,473 DEBUG org.hibernate.SQL - select child0_.id as id1_0_0_ from Child child0_ where child0_.id=? Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.852 sec <<< FAILURE! test(eu.pinske.test.LazyLoadTest) Time elapsed: 2.709 sec <<< ERROR! java.lang.RuntimeException: IMMEDIATE_LOAD:eu.pinske.model.Child#1 at eu.pinske.test.LazyLoadingListener.onLoad(LazyLoadingListener.java:13) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1213) at org.hibernate.internal.SessionImpl.immediateLoad(SessionImpl.java:1071) at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:156) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:260) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:68) at eu.pinske.model.Child_$$_jvst583_1.$$_hibernate_getEntityEntry(Child_$$_jvst583_1.java) at org.hibernate.engine.internal.EntityEntryContext.getEntityEntry(EntityEntryContext.java:158) at org.hibernate.engine.internal.StatefulPersistenceContext.getEntry(StatefulPersistenceContext.java:441) at org.hibernate.engine.spi.CascadingActions$8.isInManagedState(CascadingActions.java:397) at org.hibernate.engine.spi.CascadingActions$8.noCascade(CascadingActions.java:375) at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:119) at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:150) at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:141) at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:74) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1397) at eu.pinske.test.LazyLoadTest.test(LazyLoadTest.java:32) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

Environment

Hibernate 5.2.1, any database

Status

Assignee

Luis Barreiro

Reporter

Alexander Pinske

Fix versions

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.1

Priority

Major
Configure