Fix use of proxy as entity instance in AbstractEntityInitializer

This commit is contained in:
Andrea Boriero 2021-06-25 18:40:11 +02:00
parent fcee504c99
commit 911173d6eb
16 changed files with 474 additions and 449 deletions

View File

@ -465,10 +465,10 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
final PersistenceContext persistenceContext = session.getPersistenceContext();
final Object proxy = getProxy( persistenceContext );
// Special case map proxy to avoid stack overflows
// We know that a map proxy will always be of "the right type" so just use that object
if ( proxy != null && ( proxy instanceof MapProxy || concreteDescriptor.isInstance( proxy ) ) ) {
if ( proxy != null && ( proxy instanceof MapProxy
|| entityDescriptor.getJavaTypeDescriptor().getJavaTypeClass().isInstance( proxy ) ) ) {
entityInstance = proxy;
}
else {

View File

@ -6,7 +6,7 @@
*/
//$Id: Address.java 4373 2004-08-18 09:18:34Z oneovthafew $
package org.hibernate.test.discriminator;
package org.hibernate.orm.test.discriminator;
/**

View File

@ -6,7 +6,7 @@
*/
//$Id: Customer.java 4373 2004-08-18 09:18:34Z oneovthafew $
package org.hibernate.test.discriminator;
package org.hibernate.orm.test.discriminator;
/**

View File

@ -0,0 +1,315 @@
/*
* 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.orm.test.discriminator;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author Gavin King
*/
@DomainModel(
xmlMappings = "org/hibernate/orm/test/discriminator/Person.hbm.xml"
)
@SessionFactory
public class DiscriminatorTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session ->
session.createQuery( "delete from Person" ).executeUpdate()
);
}
@Test
public void testDiscriminatorSubclass(SessionFactoryScope scope) {
scope.inTransaction(
s -> {
Employee mark = new Employee();
mark.setName( "Mark" );
mark.setTitle( "internal sales" );
mark.setSex( 'M' );
mark.setAddress( "buckhead" );
mark.setZip( "30305" );
mark.setCountry( "USA" );
Customer joe = new Customer();
joe.setName( "Joe" );
joe.setAddress( "San Francisco" );
joe.setZip( "XXXXX" );
joe.setCountry( "USA" );
joe.setComments( "Very demanding" );
joe.setSex( 'M' );
joe.setSalesperson( mark );
Person yomomma = new Person();
yomomma.setName( "mum" );
yomomma.setSex( 'F' );
s.save( yomomma );
s.save( mark );
s.save( joe );
try {
s.createQuery( "from java.io.Serializable" ).list();
fail( "Expected IllegalAccessException" );
}
catch (Exception e) {
assertThat( e, instanceOf( IllegalArgumentException.class ) );
}
assertThat( s.createQuery( "from Person" ).list().size(), is( 3 ) );
assertThat( s.createQuery( "from Person p where p.class = Person" ).list().size(), is( 1 ) );
assertThat( s.createQuery( "from Person p where p.class = Customer" ).list().size(), is( 1 ) );
s.clear();
List<Customer> customers = s.createQuery( "from Customer c left join fetch c.salesperson" ).list();
for ( Customer c : customers ) {
assertTrue( Hibernate.isInitialized( c.getSalesperson() ) );
assertThat( c.getSalesperson().getName(), is( "Mark" ) );
}
assertThat( customers.size(), is( 1 ) );
s.clear();
customers = s.createQuery( "from Customer" ).list();
for ( Customer c : customers ) {
assertFalse( Hibernate.isInitialized( c.getSalesperson() ) );
assertThat( c.getSalesperson().getName(), is( "Mark" ) );
}
assertThat( customers.size(), is( 1 ) );
s.clear();
mark = s.get( Employee.class, mark.getId() );
joe = s.get( Customer.class, joe.getId() );
mark.setZip( "30306" );
assertThat( s.createQuery( "from Person p where p.address.zip = '30306'" ).list().size(), is( 1 ) );
s.delete( mark );
s.delete( joe );
s.delete( yomomma );
assertTrue( s.createQuery( "from Person" ).list().isEmpty() );
}
);
}
@Test
public void testAccessAsIncorrectSubclass(SessionFactoryScope scope) {
Employee employee = new Employee();
scope.inTransaction(
s -> {
employee.setName( "Steve" );
employee.setSex( 'M' );
employee.setTitle( "grand poobah" );
s.save( employee );
}
);
Customer c = null;
scope.fromTransaction(
s -> s.get( Customer.class, employee.getId() )
);
assertNull( c );
scope.inTransaction(
s -> {
Employee e = s.get( Employee.class, employee.getId() );
Customer customer = s.get( Customer.class, employee.getId() );
assertNotNull( e );
assertNull( customer );
}
);
scope.inTransaction(
session -> session.delete( employee )
);
}
@Test
public void testQuerySubclassAttribute(SessionFactoryScope scope) {
scope.inTransaction(
s -> {
Person p = new Person();
p.setName( "Emmanuel" );
p.setSex( 'M' );
s.persist( p );
Employee q = new Employee();
q.setName( "Steve" );
q.setSex( 'M' );
q.setTitle( "Mr" );
q.setSalary( new BigDecimal( 1000 ) );
s.persist( q );
List result = s.createQuery( "from Person where salary > 100" ).list();
assertEquals( result.size(), 1 );
assertSame( result.get( 0 ), q );
result = s.createQuery( "from Person where salary > 100 or name like 'E%'" ).list();
assertEquals( result.size(), 2 );
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
criteria.where( criteriaBuilder.gt( root.get( "salary" ), new BigDecimal( 100 ) ) );
result = s.createQuery( criteria ).list();
// result = s.createCriteria(Person.class)
// .add( Property.forName( "salary").gt( new BigDecimal( 100) ) )
// .list();
assertEquals( result.size(), 1 );
assertSame( result.get( 0 ), q );
//TODO: make this work:
/*result = s.createQuery("select salary from Person where salary > 100").list();
assertEquals( result.size(), 1 );
assertEquals( result.get(0), new BigDecimal(1000) );*/
s.delete( p );
s.delete( q );
}
);
}
@Test
public void testLoadSuperclassProxyPolymorphicAccess(SessionFactoryScope scope) {
Employee e = new Employee();
scope.inTransaction(
s -> {
e.setName( "Steve" );
e.setSex( 'M' );
e.setTitle( "grand poobah" );
s.save( e );
}
);
scope.inTransaction(
s -> {
// load the superclass proxy.
Person pLoad = s.load( Person.class, e.getId() );
assertTrue( pLoad instanceof HibernateProxy );
Person pGet = s.get( Person.class, e.getId() );
Person pQuery = (Person) s.createQuery( "from Person where id = :id" )
.setParameter( "id", e.getId() )
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
criteria.where( criteriaBuilder.equal( root.get( "id" ), e.getId() ) );
Person pCriteria = s.createQuery( criteria ).uniqueResult();
// Person pCriteria = ( Person ) s.createCriteria( Person.class )
// .add( Restrictions.idEq( new Long( e.getId() ) ) )
// .uniqueResult();
// assert that executing the queries polymorphically returns the same proxy
assertSame( pLoad, pGet );
assertSame( pLoad, pQuery );
assertSame( pLoad, pCriteria );
// assert that the proxy is not an instance of Employee
assertFalse( pLoad instanceof Employee );
}
);
scope.inTransaction(
s -> {
// load the superclass proxy.
Person pLoad = s.load( Person.class, e.getId() );
assertTrue( pLoad instanceof HibernateProxy );
Person pGet = s.get( Person.class, e.getId() );
Person pQuery = (Person) s.createQuery( "from Person where id = :id" )
.setParameter( "id", e.getId() )
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery( Employee.class );
Root<Employee> root = criteria.from( Employee.class );
criteria.where( criteriaBuilder.equal( root.get( "id" ), e.getId() ) );
Employee pCriteria = s.createQuery( criteria ).uniqueResult();
// Person pCriteria = ( Person ) s.createCriteria( Person.class )
// .add( Restrictions.idEq( new Long( e.getId() ) ) )
// .uniqueResult();
// assert that executing the queries polymorphically returns the same proxy
assertSame( pLoad, pGet );
assertSame( pLoad, pQuery );
assertNotSame( pLoad, pCriteria );
// assert that the proxy is not an instance of Employee
assertFalse( pLoad instanceof Employee );
}
);
scope.inTransaction(
s -> s.delete( e )
);
}
@Test
public void testLoadSuperclassProxyEvictPolymorphicAccess(SessionFactoryScope scope) {
Employee e = new Employee();
scope.inTransaction(
s -> {
e.setName( "Steve" );
e.setSex( 'M' );
e.setTitle( "grand poobah" );
s.save( e );
}
);
scope.inTransaction(
s -> {
// load the superclass proxy.
Person pLoad = s.load( Person.class, e.getId() );
assertTrue( pLoad instanceof HibernateProxy );
// evict the proxy
s.evict( pLoad );
Employee pGet = (Employee) s.get( Person.class, e.getId() );
Employee pQuery = (Employee) s.createQuery( "from Person where id = :id" )
.setParameter( "id", e.getId() )
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
criteria.where( criteriaBuilder.equal( root.get( "id" ), e.getId() ) );
Employee pCriteria = (Employee) s.createQuery( criteria ).uniqueResult();
// Employee pCriteria = ( Employee ) s.createCriteria( Person.class )
// .add( Restrictions.idEq( new Long( e.getId() ) ) )
// .uniqueResult();
// assert that executing the queries polymorphically returns the same Employee instance
assertSame( pGet, pQuery );
assertSame( pGet, pCriteria );
}
);
scope.inTransaction(
s -> s.delete( e )
);
}
}

