6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-16 15:42:50 +01:00
parent 430a765888
commit d1b86c2a72
5 changed files with 919 additions and 814 deletions

View File

@ -25,6 +25,7 @@ import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.SkipLog;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -54,58 +55,56 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
@Test
public void testGetOneColumnSameNameAsArgFunctionHQL() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn();
e.setLower( 3 );
e.setUpper( " abc " );
s.persist( e );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
e = ( EntityWithArgFunctionAsColumn ) s.createQuery( "from EntityWithArgFunctionAsColumn" ).uniqueResult();
inTransaction(
s -> {
EntityWithArgFunctionAsColumn e = (EntityWithArgFunctionAsColumn) s.createQuery(
"from EntityWithArgFunctionAsColumn" ).uniqueResult();
assertEquals( 3, e.getLower() );
assertEquals( " abc ", e.getUpper() );
t.commit();
s.close();
cleanup();
}
);
}
@Test
public void testGetOneColumnSameNameAsArgFunctionCriteria() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn();
e.setLower( 3 );
e.setUpper( " abc " );
s.persist( e );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<EntityWithArgFunctionAsColumn> criteria = criteriaBuilder.createQuery( EntityWithArgFunctionAsColumn.class );
CriteriaQuery<EntityWithArgFunctionAsColumn> criteria = criteriaBuilder.createQuery(
EntityWithArgFunctionAsColumn.class );
criteria.from( EntityWithArgFunctionAsColumn.class );
e = s.createQuery( criteria ).uniqueResult();
EntityWithArgFunctionAsColumn e = s.createQuery( criteria ).uniqueResult();
// e = ( EntityWithArgFunctionAsColumn ) s.createCriteria( EntityWithArgFunctionAsColumn.class ).uniqueResult();
assertEquals( 3, e.getLower() );
assertEquals( " abc ", e.getUpper() );
t.commit();
s.close();
cleanup();
}
);
}
@Test
public void testGetMultiColumnSameNameAsArgFunctionHQL() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn();
e1.setLower( 3 );
e1.setUpper( " abc " );
@ -118,12 +117,12 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
holder2.getEntityWithArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 );
s.save( holder1 );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
holder1 = ( EntityWithFunctionAsColumnHolder ) s.createQuery(
inTransaction(
s -> {
EntityWithFunctionAsColumnHolder holder1 = (EntityWithFunctionAsColumnHolder) s.createQuery(
"from EntityWithFunctionAsColumnHolder h left join fetch h.entityWithArgFunctionAsColumns " +
"left join fetch h.nextHolder left join fetch h.nextHolder.entityWithArgFunctionAsColumns " +
"where h.nextHolder is not null" )
@ -132,23 +131,23 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) );
assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() );
e1 = ( EntityWithArgFunctionAsColumn ) holder1.getEntityWithArgFunctionAsColumns().iterator().next();
EntityWithArgFunctionAsColumn e1 = (EntityWithArgFunctionAsColumn) holder1.getEntityWithArgFunctionAsColumns().iterator().next();
assertEquals( 3, e1.getLower() );
assertEquals( " abc ", e1.getUpper() );
assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() );
e2 = ( EntityWithArgFunctionAsColumn ) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns().iterator().next();
EntityWithArgFunctionAsColumn e2 = (EntityWithArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns()
.iterator()
.next();
assertEquals( 999, e2.getLower() );
assertEquals( " xyz ", e2.getUpper() );
t.commit();
s.close();
cleanup();
}
);
}
@Test
public void testGetMultiColumnSameNameAsArgFunctionCriteria() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn();
e1.setLower( 3 );
e1.setUpper( " abc " );
@ -161,11 +160,11 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
holder2.getEntityWithArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 );
s.save( holder1 );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
EntityWithFunctionAsColumnHolder.class );
@ -175,7 +174,7 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT );
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
holder1 = s.createQuery( criteria ).uniqueResult();
EntityWithFunctionAsColumnHolder holder1 = s.createQuery( criteria ).uniqueResult();
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
// .add( Restrictions.isNotNull( "nextHolder" ))
@ -187,17 +186,17 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) );
assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() );
e1 = ( EntityWithArgFunctionAsColumn ) holder1.getEntityWithArgFunctionAsColumns().iterator().next();
EntityWithArgFunctionAsColumn e1 = (EntityWithArgFunctionAsColumn) holder1.getEntityWithArgFunctionAsColumns().iterator().next();
assertEquals( 3, e1.getLower() );
assertEquals( " abc ", e1.getUpper() );
assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() );
e2 = ( EntityWithArgFunctionAsColumn ) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns().iterator().next();
EntityWithArgFunctionAsColumn e2 = (EntityWithArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns()
.iterator()
.next();
assertEquals( 999, e2.getLower() );
assertEquals( " xyz ", e2.getUpper() );
t.commit();
s.close();
cleanup();
}
);
}
@Test
@ -208,8 +207,8 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
return;
}
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
e1.setCurrentDate( "blah blah blah" );
EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
@ -220,12 +219,12 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
holder2.getEntityWithNoArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 );
s.save( holder1 );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
holder1 = ( EntityWithFunctionAsColumnHolder ) s.createQuery(
Session s = openSession();
try {
EntityWithFunctionAsColumnHolder holder1 = (EntityWithFunctionAsColumnHolder) s.createQuery(
"from EntityWithFunctionAsColumnHolder h left join fetch h.entityWithNoArgFunctionAsColumns " +
"left join fetch h.nextHolder left join fetch h.nextHolder.entityWithNoArgFunctionAsColumns " +
"where h.nextHolder is not null" )
@ -234,16 +233,25 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) );
assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() );
t.commit();
s.getTransaction().commit();
s.close();
e1 = ( EntityWithNoArgFunctionAsColumn ) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next();
EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next();
assertEquals( "blah blah blah", e1.getCurrentDate() );
assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() );
e2 = ( EntityWithNoArgFunctionAsColumn ) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns().iterator().next();
EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns()
.iterator()
.next();
assertEquals( "yadda yadda yadda", e2.getCurrentDate() );
cleanup();
}catch (Exception e){
if (s.getTransaction().isActive()){
s.getTransaction().rollback();
}
}finally {
if(s.isOpen()) {
s.close();
}
}
}
@Test
@ -254,24 +262,24 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
return;
}
Session s = openSession();
Transaction t = s.beginTransaction();
EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
inTransaction(
s -> {
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
e1.setCurrentDate( "blah blah blah" );
EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
e2.setCurrentDate( "yadda yadda yadda" );
EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
holder1.getEntityWithNoArgFunctionAsColumns().add( e1 );
EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
holder2.getEntityWithNoArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 );
s.save( holder1 );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
}
);
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
EntityWithFunctionAsColumnHolder.class );
@ -289,17 +297,21 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
// .uniqueResult();
assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder()
.getEntityWithNoArgFunctionAsColumns() ) );
assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() );
e1 = ( EntityWithNoArgFunctionAsColumn ) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next();
EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns()
.iterator()
.next();
assertEquals( "blah blah blah", e1.getCurrentDate() );
assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() );
e2 = ( EntityWithNoArgFunctionAsColumn ) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns().iterator().next();
EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns()
.iterator()
.next();
assertEquals( "yadda yadda yadda", e2.getCurrentDate() );
t.commit();
s.close();
cleanup();
}
);
}
@Test
@ -310,11 +322,12 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
return;
}
Session s = openSession();
Transaction t = s.beginTransaction();
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
e1.setCurrentDate( "blah blah blah" );
EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
inTransaction(
s -> {
e1.setCurrentDate( "blah blah blah" );
e2.setCurrentDate( "yadda yadda yadda" );
EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
holder1.getEntityWithNoArgFunctionAsColumns().add( e1 );
@ -322,11 +335,11 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
holder2.getEntityWithNoArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 );
s.save( holder1 );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
inTransaction(
s -> {
List results = s.createQuery(
"select str(current_date), currentDate from EntityWithNoArgFunctionAsColumn"
).list();
@ -339,30 +352,38 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
assertTrue( ( (Object[]) results.get( 1 ) )[1].equals( e1.getCurrentDate() ) ||
( (Object[]) results.get( 1 ) )[1].equals( e2.getCurrentDate() ) );
assertFalse( ( (Object[]) results.get( 0 ) )[1].equals( ( (Object[]) results.get( 1 ) )[1] ) );
t.commit();
s.close();
cleanup();
}
);
}
private void cleanup() {
Session s = openSession();
Transaction t = s.beginTransaction();
@After
public void cleanup() {
SQLFunction function = sessionFactory().getSqlFunctionRegistry().findSQLFunction( "current_date" );
if ( function == null || function.hasParenthesesIfNoArguments() ) {
SkipLog.reportSkip( "current_date reuires ()", "tests noarg function that does not require ()" );
return;
}
inTransaction(
s -> {
s.createQuery( "delete from EntityWithArgFunctionAsColumn" ).executeUpdate();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
}
);
inTransaction(
s -> {
s.createQuery( "delete from EntityWithNoArgFunctionAsColumn" ).executeUpdate();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.createQuery( "delete from EntityWithFunctionAsColumnHolder where nextHolder is not null" ).executeUpdate();
}
);
inTransaction(
s -> {
s.createQuery( "delete from EntityWithFunctionAsColumnHolder where nextHolder is not null" )
.executeUpdate();
s.createQuery( "delete from EntityWithFunctionAsColumnHolder" ).executeUpdate();
t.commit();
s.close();
}
);
}
}

