From f0cbaef2cd201b79c93222d9464deefafc022e47 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Sat, 4 Mar 2017 03:10:41 +0100 Subject: [PATCH] HHH-9638 - Query.getResultList() prepended with dereferencing of no-proxy relation, causes violation of reference equality constraint - Add test case --- .../proxy/ProxyReferenceEqualityTest.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyReferenceEqualityTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyReferenceEqualityTest.java b/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyReferenceEqualityTest.java new file mode 100644 index 0000000000..f645bf1e43 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/proxy/ProxyReferenceEqualityTest.java @@ -0,0 +1,80 @@ +/* + * 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 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 static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Christian Beikov + */ +@TestForIssue(jiraKey = "HHH-9638") +public class ProxyReferenceEqualityTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + A.class, + B.class + }; + } + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } + + @Test + public void testProxyFromQuery() { + doInHibernate( this::sessionFactory, s -> { + A a = new A(); + a.id = 1L; + a.b = new B(); + a.b.id = 1L; + s.persist( a ); + } ); + + doInHibernate( this::sessionFactory, s -> { + A a = s.find( A.class, 1L ); + List result = s.createQuery( "FROM " + B.class.getName() + " b", B.class ).getResultList(); + assertEquals( 1, result.size() ); + assertTrue( a.b == result.get( 0 ) ); + } ); + } + + @Entity + public static class A { + @Id + Long id; + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @LazyToOne(LazyToOneOption.NO_PROXY) + B b; + } + + @Entity + public static class B { + @Id + Long id; + } +}