HHH-9638 - Query.getResultList() prepended with dereferencing of no-proxy relation, causes violation of reference equality constraint

- Add test case
This commit is contained in:
Christian Beikov 2017-03-04 03:10:41 +01:00 committed by Vlad Mihalcea
parent 94cb596a73
commit f0cbaef2cd
1 changed files with 80 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<B> 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;
}
}