View File

@ -22,7 +22,6 @@ import org.hibernate.type.descriptor.sql.ClobTypeDescriptor;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Before;
import org.junit.Test;

View File

@ -8,11 +8,12 @@ package org.hibernate.test.subselect;
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.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
@ -30,8 +31,8 @@ public class SubselectTest extends BaseCoreFunctionalTestCase {
@Test
@SuppressWarnings({ "unchecked" })
public void testEntitySubselect() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Human gavin = new Human();
gavin.setName( "gavin" );
gavin.setSex( 'M' );
@ -51,27 +52,28 @@ public class SubselectTest extends BaseCoreFunctionalTestCase {
}
s.clear();
sessionFactory().getCache().evictEntityRegion( Being.class );
Being gav = (Being) s.get(Being.class, gavin.getId());
Being gav = s.get( Being.class, gavin.getId() );
assertEquals( gav.getLocation(), gavin.getAddress() );
assertEquals( gav.getSpecies(), "human" );
assertEquals( gav.getIdentity(), gavin.getName() );
s.clear();
//test the <synchronized> tag:
gavin = (Human) s.get(Human.class, gavin.getId());
gavin = s.get( Human.class, gavin.getId() );
gavin.setAddress( "Atlanta, GA" );
gav = (Being) s.createQuery( "from Being b where b.location like '%GA%'" ).uniqueResult();
assertEquals( gav.getLocation(), gavin.getAddress() );
s.delete( gavin );
s.delete( x23y4 );
assertTrue( s.createQuery( "from Being" ).list().isEmpty() );
t.commit();
s.close();
}
);
}
@Test
public void testCustomColumnReadAndWrite() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
final double HUMAN_INCHES = 73;
final double ALIEN_INCHES = 931;
final double HUMAN_CENTIMETERS = HUMAN_INCHES * 2.54d;
@ -94,35 +96,44 @@ public class SubselectTest extends BaseCoreFunctionalTestCase {
// Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType;
// Cast returned value to Number then call Number.doubleValue() so it works on all dialects.
Double humanHeightViaSql =
( (Number)s.createSQLQuery("select height_centimeters from humans").uniqueResult() ).doubleValue();
( (Number) s.createNativeQuery( "select height_centimeters from humans" )
.uniqueResult() ).doubleValue();
assertEquals( HUMAN_CENTIMETERS, humanHeightViaSql, 0.01d );
Double alienHeightViaSql =
( (Number)s.createSQLQuery("select height_centimeters from aliens").uniqueResult() ).doubleValue();
( (Number) s.createNativeQuery( "select height_centimeters from aliens" )
.uniqueResult() ).doubleValue();
assertEquals( ALIEN_CENTIMETERS, alienHeightViaSql, 0.01d );
s.clear();
// Test projection
Double heightViaHql = (Double)s.createQuery("select heightInches from Being b where b.identity = 'gavin'").uniqueResult();
Double heightViaHql = (Double) s.createQuery(
"select heightInches from Being b where b.identity = 'gavin'" ).uniqueResult();
assertEquals( HUMAN_INCHES, heightViaHql, 0.01d );
// Test restriction and entity load via criteria
Being b = (Being)s.createCriteria(Being.class)
.add(Restrictions.between("heightInches", HUMAN_INCHES - 0.01d, HUMAN_INCHES + 0.01d))
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Being> criteria = criteriaBuilder.createQuery( Being.class );
Root<Being> root = criteria.from( Being.class );
criteria.where( criteriaBuilder.between( root.get( "heightInches" ),HUMAN_INCHES - 0.01d, HUMAN_INCHES + 0.01d ) );
Being b = s.createQuery( criteria ).uniqueResult();
// Being b = (Being) s.createCriteria( Being.class )
// .add( Restrictions.between( "heightInches", HUMAN_INCHES - 0.01d, HUMAN_INCHES + 0.01d ) )
// .uniqueResult();
assertEquals( HUMAN_INCHES, b.getHeightInches(), 0.01d );
// Test predicate and entity load via HQL
b = (Being) s.createQuery( "from Being b where b.heightInches between ?1 and ?2" )
.setDouble(1, ALIEN_INCHES - 0.01d)
.setDouble(2, ALIEN_INCHES + 0.01d)
.setParameter( 1, ALIEN_INCHES - 0.01d )
.setParameter( 2, ALIEN_INCHES + 0.01d )
.uniqueResult();
assertEquals( ALIEN_INCHES, b.getHeightInches(), 0.01d );
s.delete( gavin );
s.delete( x23y4 );
t.commit();
s.close();
}
);
}
}

View File

@ -7,15 +7,15 @@
package org.hibernate.test.subselectfetch;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
@ -41,8 +41,8 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
@Test
public void testSubselectFetchHql() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Parent p = new Parent( "foo" );
p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child( "foo2" ) );
@ -52,18 +52,17 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
q.getMoreChildren().addAll( p.getChildren() );
s.persist( p );
s.persist( q );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
}
);
inTransaction(
s -> {
sessionFactory().getStatistics().clear();
List parents = s.createQuery( "from Parent where name between 'bar' and 'foo' order by name desc" )
.list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
@ -96,15 +95,14 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
s.delete( p );
s.delete( q );
t.commit();
s.close();
}
);
}
@Test
public void testSubselectFetchNamedParam() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Parent p = new Parent( "foo" );
p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child( "foo2" ) );
@ -114,20 +112,19 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
q.getMoreChildren().addAll( p.getChildren() );
s.persist( p );
s.persist( q );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
}
);
inTransaction(
s -> {
sessionFactory().getStatistics().clear();
List parents = s.createQuery( "from Parent where name between :bar and :foo order by name desc" )
.setParameter( "bar", "bar" )
.setParameter( "foo", "foo" )
.list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
@ -160,15 +157,14 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
s.delete( p );
s.delete( q );
t.commit();
s.close();
}
);
}
@Test
public void testSubselectFetchPosParam() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Parent p = new Parent( "foo" );
p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child( "foo2" ) );
@ -178,20 +174,19 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
q.getMoreChildren().addAll( p.getChildren() );
s.persist( p );
s.persist( q );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
}
);
inTransaction(
s -> {
sessionFactory().getStatistics().clear();
List parents = s.createQuery( "from Parent where name between ?1 and ?2 order by name desc" )
.setParameter( 1, "bar" )
.setParameter( 2, "foo" )
.list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
@ -224,39 +219,38 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
s.delete( p );
s.delete( q );
t.commit();
s.close();
}
);
}
@Test
public void testSubselectFetchWithLimit() {
Session s = openSession();
Transaction t = s.beginTransaction();
Parent p = new Parent("foo");
p.getChildren().add( new Child("foo1") );
p.getChildren().add( new Child("foo2") );
Parent q = new Parent("bar");
q.getChildren().add( new Child("bar1") );
q.getChildren().add( new Child("bar2") );
Parent r = new Parent("aaa");
r.getChildren().add( new Child("aaa1") );
s.persist(p);
s.persist(q);
s.persist(r);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Parent parent = new Parent( "foo" );
Parent secondParent = new Parent( "bar" );
Parent thirdParent = new Parent( "aaa" );
inTransaction(
s -> {
parent.getChildren().add( new Child( "foo1" ) );
parent.getChildren().add( new Child( "foo2" ) );
secondParent.getChildren().add( new Child( "bar1" ) );
secondParent.getChildren().add( new Child( "bar2" ) );
thirdParent.getChildren().add( new Child( "aaa1" ) );
s.persist( parent );
s.persist( secondParent );
s.persist( thirdParent );
}
);
inTransaction(
s -> {
sessionFactory().getStatistics().clear();
List parents = s.createQuery( "from Parent order by name desc" )
.setMaxResults( 2 )
.list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
@ -268,7 +262,7 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() );
r = (Parent) s.get( Parent.class, r.getName() );
Parent r = s.get( Parent.class, thirdParent.getName() );
assertTrue( Hibernate.isInitialized( r.getChildren() ) );
assertFalse( Hibernate.isInitialized( r.getMoreChildren() ) );
assertEquals( r.getChildren().size(), 1 );
@ -277,15 +271,14 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
s.delete( p );
s.delete( q );
s.delete( r );
t.commit();
s.close();
}
);
}
@Test
public void testManyToManyCriteriaJoin() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Parent p = new Parent( "foo" );
p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child( "foo2" ) );
@ -295,35 +288,57 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
q.getMoreChildren().addAll( p.getChildren() );
s.persist( p );
s.persist( q );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Parent> criteria = criteriaBuilder.createQuery( Parent.class );
Root<Parent> root = criteria.from( Parent.class );
Join<Object, Object> friends = root.join( "moreChildren", JoinType.INNER )
.join( "friends", JoinType.INNER );
criteria.orderBy( criteriaBuilder.desc( friends.get( "name" ) ) );
List parents = s.createCriteria(Parent.class)
.createCriteria("moreChildren")
.createCriteria("friends")
.addOrder( Order.desc("name") )
.list();
s.createQuery( criteria ).list();
parents = s.createCriteria(Parent.class)
.setFetchMode("moreChildren", FetchMode.JOIN)
.setFetchMode("moreChildren.friends", FetchMode.JOIN)
.addOrder( Order.desc("name") )
.list();
criteria = criteriaBuilder.createQuery( Parent.class );
root = criteria.from( Parent.class );
Join<Object, Object> moreChildren = root.join( "moreChildren", JoinType.LEFT );
friends = moreChildren.join( "friends", JoinType.LEFT );
root.fetch( "moreChildren", JoinType.LEFT ).fetch(
"friends",
JoinType.LEFT
);
criteria.orderBy( criteriaBuilder.desc( friends.get( "name" ) ) );
List parents = s.createQuery( criteria ).list();
// List parents = s.createCriteria( Parent.class )
// .createCriteria( "moreChildren" )
// .createCriteria( "friends" )
// .addOrder( Order.desc( "name" ) )
// .list();
//
// parents = s.createCriteria( Parent.class )
// .setFetchMode( "moreChildren", FetchMode.JOIN )
// .setFetchMode( "moreChildren.friends", FetchMode.JOIN )
// .addOrder( Order.desc( "name" ) )
// .list();
s.delete( parents.get( 0 ) );
s.delete( parents.get( 1 ) );
t.commit();
s.close();
}
);
}
@Test
public void testSubselectFetchCriteria() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Parent p = new Parent( "foo" );
p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child( "foo2" ) );
@ -333,20 +348,27 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
q.getMoreChildren().addAll( p.getChildren() );
s.persist( p );
s.persist( q );
t.commit();
s.close();
}
);
s = openSession();
t = s.beginTransaction();
inTransaction(
s -> {
sessionFactory().getStatistics().clear();
List parents = s.createCriteria(Parent.class)
.add( Property.forName("name").between("bar", "foo") )
.addOrder( Order.desc("name") )
.list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Parent> criteria = criteriaBuilder.createQuery( Parent.class );
Root<Parent> root = criteria.from( Parent.class );
criteria.where( criteriaBuilder.between( root.get( "name" ), "bar", "foo" ) );
criteria.orderBy( criteriaBuilder.desc( root.get( "name" ) ) );
List parents = s.createQuery( criteria ).list();
// List parents = s.createCriteria( Parent.class )
// .add( Property.forName( "name" ).between( "bar", "foo" ) )
// .addOrder( Order.desc( "name" ) )
// .list();
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
@ -379,9 +401,9 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
s.delete( p );
s.delete( q );
}
);
t.commit();
s.close();
}
}

View File

@ -8,12 +8,11 @@ package org.hibernate.test.unionsubclass2;
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.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.TeradataDialect;
@ -41,9 +40,9 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
comment = "SQL uses Teradata reserved word: title"
)
public void testUnionSubclass() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Employee mark = new Employee();
mark.setName( "Mark" );
mark.setTitle( "internal sales" );
@ -74,8 +73,17 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
assertEquals( s.createQuery( "from Person" ).list().size(), 3 );
assertEquals( s.createQuery( "from Person p where p.class = Customer" ).list().size(), 1 );
assertEquals( s.createQuery( "from Person p where p.class = Person" ).list().size(), 1 );
assertEquals( s.createQuery("from Person p where type(p) in :who").setParameter("who", Customer.class).list().size(), 1 );
assertEquals( s.createQuery("from Person p where type(p) in :who").setParameterList("who", new Class[] {Customer.class, Person.class}).list().size(), 2 );
assertEquals( s.createQuery( "from Person p where type(p) in :who" ).setParameter(
"who",
Customer.class
).list().size(), 1 );
assertEquals( s.createQuery( "from Person p where type(p) in :who" ).setParameterList(
"who",
new Class[] {
Customer.class,
Person.class
}
).list().size(), 2 );
s.clear();
List customers = s.createQuery( "from Customer c left join fetch c.salesperson" ).list();
@ -97,22 +105,31 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
s.clear();
mark = (Employee) s.get( Employee.class, Long.valueOf( mark.getId() ) );
joe = (Customer) s.get( Customer.class, Long.valueOf( joe.getId() ) );
mark = s.get( Employee.class, Long.valueOf( mark.getId() ) );
joe = s.get( Customer.class, Long.valueOf( joe.getId() ) );
mark.setZip( "30306" );
assertEquals( s.createQuery( "from Person p where p.address.zip = '30306'" ).list().size(), 1 );
s.createCriteria( Person.class ).add(
Restrictions.in( "address", new Address[] { mark.getAddress(),
joe.getAddress() } ) ).list();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
CriteriaBuilder.In<Object> addresses = criteriaBuilder.in( root.get( "address" ) );
addresses.value( new Address[] { mark.getAddress(), joe.getAddress() } );
criteria.where( addresses );
s.createQuery( criteria ).list();
// s.createCriteria( Person.class ).add(
// Restrictions.in( "address", new Address[] { mark.getAddress(), joe.getAddress()} ) ).list();
s.delete( mark );
s.delete( joe );
s.delete( yomomma );
assertTrue( s.createQuery( "from Person" ).list().isEmpty() );
t.commit();
s.close();
}
);
}
@Test
@ -126,8 +143,8 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
return; // TODO : why??
}
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Person p = new Person();
p.setName( "Emmanuel" );
p.setSex( 'M' );
@ -146,9 +163,16 @@ public class UnionSubclassTest 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 );
@ -158,8 +182,8 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
s.delete( p );
s.delete( q );
t.commit();
s.close();
}
);
}
@Test
@ -169,8 +193,8 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
comment = "SQL uses Teradata reserved word: title"
)
public void testCustomColumnReadAndWrite() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
final double HEIGHT_INCHES = 73;
final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
Person p = new Person();
@ -192,40 +216,70 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
// Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType;
// Cast returned value to Number then call Number.doubleValue() so it works on all dialects.
Double heightViaSql =
( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() ).doubleValue();
( (Number) s.createNativeQuery(
"select height_centimeters from UPerson where name='Emmanuel'" )
.uniqueResult() ).doubleValue();
assertEquals( HEIGHT_CENTIMETERS, heightViaSql, 0.01d );
Double expiryViaSql =
( (Number)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?")
.setLong(1, e.getId())
( (Number) s.createNativeQuery( "select pwd_expiry_weeks from UEmployee where person_id=?" )
.setParameter( 1, e.getId() )
.uniqueResult()
).doubleValue();
assertEquals( PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d );
// Test projection
Double heightViaHql = (Double)s.createQuery("select p.heightInches from Person p where p.name = 'Emmanuel'").uniqueResult();
Double heightViaHql = (Double) s.createQuery(
"select p.heightInches from Person p where p.name = 'Emmanuel'" )
.uniqueResult();
assertEquals( HEIGHT_INCHES, heightViaHql, 0.01d );
Double expiryViaHql = (Double)s.createQuery("select e.passwordExpiryDays from Employee e where e.name = 'Steve'").uniqueResult();
Double expiryViaHql = (Double) s.createQuery(
"select e.passwordExpiryDays from Employee e where e.name = 'Steve'" ).uniqueResult();
assertEquals( PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d );
// Test restriction and entity load via criteria
p = (Person)s.createCriteria(Person.class)
.add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d))
.uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> personCriteria = criteriaBuilder.createQuery( Person.class );
Root<Person> personRoot = personCriteria.from( Person.class );
personCriteria.where( criteriaBuilder.between(
personRoot.get( "heightInches" ),
HEIGHT_INCHES - 0.01d,
HEIGHT_INCHES + 0.01d
) );
p = s.createQuery( personCriteria ).uniqueResult();
// p = (Person) s.createCriteria( Person.class )
// .add( Restrictions.between( "heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d ) )
// .uniqueResult();
assertEquals( HEIGHT_INCHES, p.getHeightInches(), 0.01d );
e = (Employee)s.createCriteria(Employee.class)
.add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d))
.uniqueResult();
CriteriaQuery<Employee> employeeCriteria = criteriaBuilder.createQuery( Employee.class );
Root<Employee> employeeRoot = employeeCriteria.from( Employee.class );
employeeCriteria.where( criteriaBuilder.between(
employeeRoot.get( "passwordExpiryDays" ),
PASSWORD_EXPIRY_DAYS - 0.01d,
PASSWORD_EXPIRY_DAYS - 0.01d
) );
e = s.createQuery( employeeCriteria ).uniqueResult();
// e = (Employee) s.createCriteria( Employee.class )
// .add( Restrictions.between(
// "passwordExpiryDays",
// PASSWORD_EXPIRY_DAYS - 0.01d,
// PASSWORD_EXPIRY_DAYS - 0.01d,
// ) )
// .uniqueResult();
assertEquals( PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d );
// Test predicate and entity load via HQL
p = (Person) s.createQuery( "from Person p where p.heightInches between ?1 and ?2" )
.setDouble(1, HEIGHT_INCHES - 0.01d)
.setDouble(2, HEIGHT_INCHES + 0.01d)
.setParameter( 1, HEIGHT_INCHES - 0.01d )
.setParameter( 2, HEIGHT_INCHES + 0.01d )
.uniqueResult();
assertEquals( HEIGHT_INCHES, p.getHeightInches(), 0.01d );
e = (Employee) s.createQuery( "from Employee e where e.passwordExpiryDays between ?1 and ?2" )
.setDouble(1, PASSWORD_EXPIRY_DAYS - 0.01d)
.setDouble(2, PASSWORD_EXPIRY_DAYS + 0.01d)
.setParameter( 1, PASSWORD_EXPIRY_DAYS - 0.01d )
.setParameter( 2, PASSWORD_EXPIRY_DAYS + 0.01d )
.uniqueResult();
assertEquals( PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d );
@ -234,22 +288,20 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
e.setPasswordExpiryDays( 7 );
s.flush();
heightViaSql =
( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() )
( (Number) s.createNativeQuery(
"select height_centimeters from UPerson where name='Emmanuel'" )
.uniqueResult() )
.doubleValue();
assertEquals( 2.54d, heightViaSql, 0.01d );
expiryViaSql =
( (Number)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?")
.setLong(1, e.getId())
( (Number) s.createNativeQuery( "select pwd_expiry_weeks from UEmployee where person_id=?" )
.setParameter( 1, e.getId() )
.uniqueResult()
).doubleValue();
assertEquals( 1d, expiryViaSql, 0.01d );
s.delete( p );
s.delete( e );
t.commit();
s.close();
}
);
}
}