View File

@ -6,7 +6,7 @@
*/
//$Id: Employee.java 4373 2004-08-18 09:18:34Z oneovthafew $
package org.hibernate.test.discriminator;
package org.hibernate.orm.test.discriminator;
import java.math.BigDecimal;
/**

View File

@ -6,7 +6,7 @@
*/
//$Id: Employee.java 4373 2004-08-18 09:18:34Z oneovthafew $
package org.hibernate.test.discriminator;
package org.hibernate.orm.test.discriminator;
import java.math.BigDecimal;
/**

View File

@ -18,7 +18,7 @@
-->
<hibernate-mapping
package="org.hibernate.test.discriminator"
package="org.hibernate.orm.test.discriminator"
default-access="field">
<class name="Person"

View File

@ -6,7 +6,7 @@
*/
//$Id: Person.java 4373 2004-08-18 09:18:34Z oneovthafew $
package org.hibernate.test.discriminator;
package org.hibernate.orm.test.discriminator;

View File

@ -11,7 +11,7 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.test.discriminator"
package="org.hibernate.orm.test.discriminator"
default-access="field">
<!--

View File

@ -4,45 +4,52 @@
* 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.discriminator;
package org.hibernate.orm.test.discriminator;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.proxy.HibernateProxy;
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.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author Gavin King
*/
public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
public void configure(Configuration cfg) {
super.configure( cfg );
}
@DomainModel(
xmlMappings = "org/hibernate/orm/test/discriminator/SimpleInheritance.hbm.xml"
)
@SessionFactory
public class SimpleInheritanceTest {
@Override
public String[] getMappings() {
return new String[] { "discriminator/SimpleInheritance.hbm.xml" };
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session ->
session.createQuery( "delete from Person" ).executeUpdate()
);
}
@Test
public void testDiscriminatorSubclass() {
inTransaction(
public void testDiscriminatorSubclass(SessionFactoryScope scope) {
scope.inTransaction(
s -> {
Employee mark = new Employee();
mark.setId( 1 );
@ -65,33 +72,41 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
s.save( mark );
s.save( joe );
assertEquals( s.createQuery( "from java.io.Serializable" ).list().size(), 0 );
try {
s.createQuery( "from java.io.Serializable" ).list();
fail( "Expected IllegalAccessException" );
}
catch (Exception e) {
assertThat( e, instanceOf( IllegalArgumentException.class ) );
}
assertEquals( s.createQuery( "from org.hibernate.test.discriminator.Person" ).list().size(), 3 );
assertEquals( s.createQuery(
"from org.hibernate.test.discriminator.Person p where p.class = org.hibernate.test.discriminator.Person" )
.list()
.size(), 1 );
assertEquals( s.createQuery(
"from org.hibernate.test.discriminator.Person p where p.class = org.hibernate.test.discriminator.Customer" )
.list()
.size(), 1 );
assertEquals( s.createQuery( "from org.hibernate.test.discriminator.Person p where type(p) = :who" )
.setParameter( "who", Person.class )
.list()
.size(), 1 );
assertEquals( s.createQuery( "from org.hibernate.test.discriminator.Person p where type(p) in :who" )
.setParameterList( "who", new Class[] { Customer.class, Person.class } )
.list()
.size(), 2 );
assertThat(
s.createQuery( "from org.hibernate.orm.test.discriminator.Person" ).list().size(),
is( 3 )
);
assertThat( s.createQuery(
"from org.hibernate.orm.test.discriminator.Person p where p.class = org.hibernate.orm.test.discriminator.Person" )
.list()
.size(), is( 1 ) );
assertThat( s.createQuery(
"from org.hibernate.orm.test.discriminator.Person p where p.class = org.hibernate.orm.test.discriminator.Customer" )
.list()
.size(), is( 1 ) );
assertThat( s.createQuery( "from org.hibernate.orm.test.discriminator.Person p where type(p) = :who" )
.setParameter( "who", Person.class )
.list()
.size(), is( 1 ) );
assertThat( s.createQuery( "from org.hibernate.orm.test.discriminator.Person p where type(p) in :who" )
.setParameterList( "who", new Class[] { Customer.class, Person.class } )
.list()
.size(), is( 2 ) );
s.clear();
List customers = s.createQuery( "from org.hibernate.test.discriminator.Customer" ).list();
for ( Object customer : customers ) {
Customer c = (Customer) customer;
assertEquals( "Very demanding", c.getComments() );
List<Customer> customers = s.createQuery( "from org.hibernate.orm.test.discriminator.Customer" ).list();
for ( Customer c : customers ) {
assertThat( c.getComments(), is( "Very demanding" ) );
}
assertEquals( customers.size(), 1 );
assertThat( customers.size(), is( 1 ) );
s.clear();
mark = s.get( Employee.class, mark.getId() );
@ -100,16 +115,16 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
s.delete( mark );
s.delete( joe );
s.delete( yomomma );
assertTrue( s.createQuery( "from org.hibernate.test.discriminator.Person" ).list().isEmpty() );
assertTrue( s.createQuery( "from org.hibernate.orm.test.discriminator.Person" ).list().isEmpty() );
}
);
}
@Test
public void testAccessAsIncorrectSubclass() {
public void testAccessAsIncorrectSubclass(SessionFactoryScope scope) {
Employee employee = new Employee();
inTransaction(
scope.inTransaction(
s -> {
employee.setId( 4 );
employee.setName( "Steve" );
@ -119,55 +134,31 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
}
);
Customer c = null;
Session s = openSession();
try {
s.beginTransaction();
Customer c = scope.fromTransaction(
s ->
s.get( Customer.class, employee.getId() )
c = s.get( Customer.class, employee.getId() );
s.getTransaction().commit();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
finally {
s.close();
);
}
assertNull( c );
Employee e = null;
s = openSession();
try {
s.beginTransaction();
scope.inTransaction(
s -> {
Employee e = s.get( Employee.class, employee.getId() );
Customer c1 = s.get( Customer.class, e.getId() );
assertNotNull( e );
assertNull( c1 );
}
);
e = s.get( Employee.class, employee.getId() );
c = s.get( Customer.class, e.getId() );
s.getTransaction().commit();
}
catch (Exception ex) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw ex;
}
finally {
s.close();
}
assertNotNull( e );
assertNull( c );
inTransaction(
scope.inTransaction(
session -> session.delete( employee )
);
}
@Test
public void testQuerySubclassAttribute() {
inTransaction(
public void testQuerySubclassAttribute(SessionFactoryScope scope) {
scope.inTransaction(
s -> {
Person p = new Person();
p.setId( 5 );
@ -182,15 +173,15 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
q.setSalary( new BigDecimal( 1000 ) );
s.save( q );
List result = s.createQuery( "from org.hibernate.test.discriminator.Person where salary > 100" )
List result = s.createQuery( "from org.hibernate.orm.test.discriminator.Person where salary > 100" )
.list();
assertEquals( result.size(), 1 );
assertThat( result.size(), is( 1 ) );
assertSame( result.get( 0 ), q );
result = s.createQuery(
"from org.hibernate.test.discriminator.Person where salary > 100 or name like 'E%'" )
"from org.hibernate.orm.test.discriminator.Person where salary > 100 or name like 'E%'" )
.list();
assertEquals( result.size(), 2 );
assertThat( result.size(), is( 2 ) );
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
@ -201,7 +192,7 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
// result = s.createCriteria( Person.class )
// .add( Property.forName( "salary" ).gt( new BigDecimal( 100 ) ) )
// .list();
assertEquals( result.size(), 1 );
assertThat( result.size(), is( 1 ) );
assertSame( result.get( 0 ), q );
//TODO: make this work:
@ -216,9 +207,9 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
}
@Test
public void testLoadSuperclassProxyPolymorphicAccess() {
public void testLoadSuperclassProxyPolymorphicAccess(SessionFactoryScope scope) {
Employee employee = new Employee();
inTransaction(
scope.inTransaction(
s -> {
employee.setId( 7 );
employee.setName( "Steve" );
@ -228,14 +219,14 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
}
);
inTransaction(
scope.inTransaction(
s -> {
// load the superclass proxy.
Person pLoad = s.load( Person.class, new Long( employee.getId() ) );
Person pLoad = s.load( Person.class, employee.getId() );
assertTrue( pLoad instanceof HibernateProxy );
Person pGet = s.get( Person.class, employee.getId() );
Person pQuery = (Person) s.createQuery(
"from org.hibernate.test.discriminator.Person where id = :id" )
"from org.hibernate.orm.test.discriminator.Person where id = :id" )
.setParameter( "id", employee.getId() )
.uniqueResult();
@ -250,23 +241,23 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
// .uniqueResult();
// assert that executing the queries polymorphically returns the same proxy
assertSame( pLoad, pGet );
assertSame( pLoad, pQuery );
assertSame( pLoad, pCriteria );
assertSame( pLoad, pQuery );
// assert that the proxy is not an instance of Employee
assertFalse( pLoad instanceof Employee );
}
);
inTransaction(
scope.inTransaction(
s -> s.delete( employee )
);
}
@Test
public void testLoadSuperclassProxyEvictPolymorphicAccess() {
public void testLoadSuperclassProxyEvictPolymorphicAccess(SessionFactoryScope scope) {
Employee employee = new Employee();
inTransaction(
scope.inTransaction(
s -> {
employee.setId( 8 );
employee.setName( "Steve" );
@ -277,16 +268,16 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
);
inTransaction(
scope.inTransaction(
s -> {
// load the superclass proxy.
Person pLoad = s.load( Person.class, new Long( employee.getId() ) );
Person pLoad = s.load( Person.class, employee.getId() );
assertTrue( pLoad instanceof HibernateProxy );
// evict the proxy
s.evict( pLoad );
Employee pGet = (Employee) s.get( Person.class, employee.getId() );
Employee pQuery = (Employee) s.createQuery(
"from org.hibernate.test.discriminator.Person where id = :id" )
"from org.hibernate.orm.test.discriminator.Person where id = :id" )
.setParameter( "id", employee.getId() )
.uniqueResult();
@ -305,7 +296,7 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
}
);
inTransaction(
scope.inTransaction(
s -> s.delete( employee )
);
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.discriminator.joined;
package org.hibernate.orm.test.discriminator.joined;
/**
* @author Chris Cranford

View File

@ -11,10 +11,10 @@
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="org.hibernate.test.discriminator.joined"
package="org.hibernate.orm.test.discriminator.joined"
default-access="field">
<class name="org.hibernate.test.discriminator.joined.ParentEntity" discriminator-value="pe">
<class name="org.hibernate.orm.test.discriminator.joined.ParentEntity" discriminator-value="pe">
<id name="id" type="integer">
<column name="id"/>
</id>
@ -24,7 +24,7 @@
<property name="type" insert="false" update="false" type="string"/>
</class>
<joined-subclass name="org.hibernate.test.discriminator.joined.ChildEntity" discriminator-value="ce" extends="org.hibernate.test.discriminator.joined.ParentEntity">
<joined-subclass name="org.hibernate.orm.test.discriminator.joined.ChildEntity" discriminator-value="ce" extends="org.hibernate.orm.test.discriminator.joined.ParentEntity">
<key>
<column name="id" />
</key>

View File

@ -0,0 +1,50 @@
/*
* 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.orm.test.discriminator.joined;
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.After;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-11133")
@DomainModel(
xmlMappings = "org/hibernate/orm/test/discriminator/joined/JoinedSubclassInheritance.hbm.xml"
)
@SessionFactory
public class JoinedSubclassInheritanceTest {
@After
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session ->
session.createQuery( "delete from ChildEntity" ).executeUpdate()
);
}
@Test
public void testConfiguredDiscriminatorValue(SessionFactoryScope scope) {
final ChildEntity childEntity = new ChildEntity( 1, "Child" );
scope.inTransaction( session ->
session.save( childEntity )
);
scope.inTransaction( session -> {
ChildEntity ce = session.find( ChildEntity.class, 1 );
assertThat( ce.getType(), is( "ce" ) );
} );
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.discriminator.joined;
package org.hibernate.orm.test.discriminator.joined;
/**
* @author Chris Cranford

View File

@ -1,291 +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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.discriminator;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.junit.Test;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gavin King
*/
public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] { "discriminator/Person.hbm.xml" };
}
@Test
public void testDiscriminatorSubclass() {
inTransaction(
s -> {
Employee mark = new Employee();
mark.setName("Mark");
mark.setTitle("internal sales");
mark.setSex('M');
mark.setAddress("buckhead");
mark.setZip("30305");
mark.setCountry("USA");
Customer joe = new Customer();
joe.setName("Joe");
joe.setAddress("San Francisco");
joe.setZip("XXXXX");
joe.setCountry("USA");
joe.setComments("Very demanding");
joe.setSex('M');
joe.setSalesperson(mark);
Person yomomma = new Person();
yomomma.setName("mum");
yomomma.setSex('F');
s.save(yomomma);
s.save(mark);
s.save(joe);
assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 );
assertEquals( s.createQuery("from Person").list().size(), 3 );
assertEquals( s.createQuery("from Person p where p.class = Person").list().size(), 1 );
assertEquals( s.createQuery("from Person p where p.class = Customer").list().size(), 1 );
s.clear();
List customers = s.createQuery("from Customer c left join fetch c.salesperson").list();
for ( Object customer : customers ) {
Customer c = (Customer) customer;
assertTrue( Hibernate.isInitialized( c.getSalesperson() ) );
assertEquals( c.getSalesperson().getName(), "Mark" );
}
assertEquals( customers.size(), 1 );
s.clear();
customers = s.createQuery("from Customer").list();
for ( Object customer : customers ) {
Customer c = (Customer) customer;
assertFalse( Hibernate.isInitialized( c.getSalesperson() ) );
assertEquals( c.getSalesperson().getName(), "Mark" );
}
assertEquals( customers.size(), 1 );
s.clear();
mark = s.get( Employee.class, new Long( mark.getId() ) );
joe = s.get( Customer.class, new Long( joe.getId() ) );
mark.setZip("30306");
assertEquals( s.createQuery("from Person p where p.address.zip = '30306'").list().size(), 1 );
s.delete(mark);
s.delete(joe);
s.delete(yomomma);
assertTrue( s.createQuery("from Person").list().isEmpty() );
}
);
}
@Test
public void testAccessAsIncorrectSubclass() {
Employee employee = new Employee();
inTransaction(
s -> {
employee.setName( "Steve" );
employee.setSex( 'M' );
employee.setTitle( "grand poobah" );
s.save( employee );
}
);
Customer c = null;
Session s = openSession();
try {
s.beginTransaction();
c = s.get( Customer.class, new Long( employee.getId() ) );
s.getTransaction().commit();
}catch (Exception exception){
if(s.getTransaction().isActive()){
s.getTransaction().rollback();
}
throw exception;
}finally {
s.close();
}
assertNull( c );
Employee e = null;
s = openSession();
try {
s.beginTransaction();
e = s.get( Employee.class, new Long( employee.getId() ) );
c = s.get( Customer.class, new Long( employee.getId() ) );
s.getTransaction().commit();
}catch (Exception exception){
if(s.getTransaction().isActive()){
s.getTransaction().rollback();
}
throw exception;
}finally{
s.close();
}
assertNotNull( e );
assertNull( c );
inTransaction(
session -> session.delete( employee )
);
}
@Test
public void testQuerySubclassAttribute() {
inTransaction(
s -> {
Person p = new Person();
p.setName("Emmanuel");
p.setSex('M');
s.persist(p);
Employee q = new Employee();
q.setName("Steve");
q.setSex('M');
q.setTitle("Mr");
q.setSalary( new BigDecimal(1000) );
s.persist(q);
List result = s.createQuery("from Person where salary > 100").list();
assertEquals( result.size(), 1 );
assertSame( result.get(0), q );
result = s.createQuery("from Person where salary > 100 or name like 'E%'").list();
assertEquals( result.size(), 2 );
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
criteria.where( criteriaBuilder.gt( root.get( "salary" ), new BigDecimal( 100) ) );
result = s.createQuery( criteria ).list();
// result = s.createCriteria(Person.class)
// .add( Property.forName( "salary").gt( new BigDecimal( 100) ) )
// .list();
assertEquals( result.size(), 1 );
assertSame( result.get(0), q );
//TODO: make this work:
/*result = s.createQuery("select salary from Person where salary > 100").list();
assertEquals( result.size(), 1 );
assertEquals( result.get(0), new BigDecimal(1000) );*/
s.delete(p);
s.delete(q);
}
);
}
@Test
public void testLoadSuperclassProxyPolymorphicAccess() {
Employee e = new Employee();
inTransaction(
s -> {
e.setName( "Steve" );
e.setSex( 'M' );
e.setTitle( "grand poobah" );
s.save( e );
}
);
inTransaction(
s -> {
// load the superclass proxy.
Person pLoad = s.load( Person.class, new Long( e.getId() ) );
assertTrue( pLoad instanceof HibernateProxy);
Person pGet = s.get( Person.class, new Long( e.getId() ));
Person pQuery = ( Person ) s.createQuery( "from Person where id = :id" )
.setParameter( "id", e.getId() )
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
criteria.where( criteriaBuilder.equal( root.get( "id" ), e.getId() ));
Person pCriteria = s.createQuery( criteria ).uniqueResult();
// Person pCriteria = ( Person ) s.createCriteria( Person.class )
// .add( Restrictions.idEq( new Long( e.getId() ) ) )
// .uniqueResult();
// assert that executing the queries polymorphically returns the same proxy
assertSame( pLoad, pGet );
assertSame( pLoad, pQuery );
assertSame( pLoad, pCriteria );
// assert that the proxy is not an instance of Employee
assertFalse( pLoad instanceof Employee );
}
);
inTransaction(
s -> s.delete( e )
);
}
@Test
public void testLoadSuperclassProxyEvictPolymorphicAccess() {
Employee e = new Employee();
inTransaction(
s -> {
e.setName( "Steve" );
e.setSex( 'M' );
e.setTitle( "grand poobah" );
s.save( e );
}
);
inTransaction(
s -> {
// load the superclass proxy.
Person pLoad = ( Person ) s.load( Person.class, new Long( e.getId() ) );
assertTrue( pLoad instanceof HibernateProxy);
// evict the proxy
s.evict( pLoad );
Employee pGet = ( Employee ) s.get( Person.class, new Long( e.getId() ));
Employee pQuery = ( Employee ) s.createQuery( "from Person where id = :id" )
.setParameter( "id", e.getId() )
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
criteria.where( criteriaBuilder.equal( root.get( "id" ), e.getId() ) );
Employee pCriteria = (Employee) s.createQuery( criteria ).uniqueResult();
// Employee pCriteria = ( Employee ) s.createCriteria( Person.class )
// .add( Restrictions.idEq( new Long( e.getId() ) ) )
// .uniqueResult();
// assert that executing the queries polymorphically returns the same Employee instance
assertSame( pGet, pQuery );
assertSame( pGet, pCriteria );
}
);
inTransaction(
s -> s.delete( e )
);
}
}

View File

@ -1,40 +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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.discriminator.joined;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-11133")
public class JoinedSubclassInheritanceTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
return new String[] {"discriminator/joined/JoinedSubclassInheritance.hbm.xml"};
}
@Test
public void testConfiguredDiscriminatorValue() {
final ChildEntity childEntity = new ChildEntity( 1, "Child" );
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
session.save( childEntity );
} );
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
ChildEntity ce = session.find( ChildEntity.class, 1 );
assertEquals( "ce", ce.getType() );
} );
}
}