Fix use of proxy as entity instance in AbstractEntityInitializer
This commit is contained in:
parent
fcee504c99
commit
911173d6eb
|
@ -465,10 +465,10 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
|
||||||
|
|
||||||
final PersistenceContext persistenceContext = session.getPersistenceContext();
|
final PersistenceContext persistenceContext = session.getPersistenceContext();
|
||||||
final Object proxy = getProxy( persistenceContext );
|
final Object proxy = getProxy( persistenceContext );
|
||||||
|
|
||||||
// Special case map proxy to avoid stack overflows
|
// 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
|
// 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;
|
entityInstance = proxy;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//$Id: Address.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
//$Id: Address.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
||||||
package org.hibernate.test.discriminator;
|
package org.hibernate.orm.test.discriminator;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//$Id: Customer.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
//$Id: Customer.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
||||||
package org.hibernate.test.discriminator;
|
package org.hibernate.orm.test.discriminator;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -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 )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//$Id: Employee.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
//$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;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//$Id: Employee.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
//$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;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
|
@ -18,7 +18,7 @@
|
||||||
-->
|
-->
|
||||||
|
|
||||||
<hibernate-mapping
|
<hibernate-mapping
|
||||||
package="org.hibernate.test.discriminator"
|
package="org.hibernate.orm.test.discriminator"
|
||||||
default-access="field">
|
default-access="field">
|
||||||
|
|
||||||
<class name="Person"
|
<class name="Person"
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//$Id: Person.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
//$Id: Person.java 4373 2004-08-18 09:18:34Z oneovthafew $
|
||||||
package org.hibernate.test.discriminator;
|
package org.hibernate.orm.test.discriminator;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||||
|
|
||||||
<hibernate-mapping
|
<hibernate-mapping
|
||||||
package="org.hibernate.test.discriminator"
|
package="org.hibernate.orm.test.discriminator"
|
||||||
default-access="field">
|
default-access="field">
|
||||||
|
|
||||||
<!--
|
<!--
|
|
@ -4,45 +4,52 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* 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.math.BigDecimal;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.persistence.criteria.CriteriaBuilder;
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.hibernate.cfg.Configuration;
|
|
||||||
import org.hibernate.proxy.HibernateProxy;
|
import org.hibernate.proxy.HibernateProxy;
|
||||||
|
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.orm.junit.DomainModel;
|
||||||
import org.junit.Test;
|
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.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.Assert.assertSame;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
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
|
* @author Gavin King
|
||||||
*/
|
*/
|
||||||
public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
@DomainModel(
|
||||||
public void configure(Configuration cfg) {
|
xmlMappings = "org/hibernate/orm/test/discriminator/SimpleInheritance.hbm.xml"
|
||||||
super.configure( cfg );
|
)
|
||||||
}
|
@SessionFactory
|
||||||
|
public class SimpleInheritanceTest {
|
||||||
|
|
||||||
@Override
|
@AfterEach
|
||||||
public String[] getMappings() {
|
public void tearDown(SessionFactoryScope scope) {
|
||||||
return new String[] { "discriminator/SimpleInheritance.hbm.xml" };
|
scope.inTransaction(
|
||||||
|
session ->
|
||||||
|
session.createQuery( "delete from Person" ).executeUpdate()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDiscriminatorSubclass() {
|
public void testDiscriminatorSubclass(SessionFactoryScope scope) {
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
Employee mark = new Employee();
|
Employee mark = new Employee();
|
||||||
mark.setId( 1 );
|
mark.setId( 1 );
|
||||||
|
@ -65,33 +72,41 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
s.save( mark );
|
s.save( mark );
|
||||||
s.save( joe );
|
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 );
|
assertThat(
|
||||||
assertEquals( s.createQuery(
|
s.createQuery( "from org.hibernate.orm.test.discriminator.Person" ).list().size(),
|
||||||
"from org.hibernate.test.discriminator.Person p where p.class = org.hibernate.test.discriminator.Person" )
|
is( 3 )
|
||||||
.list()
|
);
|
||||||
.size(), 1 );
|
assertThat( s.createQuery(
|
||||||
assertEquals( s.createQuery(
|
"from org.hibernate.orm.test.discriminator.Person p where p.class = org.hibernate.orm.test.discriminator.Person" )
|
||||||
"from org.hibernate.test.discriminator.Person p where p.class = org.hibernate.test.discriminator.Customer" )
|
.list()
|
||||||
.list()
|
.size(), is( 1 ) );
|
||||||
.size(), 1 );
|
assertThat( s.createQuery(
|
||||||
assertEquals( s.createQuery( "from org.hibernate.test.discriminator.Person p where type(p) = :who" )
|
"from org.hibernate.orm.test.discriminator.Person p where p.class = org.hibernate.orm.test.discriminator.Customer" )
|
||||||
.setParameter( "who", Person.class )
|
.list()
|
||||||
.list()
|
.size(), is( 1 ) );
|
||||||
.size(), 1 );
|
assertThat( s.createQuery( "from org.hibernate.orm.test.discriminator.Person p where type(p) = :who" )
|
||||||
assertEquals( s.createQuery( "from org.hibernate.test.discriminator.Person p where type(p) in :who" )
|
.setParameter( "who", Person.class )
|
||||||
.setParameterList( "who", new Class[] { Customer.class, Person.class } )
|
.list()
|
||||||
.list()
|
.size(), is( 1 ) );
|
||||||
.size(), 2 );
|
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();
|
s.clear();
|
||||||
|
|
||||||
List customers = s.createQuery( "from org.hibernate.test.discriminator.Customer" ).list();
|
List<Customer> customers = s.createQuery( "from org.hibernate.orm.test.discriminator.Customer" ).list();
|
||||||
for ( Object customer : customers ) {
|
for ( Customer c : customers ) {
|
||||||
Customer c = (Customer) customer;
|
assertThat( c.getComments(), is( "Very demanding" ) );
|
||||||
assertEquals( "Very demanding", c.getComments() );
|
|
||||||
}
|
}
|
||||||
assertEquals( customers.size(), 1 );
|
assertThat( customers.size(), is( 1 ) );
|
||||||
s.clear();
|
s.clear();
|
||||||
|
|
||||||
mark = s.get( Employee.class, mark.getId() );
|
mark = s.get( Employee.class, mark.getId() );
|
||||||
|
@ -100,16 +115,16 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
s.delete( mark );
|
s.delete( mark );
|
||||||
s.delete( joe );
|
s.delete( joe );
|
||||||
s.delete( yomomma );
|
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
|
@Test
|
||||||
public void testAccessAsIncorrectSubclass() {
|
public void testAccessAsIncorrectSubclass(SessionFactoryScope scope) {
|
||||||
Employee employee = new Employee();
|
Employee employee = new Employee();
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
employee.setId( 4 );
|
employee.setId( 4 );
|
||||||
employee.setName( "Steve" );
|
employee.setName( "Steve" );
|
||||||
|
@ -119,55 +134,31 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
Customer c = null;
|
Customer c = scope.fromTransaction(
|
||||||
Session s = openSession();
|
s ->
|
||||||
try {
|
s.get( Customer.class, employee.getId() )
|
||||||
s.beginTransaction();
|
|
||||||
|
|
||||||
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 );
|
assertNull( c );
|
||||||
|
|
||||||
Employee e = null;
|
scope.inTransaction(
|
||||||
s = openSession();
|
s -> {
|
||||||
try {
|
Employee e = s.get( Employee.class, employee.getId() );
|
||||||
s.beginTransaction();
|
Customer c1 = s.get( Customer.class, e.getId() );
|
||||||
|
assertNotNull( e );
|
||||||
|
assertNull( c1 );
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
e = s.get( Employee.class, employee.getId() );
|
scope.inTransaction(
|
||||||
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(
|
|
||||||
session -> session.delete( employee )
|
session -> session.delete( employee )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testQuerySubclassAttribute() {
|
public void testQuerySubclassAttribute(SessionFactoryScope scope) {
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
p.setId( 5 );
|
p.setId( 5 );
|
||||||
|
@ -182,15 +173,15 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
q.setSalary( new BigDecimal( 1000 ) );
|
q.setSalary( new BigDecimal( 1000 ) );
|
||||||
s.save( q );
|
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();
|
.list();
|
||||||
assertEquals( result.size(), 1 );
|
assertThat( result.size(), is( 1 ) );
|
||||||
assertSame( result.get( 0 ), q );
|
assertSame( result.get( 0 ), q );
|
||||||
|
|
||||||
result = s.createQuery(
|
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();
|
.list();
|
||||||
assertEquals( result.size(), 2 );
|
assertThat( result.size(), is( 2 ) );
|
||||||
|
|
||||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||||
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
|
||||||
|
@ -201,7 +192,7 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
// result = s.createCriteria( Person.class )
|
// result = s.createCriteria( Person.class )
|
||||||
// .add( Property.forName( "salary" ).gt( new BigDecimal( 100 ) ) )
|
// .add( Property.forName( "salary" ).gt( new BigDecimal( 100 ) ) )
|
||||||
// .list();
|
// .list();
|
||||||
assertEquals( result.size(), 1 );
|
assertThat( result.size(), is( 1 ) );
|
||||||
assertSame( result.get( 0 ), q );
|
assertSame( result.get( 0 ), q );
|
||||||
|
|
||||||
//TODO: make this work:
|
//TODO: make this work:
|
||||||
|
@ -216,9 +207,9 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadSuperclassProxyPolymorphicAccess() {
|
public void testLoadSuperclassProxyPolymorphicAccess(SessionFactoryScope scope) {
|
||||||
Employee employee = new Employee();
|
Employee employee = new Employee();
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
employee.setId( 7 );
|
employee.setId( 7 );
|
||||||
employee.setName( "Steve" );
|
employee.setName( "Steve" );
|
||||||
|
@ -228,14 +219,14 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
// load the superclass proxy.
|
// 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 );
|
assertTrue( pLoad instanceof HibernateProxy );
|
||||||
Person pGet = s.get( Person.class, employee.getId() );
|
Person pGet = s.get( Person.class, employee.getId() );
|
||||||
Person pQuery = (Person) s.createQuery(
|
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() )
|
.setParameter( "id", employee.getId() )
|
||||||
.uniqueResult();
|
.uniqueResult();
|
||||||
|
|
||||||
|
@ -250,23 +241,23 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
// .uniqueResult();
|
// .uniqueResult();
|
||||||
// assert that executing the queries polymorphically returns the same proxy
|
// assert that executing the queries polymorphically returns the same proxy
|
||||||
assertSame( pLoad, pGet );
|
assertSame( pLoad, pGet );
|
||||||
assertSame( pLoad, pQuery );
|
|
||||||
assertSame( pLoad, pCriteria );
|
assertSame( pLoad, pCriteria );
|
||||||
|
assertSame( pLoad, pQuery );
|
||||||
|
|
||||||
// assert that the proxy is not an instance of Employee
|
// assert that the proxy is not an instance of Employee
|
||||||
assertFalse( pLoad instanceof Employee );
|
assertFalse( pLoad instanceof Employee );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> s.delete( employee )
|
s -> s.delete( employee )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadSuperclassProxyEvictPolymorphicAccess() {
|
public void testLoadSuperclassProxyEvictPolymorphicAccess(SessionFactoryScope scope) {
|
||||||
Employee employee = new Employee();
|
Employee employee = new Employee();
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
employee.setId( 8 );
|
employee.setId( 8 );
|
||||||
employee.setName( "Steve" );
|
employee.setName( "Steve" );
|
||||||
|
@ -277,16 +268,16 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
// load the superclass proxy.
|
// 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 );
|
assertTrue( pLoad instanceof HibernateProxy );
|
||||||
// evict the proxy
|
// evict the proxy
|
||||||
s.evict( pLoad );
|
s.evict( pLoad );
|
||||||
Employee pGet = (Employee) s.get( Person.class, employee.getId() );
|
Employee pGet = (Employee) s.get( Person.class, employee.getId() );
|
||||||
Employee pQuery = (Employee) s.createQuery(
|
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() )
|
.setParameter( "id", employee.getId() )
|
||||||
.uniqueResult();
|
.uniqueResult();
|
||||||
|
|
||||||
|
@ -305,7 +296,7 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
inTransaction(
|
scope.inTransaction(
|
||||||
s -> s.delete( employee )
|
s -> s.delete( employee )
|
||||||
);
|
);
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* 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
|
* @author Chris Cranford
|
|
@ -11,10 +11,10 @@
|
||||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||||
|
|
||||||
<hibernate-mapping
|
<hibernate-mapping
|
||||||
package="org.hibernate.test.discriminator.joined"
|
package="org.hibernate.orm.test.discriminator.joined"
|
||||||
default-access="field">
|
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">
|
<id name="id" type="integer">
|
||||||
<column name="id"/>
|
<column name="id"/>
|
||||||
</id>
|
</id>
|
||||||
|
@ -24,7 +24,7 @@
|
||||||
<property name="type" insert="false" update="false" type="string"/>
|
<property name="type" insert="false" update="false" type="string"/>
|
||||||
</class>
|
</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>
|
<key>
|
||||||
<column name="id" />
|
<column name="id" />
|
||||||
</key>
|
</key>
|
|
@ -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" ) );
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* 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
|
* @author Chris Cranford
|
|
@ -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 )
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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() );
|
|
||||||
} );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue