From 4563c73abb0cdc5ac48700c50052151825c1fca2 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 16 Aug 2021 17:48:11 +0200 Subject: [PATCH] Fix final attributes getter and setter methods log Could not create proxy factory instead of failing SF creation --- ...ityRepresentationStrategyPojoStandard.java | 42 +- .../orm/test/proxy/FinalGetterSetterTest.java | 526 ++++++++++++++++++ .../test/proxy/HibernateUnproxyTest.java | 102 ++-- .../proxy/ProxyReferenceEqualityTest.java | 55 +- .../test/proxy/narrow/AbstractEntity.java | 2 +- .../test/proxy/narrow/ConcreteEntity.java | 2 +- .../narrow/LazyAbstractEntityReference.java | 2 +- .../test/proxy/narrow/ProxyNarrowingTest.java | 82 +++ .../test/proxy/FinalGetterSetterTest.java | 502 ----------------- .../MissingSetterWithEnhancementTest.java | 2 +- .../org/hibernate/test/proxy/ProxyTest.java | 4 +- .../test/proxy/narrow/ProxyNarrowingTest.java | 87 --- 12 files changed, 740 insertions(+), 668 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/proxy/FinalGetterSetterTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/HibernateUnproxyTest.java (66%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/ProxyReferenceEqualityTest.java (55%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/narrow/AbstractEntity.java (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/narrow/ConcreteEntity.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/proxy/narrow/LazyAbstractEntityReference.java (95%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/ProxyNarrowingTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/proxy/FinalGetterSetterTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/ProxyNarrowingTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityRepresentationStrategyPojoStandard.java b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityRepresentationStrategyPojoStandard.java index 90133fb20e..e9fbaa3836 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityRepresentationStrategyPojoStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/internal/EntityRepresentationStrategyPojoStandard.java @@ -230,14 +230,42 @@ public class EntityRepresentationStrategyPojoStandard implements EntityRepresent Iterator properties = bootDescriptor.getPropertyIterator(); Class clazz = bootDescriptor.getMappedClass(); - while ( properties.hasNext() ) { - Property property = (Property) properties.next(); - ProxyFactoryHelper.validateGetterSetterMethodProxyability( "Getter", property.getGetter( clazz ).getMethod() ); - ProxyFactoryHelper.validateGetterSetterMethodProxyability( "Setter", property.getSetter( clazz ).getMethod() ); - } + final Method idGetterMethod; + final Method idSetterMethod; - final Method idGetterMethod = identifierPropertyAccess == null ? null : identifierPropertyAccess.getGetter().getMethod(); - final Method idSetterMethod = identifierPropertyAccess == null ? null : identifierPropertyAccess.getSetter().getMethod(); + try { + while ( properties.hasNext() ) { + Property property = (Property) properties.next(); + ProxyFactoryHelper.validateGetterSetterMethodProxyability( + "Getter", + property.getGetter( clazz ).getMethod() + ); + ProxyFactoryHelper.validateGetterSetterMethodProxyability( + "Setter", + property.getSetter( clazz ).getMethod() + ); + } + if ( identifierPropertyAccess != null ) { + idGetterMethod = identifierPropertyAccess.getGetter().getMethod(); + idSetterMethod = identifierPropertyAccess.getSetter().getMethod(); + ProxyFactoryHelper.validateGetterSetterMethodProxyability( + "Getter", + idGetterMethod + ); + ProxyFactoryHelper.validateGetterSetterMethodProxyability( + "Setter", + idSetterMethod + ); + } + else { + idGetterMethod = null; + idSetterMethod = null; + } + } + catch (HibernateException he) { + LOG.unableToCreateProxyFactory( clazz.getName(), he ); + return null; + } final Method proxyGetIdentifierMethod = idGetterMethod == null || proxyInterface == null ? null diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/FinalGetterSetterTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/FinalGetterSetterTest.java new file mode 100644 index 0000000000..0ac6d908c7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/FinalGetterSetterTest.java @@ -0,0 +1,526 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.proxy; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Version; + +import org.hibernate.Hibernate; +import org.hibernate.ObjectNotFoundException; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Gail Badner + */ +@TestForIssue(jiraKey = "HHH-13891") +@DomainModel( + annotatedClasses = { + FinalGetterSetterTest.EntityWithFinalClass.class, + FinalGetterSetterTest.EntityWithFinalIdGetter.class, + FinalGetterSetterTest.EntityWithFinalIdSetter.class, + FinalGetterSetterTest.EntityWithFinalVersionGetter.class, + FinalGetterSetterTest.EntityWithFinalVersionSetter.class, + FinalGetterSetterTest.EntityWithFinalPropertyGetter.class, + FinalGetterSetterTest.EntityWithFinalPropertySetter.class + } +) +@SessionFactory +public class FinalGetterSetterTest { + + @Test + public void testEntityWithFinalClass(SessionFactoryScope scope) { + scope.inTransaction( session -> + assertNull( session.get( EntityWithFinalClass.class, 999 ) ) + ); + + try { + scope.inTransaction( session -> + session.load( EntityWithFinalClass.class, 999 ) + ); + fail( "Should have thrown ObjectNotFoundException" ); + } + catch (ObjectNotFoundException expected) { + } + + scope.inTransaction( session -> { + final EntityWithFinalClass entity = new EntityWithFinalClass(); + entity.id = 1; + entity.name = "An Entity"; + session.persist( entity ); + } ); + + scope.inTransaction( session -> { + final EntityWithFinalClass entity = session.load( EntityWithFinalClass.class, 1 ); + assertNotNull( entity ); + assertTrue( Hibernate.isInitialized( entity ) ); + } ); + } + + @Test + public void testEntityWithFinalIdGetter(SessionFactoryScope scope) { + scope.inTransaction( + session -> + assertNull( session.get( EntityWithFinalIdGetter.class, 999 ) ) + ); + + try { + scope.inTransaction( + session -> + session.load( EntityWithFinalIdGetter.class, 999 ) + ); + fail( "Should have thrown ObjectNotFoundException" ); + } + catch (ObjectNotFoundException expected) { + } + + scope.inTransaction( + session -> { + final EntityWithFinalIdGetter entity = new EntityWithFinalIdGetter(); + entity.id = 1; + entity.name = "An Entity"; + session.persist( entity ); + } ); + + scope.inTransaction( + session -> { + final EntityWithFinalIdGetter entity = session.load( EntityWithFinalIdGetter.class, 1 ); + assertNotNull( entity ); + assertTrue( Hibernate.isInitialized( entity ) ); + } ); + } + + @Test + public void testEntityWithFinalIdSetter(SessionFactoryScope scope) { + scope.inTransaction( + session -> + assertNull( session.get( EntityWithFinalIdSetter.class, 999 ) ) + ); + + try { + scope.inTransaction( + session -> + session.load( EntityWithFinalIdSetter.class, 999 ) + ); + fail( "Should have thrown ObjectNotFoundException" ); + } + catch (ObjectNotFoundException expected) { + } + + scope.inTransaction( + session -> { + final EntityWithFinalIdSetter entity = new EntityWithFinalIdSetter(); + entity.id = 1; + entity.name = "An Entity"; + session.persist( entity ); + } ); + + scope.inTransaction( + session -> { + final EntityWithFinalIdSetter entity = session.load( EntityWithFinalIdSetter.class, 1 ); + assertNotNull( entity ); + assertTrue( Hibernate.isInitialized( entity ) ); + } ); + } + + @Test + public void testEntityWithFinalVersionGetter(SessionFactoryScope scope) { + scope.inTransaction( + session -> + assertNull( session.get( EntityWithFinalVersionGetter.class, 999 ) ) + ); + + try { + scope.inTransaction( + session -> + session.load( EntityWithFinalVersionGetter.class, 999 ) + ); + fail( "Should have thrown ObjectNotFoundException" ); + } + catch (ObjectNotFoundException expected) { + } + + scope.inTransaction( + session -> { + final EntityWithFinalVersionGetter entity = new EntityWithFinalVersionGetter(); + entity.id = 1; + entity.name = "An Entity"; + session.persist( entity ); + } ); + + scope.inTransaction( + session -> { + final EntityWithFinalVersionGetter entity = session.load( EntityWithFinalVersionGetter.class, 1 ); + assertNotNull( entity ); + assertTrue( Hibernate.isInitialized( entity ) ); + } ); + } + + @Test + public void testEntityWithFinalVersionSetter(SessionFactoryScope scope) { + scope.inTransaction( + session -> + assertNull( session.get( EntityWithFinalVersionSetter.class, 999 ) ) + ); + + try { + scope.inTransaction( + session -> + session.load( EntityWithFinalVersionSetter.class, 999 ) + ); + fail( "Should have thrown ObjectNotFoundException" ); + } + catch (ObjectNotFoundException expected) { + } + + scope.inTransaction( + session -> { + final EntityWithFinalVersionSetter entity = new EntityWithFinalVersionSetter(); + entity.id = 1; + entity.name = "An Entity"; + session.persist( entity ); + } ); + + scope.inTransaction( + session -> { + final EntityWithFinalVersionSetter entity = session.load( EntityWithFinalVersionSetter.class, 1 ); + assertNotNull( entity ); + assertTrue( Hibernate.isInitialized( entity ) ); + } ); + } + + @Test + public void testEntityWithFinalPropertyGetter(SessionFactoryScope scope) { + scope.inTransaction( + session -> + assertNull( session.get( EntityWithFinalPropertyGetter.class, 999 ) ) + ); + + try { + scope.inTransaction( + session -> + session.load( EntityWithFinalPropertyGetter.class, 999 ) + ); + fail( "Should have thrown ObjectNotFoundException" ); + } + catch (ObjectNotFoundException expected) { + } + + scope.inTransaction( + session -> { + final EntityWithFinalPropertyGetter entity = new EntityWithFinalPropertyGetter(); + entity.id = 1; + entity.name = "An Entity"; + session.persist( entity ); + } ); + + scope.inTransaction( + session -> { + final EntityWithFinalPropertyGetter entity = session.load( EntityWithFinalPropertyGetter.class, 1 ); + assertNotNull( entity ); + assertTrue( Hibernate.isInitialized( entity ) ); + } ); + } + + @Test + public void testEntityWithFinalPropertySetter(SessionFactoryScope scope) { + scope.inTransaction( + session -> + assertNull( session.get( EntityWithFinalPropertySetter.class, 999 ) ) + ); + + try { + scope.inTransaction( + session -> + session.load( EntityWithFinalPropertySetter.class, 999 ) + ); + fail( "Should have thrown ObjectNotFoundException" ); + } + catch (ObjectNotFoundException expected) { + } + + scope.inTransaction( + session -> { + final EntityWithFinalPropertySetter entity = new EntityWithFinalPropertySetter(); + entity.id = 1; + entity.name = "An Entity"; + session.persist( entity ); + } ); + + scope.inTransaction( + session -> { + final EntityWithFinalPropertySetter entity = session.load( EntityWithFinalPropertySetter.class, 1 ); + assertNotNull( entity ); + assertTrue( Hibernate.isInitialized( entity ) ); + } ); + } + + @Entity(name = "EntityWithFinalClass") + public static final class EntityWithFinalClass { + + @Id + private int id; + + @Version + @Column(name = "ver") + private int version; + + private String name; + + public final int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "EntityWithFinalIdGetter") + public static class EntityWithFinalIdGetter { + + @Id + private int id; + + @Version + @Column(name = "ver") + private int version; + + private String name; + + public final int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "EntityWithFinalIdSetter") + public static class EntityWithFinalIdSetter { + @Id + private int id; + + @Version + @Column(name = "ver") + private int version; + + private String name; + + public int getId() { + return id; + } + + public final void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "EntityWithFinalVersionGetter") + public static class EntityWithFinalVersionGetter { + @Id + private int id; + + @Version + @Column(name = "ver") + private int version; + + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public final int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "EntityWithFinalVersionSetter") + public static class EntityWithFinalVersionSetter { + @Id + private int id; + + @Version + @Column(name = "ver") + private int version; + + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public final void setVersion(int version) { + this.version = version; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "EntityWithFinalPropertyGetter") + public static class EntityWithFinalPropertyGetter { + @Id + private int id; + + @Version + @Column(name = "ver") + private int version; + + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public final String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } + + @Entity(name = "EntityWithFinalPropertySetter") + public static class EntityWithFinalPropertySetter { + @Id + private int id; + + @Version + @Column(name = "ver") + private int version; + + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public String getName() { + return name; + } + + public final void setName(String name) { + this.name = name; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/HibernateUnproxyTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/HibernateUnproxyTest.java similarity index 66% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/HibernateUnproxyTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/HibernateUnproxyTest.java index 1dc68cbb6e..972bbcb5bc 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/HibernateUnproxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/HibernateUnproxyTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; import java.util.Objects; import javax.persistence.CascadeType; @@ -15,35 +15,45 @@ import javax.persistence.Id; import javax.persistence.OneToOne; import org.hibernate.Hibernate; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class HibernateUnproxyTest extends BaseEntityManagerFunctionalTestCase { +@Jpa( + annotatedClasses = { HibernateUnproxyTest.Parent.class, HibernateUnproxyTest.Child.class } +) +public class HibernateUnproxyTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Parent.class, Child.class }; + @AfterEach + public void tearDown(EntityManagerFactoryScope scope){ + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "update Parent p set p.child = null" ).executeUpdate(); + entityManager.createQuery( "delete from Child" ).executeUpdate(); + entityManager.createQuery( "delete from Parent" ).executeUpdate(); + } + ); } @Test - public void testInitializedProxyCanBeUnproxied() { + public void testInitializedProxyCanBeUnproxied(EntityManagerFactoryScope scope) { Parent p = new Parent(); Child c = new Child(); p.setChild( c ); - doInJPA( this::entityManagerFactory, ( entityManager -> { - entityManager.persist( p ); - } ) ); + scope.inTransaction( entityManager -> + entityManager.persist( p ) + ); - doInJPA( this::entityManagerFactory, ( entityManager -> { + scope.inTransaction( entityManager -> { Parent parent = entityManager.find( Parent.class, p.getId() ); Child child = parent.getChild(); @@ -52,9 +62,9 @@ public class HibernateUnproxyTest extends BaseEntityManagerFunctionalTestCase { Child unproxiedChild = (Child) Hibernate.unproxy( child ); assertEquals( Child.class, unproxiedChild.getClass() ); - } ) ); + } ); - doInJPA( this::entityManagerFactory, ( entityManager -> { + scope.inTransaction( entityManager -> { Parent parent = entityManager.find( Parent.class, p.getId() ); Child child = parent.getChild(); @@ -64,20 +74,21 @@ public class HibernateUnproxyTest extends BaseEntityManagerFunctionalTestCase { Child unproxiedChild = Hibernate.unproxy( child, Child.class ); assertEquals( Child.class, unproxiedChild.getClass() ); - } ) ); + } ); } @Test - public void testNotInitializedProxyCanBeUnproxiedWithInitialization() { + public void testNotInitializedProxyCanBeUnproxiedWithInitialization(EntityManagerFactoryScope scope) { Parent p = new Parent(); Child c = new Child(); p.setChild( c ); - doInJPA( this::entityManagerFactory, ( entityManager -> { - entityManager.persist( p ); - } ) ); + scope.inTransaction( entityManager -> + entityManager.persist( p ) + ); + + scope.inTransaction( entityManager -> { - doInJPA( this::entityManagerFactory, ( entityManager -> { Parent parent = entityManager.find( Parent.class, p.getId() ); Child child = parent.getChild(); @@ -87,9 +98,10 @@ public class HibernateUnproxyTest extends BaseEntityManagerFunctionalTestCase { assertTrue( Hibernate.isInitialized( child ) ); assertEquals( Child.class, unproxiedChild.getClass() ); - } ) ); + } ); + + scope.inTransaction( entityManager -> { - doInJPA( this::entityManagerFactory, ( entityManager -> { Parent parent = entityManager.find( Parent.class, p.getId() ); Child child = parent.getChild(); @@ -99,28 +111,28 @@ public class HibernateUnproxyTest extends BaseEntityManagerFunctionalTestCase { assertTrue( Hibernate.isInitialized( child ) ); assertEquals( Child.class, unproxiedChild.getClass() ); - } ) ); + } ); } @Test - public void testNotHibernateProxyShouldThrowException() { + public void testNotHibernateProxyShouldThrowException(EntityManagerFactoryScope scope) { Parent p = new Parent(); Child c = new Child(); p.setChild( c ); - doInJPA( this::entityManagerFactory, ( entityManager -> { - entityManager.persist( p ); - } ) ); + scope.inTransaction( entityManager -> + entityManager.persist( p ) + ); - doInJPA( this::entityManagerFactory, ( entityManager -> { + scope.inTransaction( entityManager -> { Parent parent = entityManager.find( Parent.class, p.getId() ); assertSame( parent, Hibernate.unproxy( parent ) ); - } ) ); + } ); - doInJPA( this::entityManagerFactory, ( entityManager -> { + scope.inTransaction( entityManager -> { Parent parent = entityManager.find( Parent.class, p.getId() ); assertSame( parent, Hibernate.unproxy( parent, Parent.class ) ); - } ) ); + } ); } @Test @@ -131,25 +143,25 @@ public class HibernateUnproxyTest extends BaseEntityManagerFunctionalTestCase { } @Test - public void testProxyEquality() { - Parent parent = doInJPA( this::entityManagerFactory, ( entityManager -> { + public void testProxyEquality(EntityManagerFactoryScope scope) { + Parent parent = scope.fromTransaction( entityManager -> { Parent p = new Parent(); p.name = "John Doe"; entityManager.persist( p ); return p; - } ) ); + } ); - doInJPA( this::entityManagerFactory, ( entityManager -> { + scope.inTransaction( entityManager -> { Parent p = entityManager.getReference( Parent.class, parent.getId() ); assertFalse( parent.equals( p ) ); assertTrue( parent.equals( Hibernate.unproxy( p ) ) ); - } ) ); + } ); - doInJPA( this::entityManagerFactory, ( entityManager -> { + scope.inTransaction( entityManager -> { Parent p = entityManager.getReference( Parent.class, parent.getId() ); assertFalse( parent.equals( p ) ); assertTrue( parent.equals( Hibernate.unproxy( p, Parent.class ) ) ); - } ) ); + } ); } @Entity(name = "Parent") @@ -200,6 +212,8 @@ public class HibernateUnproxyTest extends BaseEntityManagerFunctionalTestCase { @GeneratedValue private Integer id; + private String name; + @OneToOne(fetch = FetchType.LAZY) private Parent parent; diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyReferenceEqualityTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/ProxyReferenceEqualityTest.java similarity index 55% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyReferenceEqualityTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/ProxyReferenceEqualityTest.java index 04f07e12b7..84d2cf37e5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyReferenceEqualityTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/ProxyReferenceEqualityTest.java @@ -4,50 +4,55 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy; +package org.hibernate.orm.test.proxy; import java.util.List; - import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToOne; - import org.hibernate.annotations.LazyToOne; import org.hibernate.annotations.LazyToOneOption; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author Christian Beikov */ @TestForIssue(jiraKey = "HHH-9638") -public class ProxyReferenceEqualityTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = { + ProxyReferenceEqualityTest.A.class, + ProxyReferenceEqualityTest.B.class + } +) +@SessionFactory +public class ProxyReferenceEqualityTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - A.class, - B.class - }; - } - @Override - protected boolean isCleanupTestDataRequired() { - return true; + @AfterEach + public void tearDown(SessionFactoryScope scope){ + scope.inTransaction( + session -> { + session.createQuery( "delete from A" ).executeUpdate(); + session.createQuery( "delete from B" ).executeUpdate(); + } + ); } @Test - public void testProxyFromQuery() { - doInHibernate( this::sessionFactory, s -> { + public void testProxyFromQuery(SessionFactoryScope scope) { + scope.inTransaction( s -> { A a = new A(); a.id = 1L; a.b = new B(); @@ -55,7 +60,7 @@ public class ProxyReferenceEqualityTest extends BaseCoreFunctionalTestCase { s.persist( a ); } ); - doInHibernate( this::sessionFactory, s -> { + scope.inTransaction( s -> { A a = s.find( A.class, 1L ); List result = s.createQuery( "FROM " + B.class.getName() + " b", B.class ).getResultList(); assertEquals( 1, result.size() ); @@ -67,14 +72,20 @@ public class ProxyReferenceEqualityTest extends BaseCoreFunctionalTestCase { public static class A { @Id Long id; + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @LazyToOne(LazyToOneOption.NO_PROXY) B b; + + String name; + } @Entity(name = "B") public static class B { @Id Long id; + + String name; } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/AbstractEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/AbstractEntity.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/AbstractEntity.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/AbstractEntity.java index 3bf9e1419c..d34ff4fd64 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/AbstractEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/AbstractEntity.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy.narrow; +package org.hibernate.orm.test.proxy.narrow; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/ConcreteEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/ConcreteEntity.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/ConcreteEntity.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/ConcreteEntity.java index d4722d57a8..8682936344 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/ConcreteEntity.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/ConcreteEntity.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy.narrow; +package org.hibernate.orm.test.proxy.narrow; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/LazyAbstractEntityReference.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/LazyAbstractEntityReference.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/LazyAbstractEntityReference.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/LazyAbstractEntityReference.java index aace20d5c8..c503eb95ec 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/LazyAbstractEntityReference.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/LazyAbstractEntityReference.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.proxy.narrow; +package org.hibernate.orm.test.proxy.narrow; import javax.persistence.Entity; import javax.persistence.FetchType; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/ProxyNarrowingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/ProxyNarrowingTest.java new file mode 100644 index 0000000000..9fd98ad92c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/proxy/narrow/ProxyNarrowingTest.java @@ -0,0 +1,82 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.proxy.narrow; + +import org.hibernate.Hibernate; +import org.hibernate.proxy.HibernateProxy; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + + +/** + * @author Yoann Rodière + * @author Guillaume Smet + */ +@DomainModel( + annotatedClasses = { + AbstractEntity.class, ConcreteEntity.class, LazyAbstractEntityReference.class + } +) +@SessionFactory +public class ProxyNarrowingTest { + + @Test + public void testNarrowedProxyIsInitializedIfOriginalProxyIsInitialized(SessionFactoryScope scope) { + + Integer entityReferenceId = scope.fromTransaction( + session -> { + ConcreteEntity entity = new ConcreteEntity(); + session.save( entity ); + + LazyAbstractEntityReference reference = new LazyAbstractEntityReference( entity ); + session.save( reference ); + Integer id = reference.getId(); + + session.flush(); + return id; + } + ); + + + scope.inTransaction( + session -> { + // load a proxified version of the entity into the session: the proxy is based on the AbstractEntity class + // as the reference class property is of type AbstractEntity. + LazyAbstractEntityReference reference = session.get( + LazyAbstractEntityReference.class, + entityReferenceId + ); + AbstractEntity abstractEntityProxy = reference.getEntity(); + + assertTrue( ( abstractEntityProxy instanceof HibernateProxy ) && !Hibernate.isInitialized( + abstractEntityProxy ) ); + Hibernate.initialize( abstractEntityProxy ); + assertTrue( Hibernate.isInitialized( abstractEntityProxy ) ); + + // load the concrete class via session.load to trigger the StatefulPersistenceContext.narrowProxy code + ConcreteEntity concreteEntityProxy = session.load( + ConcreteEntity.class, + abstractEntityProxy.getId() + ); + + // the new proxy created should be initialized + assertTrue( Hibernate.isInitialized( concreteEntityProxy ) ); + assertTrue( session.contains( concreteEntityProxy ) ); + + // clean up + session.delete( reference ); + session.delete( concreteEntityProxy ); + } + ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/FinalGetterSetterTest.java b/hibernate-core/src/test/java/org/hibernate/test/proxy/FinalGetterSetterTest.java deleted file mode 100644 index 7f4c742b5e..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/FinalGetterSetterTest.java +++ /dev/null @@ -1,502 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.proxy; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Version; - -import org.hibernate.Hibernate; -import org.hibernate.ObjectNotFoundException; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; - -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * @author Gail Badner - */ -@TestForIssue( jiraKey = "HHH-13891" ) -public class FinalGetterSetterTest extends BaseNonConfigCoreFunctionalTestCase { - - @Test - public void testEntityWithFinalClass() { - doInHibernate( this::sessionFactory, session -> { - assertNull( session.get( EntityWithFinalClass.class, 999 ) ); - }); - - try { - doInHibernate( this::sessionFactory, session -> { - session.load( EntityWithFinalClass.class, 999 ); - }); - fail( "Should have thrown ObjectNotFoundException" ); - } - catch (ObjectNotFoundException expected) { - } - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalClass entity = new EntityWithFinalClass(); - entity.id = 1; - entity.name = "An Entity"; - session.persist( entity ); - }); - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalClass entity = session.load( EntityWithFinalClass.class, 1 ); - assertNotNull( entity ); - assertTrue( Hibernate.isInitialized( entity ) ); - }); - } - - @Test - public void testEntityWithFinalIdGetter() { - doInHibernate( this::sessionFactory, session -> { - assertNull( session.get( EntityWithFinalIdGetter.class, 999 ) ); - }); - - try { - doInHibernate( this::sessionFactory, session -> { - session.load( EntityWithFinalIdGetter.class, 999 ); - }); - fail( "Should have thrown ObjectNotFoundException" ); - } - catch (ObjectNotFoundException expected) { - } - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalIdGetter entity = new EntityWithFinalIdGetter(); - entity.id = 1; - entity.name = "An Entity"; - session.persist( entity ); - }); - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalIdGetter entity = session.load( EntityWithFinalIdGetter.class, 1 ); - assertNotNull( entity ); - assertTrue( Hibernate.isInitialized( entity ) ); - }); - } - - @Test - public void testEntityWithFinalIdSetter() { - doInHibernate( this::sessionFactory, session -> { - assertNull( session.get( EntityWithFinalIdSetter.class, 999 ) ); - }); - - try { - doInHibernate( this::sessionFactory, session -> { - session.load( EntityWithFinalIdSetter.class, 999 ); - }); - fail( "Should have thrown ObjectNotFoundException" ); - } - catch (ObjectNotFoundException expected) { - } - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalIdSetter entity = new EntityWithFinalIdSetter(); - entity.id = 1; - entity.name = "An Entity"; - session.persist( entity ); - }); - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalIdSetter entity = session.load( EntityWithFinalIdSetter.class, 1 ); - assertNotNull( entity ); - assertTrue( Hibernate.isInitialized( entity ) ); - }); - } - - @Test - public void testEntityWithFinalVersionGetter() { - doInHibernate( this::sessionFactory, session -> { - assertNull( session.get( EntityWithFinalVersionGetter.class, 999 ) ); - }); - - try { - doInHibernate( this::sessionFactory, session -> { - session.load( EntityWithFinalVersionGetter.class, 999 ); - }); - fail( "Should have thrown ObjectNotFoundException" ); - } - catch (ObjectNotFoundException expected) { - } - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalVersionGetter entity = new EntityWithFinalVersionGetter(); - entity.id = 1; - entity.name = "An Entity"; - session.persist( entity ); - }); - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalVersionGetter entity = session.load( EntityWithFinalVersionGetter.class, 1 ); - assertNotNull( entity ); - assertTrue( Hibernate.isInitialized( entity ) ); - }); - } - - @Test - public void testEntityWithFinalVersionSetter() { - doInHibernate( this::sessionFactory, session -> { - assertNull( session.get( EntityWithFinalVersionSetter.class, 999 ) ); - }); - - try { - doInHibernate( this::sessionFactory, session -> { - session.load( EntityWithFinalVersionSetter.class, 999 ); - }); - fail( "Should have thrown ObjectNotFoundException" ); - } - catch (ObjectNotFoundException expected) { - } - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalVersionSetter entity = new EntityWithFinalVersionSetter(); - entity.id = 1; - entity.name = "An Entity"; - session.persist( entity ); - }); - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalVersionSetter entity = session.load( EntityWithFinalVersionSetter.class, 1 ); - assertNotNull( entity ); - assertTrue( Hibernate.isInitialized( entity ) ); - }); - } - - @Test - public void testEntityWithFinalPropertyGetter() { - doInHibernate( this::sessionFactory, session -> { - assertNull( session.get( EntityWithFinalPropertyGetter.class, 999 ) ); - }); - - try { - doInHibernate( this::sessionFactory, session -> { - session.load( EntityWithFinalPropertyGetter.class, 999 ); - }); - fail( "Should have thrown ObjectNotFoundException" ); - } - catch (ObjectNotFoundException expected) { - } - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalPropertyGetter entity = new EntityWithFinalPropertyGetter(); - entity.id = 1; - entity.name = "An Entity"; - session.persist( entity ); - }); - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalPropertyGetter entity = session.load( EntityWithFinalPropertyGetter.class, 1 ); - assertNotNull( entity ); - assertTrue( Hibernate.isInitialized( entity ) ); - }); - } - - @Test - public void testEntityWithFinalPropertySetter() { - doInHibernate( this::sessionFactory, session -> { - assertNull( session.get( EntityWithFinalPropertySetter.class, 999 ) ); - }); - - try { - doInHibernate( this::sessionFactory, session -> { - session.load( EntityWithFinalPropertySetter.class, 999 ); - }); - fail( "Should have thrown ObjectNotFoundException" ); - } - catch (ObjectNotFoundException expected) { - } - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalPropertySetter entity = new EntityWithFinalPropertySetter(); - entity.id = 1; - entity.name = "An Entity"; - session.persist( entity ); - }); - - doInHibernate( this::sessionFactory, session -> { - final EntityWithFinalPropertySetter entity = session.load( EntityWithFinalPropertySetter.class, 1 ); - assertNotNull( entity ); - assertTrue( Hibernate.isInitialized( entity ) ); - }); - } - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { - EntityWithFinalClass.class, - EntityWithFinalIdGetter.class, - EntityWithFinalIdSetter.class, - EntityWithFinalVersionGetter.class, - EntityWithFinalVersionSetter.class, - EntityWithFinalPropertyGetter.class, - EntityWithFinalPropertySetter.class - }; - } - - @Entity( name = "EntityWithFinalClass") - public static final class EntityWithFinalClass { - - @Id - private int id; - - @Version - @Column(name = "ver") - private int version; - - private String name; - - public final int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - - @Entity( name = "EntityWithFinalIdGetter") - public static class EntityWithFinalIdGetter { - - @Id - private int id; - - @Version - @Column(name = "ver") - private int version; - - private String name; - - public final int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - - @Entity( name = "EntityWithFinalIdSetter") - public static class EntityWithFinalIdSetter { - @Id - private int id; - - @Version - @Column(name = "ver") - private int version; - - private String name; - - public int getId() { - return id; - } - - public final void setId(int id) { - this.id = id; - } - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - - @Entity( name = "EntityWithFinalVersionGetter") - public static class EntityWithFinalVersionGetter { - @Id - private int id; - - @Version - @Column(name = "ver") - private int version; - - private String name; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public final int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - - @Entity( name = "EntityWithFinalVersionSetter") - public static class EntityWithFinalVersionSetter { - @Id - private int id; - - @Version - @Column(name = "ver") - private int version; - - private String name; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getVersion() { - return version; - } - - public final void setVersion(int version) { - this.version = version; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - - @Entity( name = "EntityWithFinalPropertyGetter") - public static class EntityWithFinalPropertyGetter { - @Id - private int id; - - @Version - @Column(name = "ver") - private int version; - - private String name; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public final String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - } - - @Entity( name = "EntityWithFinalPropertySetter") - public static class EntityWithFinalPropertySetter { - @Id - private int id; - - @Version - @Column(name = "ver") - private int version; - - private String name; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public String getName() { - return name; - } - - public final void setName(String name) { - this.name = name; - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java b/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java index 4cb59b1ad8..88ae5ba120 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/proxy/MissingSetterWithEnhancementTest.java @@ -64,7 +64,7 @@ public class MissingSetterWithEnhancementTest { catch (MappingException e) { assertEquals( "Could not locate setter method for property [" + EntityWithMissingSetter.class.getName() + "#name]", - e.getCause().getCause().getCause().getMessage() + e.getMessage() ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java b/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java index dac278a905..171015cca0 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyTest.java @@ -366,8 +366,8 @@ public class ProxyTest extends BaseCoreFunctionalTestCase { Container proxy = ( Container ) s.load( Container.class, lastContainerId ); assertFalse( Hibernate.isInitialized( proxy ) ); // load the rest back into the PC - List all = s.createQuery( "from Container as c inner join fetch c.owner inner join fetch c.dataPoints where c.id <> :last" ) - .setParameter( "last", lastContainerId.longValue() ) + List all = s.createQuery( "from Container as c inner join fetch c.owner inner join fetch c.dataPoints where c.id <> :l" ) + .setParameter( "l", lastContainerId.longValue() ) .list(); Container container = ( Container ) all.get( 0 ); s.delete( container ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/ProxyNarrowingTest.java b/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/ProxyNarrowingTest.java deleted file mode 100644 index c3f23b4e69..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/proxy/narrow/ProxyNarrowingTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.proxy.narrow; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.proxy.HibernateProxy; - -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -/** - * @author Yoann Rodière - * @author Guillaume Smet - */ -public class ProxyNarrowingTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { AbstractEntity.class, ConcreteEntity.class, LazyAbstractEntityReference.class }; - } - - @Test - public void testNarrowedProxyIsInitializedIfOriginalProxyIsInitialized() { - Session session = openSession(); - - Integer entityReferenceId = null; - - // Populate the database - try { - Transaction t = session.beginTransaction(); - - ConcreteEntity entity = new ConcreteEntity(); - session.save( entity ); - - LazyAbstractEntityReference reference = new LazyAbstractEntityReference( entity ); - session.save( reference ); - entityReferenceId = reference.getId(); - - session.flush(); - t.commit(); - } - finally { - session.close(); - } - - session = openSession(); - - try { - session.beginTransaction(); - - // load a proxified version of the entity into the session: the proxy is based on the AbstractEntity class - // as the reference class property is of type AbstractEntity. - LazyAbstractEntityReference reference = session.get( LazyAbstractEntityReference.class, entityReferenceId ); - AbstractEntity abstractEntityProxy = reference.getEntity(); - - assertTrue( ( abstractEntityProxy instanceof HibernateProxy ) && !Hibernate.isInitialized( abstractEntityProxy ) ); - Hibernate.initialize( abstractEntityProxy ); - assertTrue( Hibernate.isInitialized( abstractEntityProxy ) ); - - // load the concrete class via session.load to trigger the StatefulPersistenceContext.narrowProxy code - ConcreteEntity concreteEntityProxy = session.load( ConcreteEntity.class, abstractEntityProxy.getId() ); - - // the new proxy created should be initialized - assertTrue( Hibernate.isInitialized( concreteEntityProxy ) ); - assertTrue( session.contains( concreteEntityProxy ) ); - - - // clean up - session.delete( reference ); - session.delete( concreteEntityProxy ); - - session.getTransaction().commit(); - } - finally { - session.close(); - } - } - -}