6 - SQM based on JPA type system
This commit is contained in:
parent
86ccb2115b
commit
0a95c8c815
|
@ -8,13 +8,15 @@ 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.criterion.Property;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.proxy.HibernateProxy;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
|
@ -36,9 +38,8 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testDiscriminatorSubclass() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
Employee mark = new Employee();
|
||||
mark.setName("Mark");
|
||||
mark.setTitle("internal sales");
|
||||
|
@ -90,8 +91,8 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
s.clear();
|
||||
|
||||
|
||||
mark = (Employee) s.get( Employee.class, new Long( mark.getId() ) );
|
||||
joe = (Customer) s.get( Customer.class, new Long( joe.getId() ) );
|
||||
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 );
|
||||
|
@ -99,49 +100,65 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
s.delete(joe);
|
||||
s.delete(yomomma);
|
||||
assertTrue( s.createQuery("from Person").list().isEmpty() );
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccessAsIncorrectSubclass() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Employee e = new Employee();
|
||||
e.setName( "Steve" );
|
||||
e.setSex( 'M' );
|
||||
e.setTitle( "grand poobah" );
|
||||
s.save( e );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
Employee employee = new Employee();
|
||||
inTransaction(
|
||||
s -> {
|
||||
employee.setName( "Steve" );
|
||||
employee.setSex( 'M' );
|
||||
employee.setTitle( "grand poobah" );
|
||||
s.save( employee );
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
Customer c = null;
|
||||
Session s = openSession();
|
||||
try {
|
||||
s.beginTransaction();
|
||||
Customer c = ( Customer ) s.get( Customer.class, new Long( e.getId() ) );
|
||||
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 );
|
||||
|
||||
Employee e = null;
|
||||
s = openSession();
|
||||
try {
|
||||
s.beginTransaction();
|
||||
e = ( Employee ) s.get( Employee.class, new Long( e.getId() ) );
|
||||
c = ( Customer ) s.get( Customer.class, new Long( e.getId() ) );
|
||||
e = s.get( Employee.class, new Long( employee.getId() ) );
|
||||
c = s.get( Customer.class, new Long( employee.getId() ) );
|
||||
s.getTransaction().commit();
|
||||
}catch (Exception exc){
|
||||
if(s.getTransaction().isActive()){
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
}finally{
|
||||
s.close();
|
||||
}
|
||||
|
||||
assertNotNull( e );
|
||||
assertNull( c );
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
s.delete( e );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
session -> session.delete( employee )
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuerySubclassAttribute() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Person p = new Person();
|
||||
p.setName("Emmanuel");
|
||||
p.setSex('M');
|
||||
|
@ -160,9 +177,14 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
result = s.createQuery("from Person where salary > 100 or name like 'E%'").list();
|
||||
assertEquals( result.size(), 2 );
|
||||
|
||||
result = s.createCriteria(Person.class)
|
||||
.add( Property.forName("salary").gt( new BigDecimal(100) ) )
|
||||
.list();
|
||||
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 );
|
||||
|
||||
|
@ -173,34 +195,40 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
s.delete(p);
|
||||
s.delete(q);
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadSuperclassProxyPolymorphicAccess() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Employee e = new Employee();
|
||||
inTransaction(
|
||||
s -> {
|
||||
e.setName( "Steve" );
|
||||
e.setSex( 'M' );
|
||||
e.setTitle( "grand poobah" );
|
||||
s.save( e );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
inTransaction(
|
||||
s -> {
|
||||
// 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);
|
||||
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" )
|
||||
.setLong( "id", e.getId() )
|
||||
.uniqueResult();
|
||||
Person pCriteria = ( Person ) s.createCriteria( Person.class )
|
||||
.add( Restrictions.idEq( new Long( e.getId() ) ) )
|
||||
.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 );
|
||||
|
@ -208,31 +236,28 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
// assert that the proxy is not an instance of Employee
|
||||
assertFalse( pLoad instanceof Employee );
|
||||
}
|
||||
);
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
s.delete( e );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> s.delete( e )
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadSuperclassProxyEvictPolymorphicAccess() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Employee e = new Employee();
|
||||
inTransaction(
|
||||
s -> {
|
||||
e.setName( "Steve" );
|
||||
e.setSex( 'M' );
|
||||
e.setTitle( "grand poobah" );
|
||||
s.save( e );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
inTransaction(
|
||||
s -> {
|
||||
// load the superclass proxy.
|
||||
Person pLoad = ( Person ) s.load( Person.class, new Long( e.getId() ) );
|
||||
assertTrue( pLoad instanceof HibernateProxy);
|
||||
|
@ -240,21 +265,25 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
s.evict( pLoad );
|
||||
Employee pGet = ( Employee ) s.get( Person.class, new Long( e.getId() ));
|
||||
Employee pQuery = ( Employee ) s.createQuery( "from Person where id = :id" )
|
||||
.setLong( "id", e.getId() )
|
||||
.uniqueResult();
|
||||
Employee pCriteria = ( Employee ) s.createCriteria( Person.class )
|
||||
.add( Restrictions.idEq( new Long( e.getId() ) ) )
|
||||
.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 );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
s.delete( e );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> s.delete( e )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue