6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-16 17:55:56 +01:00
parent 86ccb2115b
commit 0a95c8c815
1 changed files with 203 additions and 174 deletions

View File

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