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.SkipForDialect;
import org.hibernate.testing.SkipLog; import org.hibernate.testing.SkipLog;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -36,9 +37,9 @@ import static org.junit.Assert.assertTrue;
* *
* @author Gail Badner * @author Gail Badner
*/ */
@SkipForDialect( value = SybaseASE15Dialect.class, jiraKey = "HHH-6426") @SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-6426")
@SkipForDialect( value = PostgresPlusDialect.class, comment = "Almost all of the tests result in 'ambiguous column' errors.") @SkipForDialect(value = PostgresPlusDialect.class, comment = "Almost all of the tests result in 'ambiguous column' errors.")
public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
@Override @Override
public String[] getMappings() { public String[] getMappings() {
return new String[] { return new String[] {
@ -54,128 +55,126 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testGetOneColumnSameNameAsArgFunctionHQL() { public void testGetOneColumnSameNameAsArgFunctionHQL() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn(); EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn();
e.setLower( 3 ); e.setLower( 3 );
e.setUpper( " abc " ); e.setUpper( " abc " );
s.persist( e ); s.persist( e );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
e = ( EntityWithArgFunctionAsColumn ) s.createQuery( "from EntityWithArgFunctionAsColumn" ).uniqueResult(); EntityWithArgFunctionAsColumn e = (EntityWithArgFunctionAsColumn) s.createQuery(
assertEquals( 3, e.getLower() ); "from EntityWithArgFunctionAsColumn" ).uniqueResult();
assertEquals( " abc ", e.getUpper() ); assertEquals( 3, e.getLower() );
t.commit(); assertEquals( " abc ", e.getUpper() );
s.close(); }
);
cleanup();
} }
@Test @Test
public void testGetOneColumnSameNameAsArgFunctionCriteria() { public void testGetOneColumnSameNameAsArgFunctionCriteria() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn(); EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn();
e.setLower( 3 ); e.setLower( 3 );
e.setUpper( " abc " ); e.setUpper( " abc " );
s.persist( e ); s.persist( e );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<EntityWithArgFunctionAsColumn> criteria = criteriaBuilder.createQuery( EntityWithArgFunctionAsColumn.class ); CriteriaQuery<EntityWithArgFunctionAsColumn> criteria = criteriaBuilder.createQuery(
criteria.from( EntityWithArgFunctionAsColumn.class ); EntityWithArgFunctionAsColumn.class );
criteria.from( EntityWithArgFunctionAsColumn.class );
e = s.createQuery( criteria ).uniqueResult(); EntityWithArgFunctionAsColumn e = s.createQuery( criteria ).uniqueResult();
// e = ( EntityWithArgFunctionAsColumn ) s.createCriteria( EntityWithArgFunctionAsColumn.class ).uniqueResult(); // e = ( EntityWithArgFunctionAsColumn ) s.createCriteria( EntityWithArgFunctionAsColumn.class ).uniqueResult();
assertEquals( 3, e.getLower() ); assertEquals( 3, e.getLower() );
assertEquals( " abc ", e.getUpper() ); assertEquals( " abc ", e.getUpper() );
t.commit(); }
s.close(); );
cleanup();
} }
@Test @Test
public void testGetMultiColumnSameNameAsArgFunctionHQL() { public void testGetMultiColumnSameNameAsArgFunctionHQL() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn(); EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn();
e1.setLower( 3 ); e1.setLower( 3 );
e1.setUpper( " abc " ); e1.setUpper( " abc " );
EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn(); EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn();
e2.setLower( 999 ); e2.setLower( 999 );
e2.setUpper( " xyz " ); e2.setUpper( " xyz " );
EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
holder1.getEntityWithArgFunctionAsColumns().add( e1 ); holder1.getEntityWithArgFunctionAsColumns().add( e1 );
EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
holder2.getEntityWithArgFunctionAsColumns().add( e2 ); holder2.getEntityWithArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 ); holder1.setNextHolder( holder2 );
s.save( holder1 ); s.save( holder1 );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
holder1 = ( EntityWithFunctionAsColumnHolder ) s.createQuery( EntityWithFunctionAsColumnHolder holder1 = (EntityWithFunctionAsColumnHolder) s.createQuery(
"from EntityWithFunctionAsColumnHolder h left join fetch h.entityWithArgFunctionAsColumns " + "from EntityWithFunctionAsColumnHolder h left join fetch h.entityWithArgFunctionAsColumns " +
"left join fetch h.nextHolder left join fetch h.nextHolder.entityWithArgFunctionAsColumns " + "left join fetch h.nextHolder left join fetch h.nextHolder.entityWithArgFunctionAsColumns " +
"where h.nextHolder is not null" ) "where h.nextHolder is not null" )
.uniqueResult(); .uniqueResult();
assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) );
assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() ); assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() );
e1 = ( EntityWithArgFunctionAsColumn ) holder1.getEntityWithArgFunctionAsColumns().iterator().next(); EntityWithArgFunctionAsColumn e1 = (EntityWithArgFunctionAsColumn) holder1.getEntityWithArgFunctionAsColumns().iterator().next();
assertEquals( 3, e1.getLower() ); assertEquals( 3, e1.getLower() );
assertEquals( " abc ", e1.getUpper() ); assertEquals( " abc ", e1.getUpper() );
assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() ); assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() );
e2 = ( EntityWithArgFunctionAsColumn ) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns().iterator().next(); EntityWithArgFunctionAsColumn e2 = (EntityWithArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns()
assertEquals( 999, e2.getLower() ); .iterator()
assertEquals( " xyz ", e2.getUpper() ); .next();
t.commit(); assertEquals( 999, e2.getLower() );
s.close(); assertEquals( " xyz ", e2.getUpper() );
}
cleanup(); );
} }
@Test @Test
public void testGetMultiColumnSameNameAsArgFunctionCriteria() { public void testGetMultiColumnSameNameAsArgFunctionCriteria() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn(); EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn();
e1.setLower( 3 ); e1.setLower( 3 );
e1.setUpper( " abc " ); e1.setUpper( " abc " );
EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn(); EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn();
e2.setLower( 999 ); e2.setLower( 999 );
e2.setUpper( " xyz " ); e2.setUpper( " xyz " );
EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
holder1.getEntityWithArgFunctionAsColumns().add( e1 ); holder1.getEntityWithArgFunctionAsColumns().add( e1 );
EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
holder2.getEntityWithArgFunctionAsColumns().add( e2 ); holder2.getEntityWithArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 ); holder1.setNextHolder( holder2 );
s.save( holder1 ); s.save( holder1 );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery( CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
EntityWithFunctionAsColumnHolder.class ); EntityWithFunctionAsColumnHolder.class );
Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class ); Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class );
root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT )
.fetch( "nextHolder", JoinType.LEFT ) .fetch( "nextHolder", JoinType.LEFT )
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); .fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT );
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
holder1 = s.createQuery( criteria ).uniqueResult(); EntityWithFunctionAsColumnHolder holder1 = s.createQuery( criteria ).uniqueResult();
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class ) // holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
// .add( Restrictions.isNotNull( "nextHolder" )) // .add( Restrictions.isNotNull( "nextHolder" ))
@ -183,21 +182,21 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
// .setFetchMode( "nextHolder", FetchMode.JOIN ) // .setFetchMode( "nextHolder", FetchMode.JOIN )
// .setFetchMode( "nextHolder.entityWithArgFunctionAsColumns", FetchMode.JOIN ) // .setFetchMode( "nextHolder.entityWithArgFunctionAsColumns", FetchMode.JOIN )
// .uniqueResult(); // .uniqueResult();
assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) );
assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() ); assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() );
e1 = ( EntityWithArgFunctionAsColumn ) holder1.getEntityWithArgFunctionAsColumns().iterator().next(); EntityWithArgFunctionAsColumn e1 = (EntityWithArgFunctionAsColumn) holder1.getEntityWithArgFunctionAsColumns().iterator().next();
assertEquals( 3, e1.getLower() ); assertEquals( 3, e1.getLower() );
assertEquals( " abc ", e1.getUpper() ); assertEquals( " abc ", e1.getUpper() );
assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() ); assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() );
e2 = ( EntityWithArgFunctionAsColumn ) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns().iterator().next(); EntityWithArgFunctionAsColumn e2 = (EntityWithArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns()
assertEquals( 999, e2.getLower() ); .iterator()
assertEquals( " xyz ", e2.getUpper() ); .next();
t.commit(); assertEquals( 999, e2.getLower() );
s.close(); assertEquals( " xyz ", e2.getUpper() );
}
cleanup(); );
} }
@Test @Test
@ -208,42 +207,51 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
return; return;
} }
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 );
}
);
Session s = openSession(); Session s = openSession();
Transaction t = s.beginTransaction(); try {
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn(); EntityWithFunctionAsColumnHolder holder1 = (EntityWithFunctionAsColumnHolder) s.createQuery(
e1.setCurrentDate( "blah blah blah" ); "from EntityWithFunctionAsColumnHolder h left join fetch h.entityWithNoArgFunctionAsColumns " +
EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn(); "left join fetch h.nextHolder left join fetch h.nextHolder.entityWithNoArgFunctionAsColumns " +
e2.setCurrentDate( "yadda yadda yadda" ); "where h.nextHolder is not null" )
EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); .uniqueResult();
holder1.getEntityWithNoArgFunctionAsColumns().add( e1 ); assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) );
EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
holder2.getEntityWithNoArgFunctionAsColumns().add( e2 ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) );
holder1.setNextHolder( holder2 ); assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() );
s.save( holder1 ); s.getTransaction().commit();
t.commit(); s.close();
s.close();
s = openSession(); EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next();
t = s.beginTransaction(); assertEquals( "blah blah blah", e1.getCurrentDate() );
holder1 = ( EntityWithFunctionAsColumnHolder ) s.createQuery( assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() );
"from EntityWithFunctionAsColumnHolder h left join fetch h.entityWithNoArgFunctionAsColumns " + EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns()
"left join fetch h.nextHolder left join fetch h.nextHolder.entityWithNoArgFunctionAsColumns " + .iterator()
"where h.nextHolder is not null" ) .next();
.uniqueResult(); assertEquals( "yadda yadda yadda", e2.getCurrentDate() );
assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) ); }catch (Exception e){
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); if (s.getTransaction().isActive()){
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) ); s.getTransaction().rollback();
assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() ); }
t.commit(); }finally {
s.close(); if(s.isOpen()) {
s.close();
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();
assertEquals( "yadda yadda yadda", e2.getCurrentDate() );
cleanup();
} }
@Test @Test
@ -254,32 +262,32 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
return; return;
} }
Session s = openSession();
Transaction t = s.beginTransaction();
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
e1.setCurrentDate( "blah blah blah" );
EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
e2.setCurrentDate( "yadda yadda yadda" );
EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
holder1.getEntityWithNoArgFunctionAsColumns().add( e1 );
EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
holder2.getEntityWithNoArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 );
s.save( holder1 );
t.commit();
s.close();
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
e1.setCurrentDate( "blah blah blah" );
EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn();
e2.setCurrentDate( "yadda yadda yadda" );
holder1.getEntityWithNoArgFunctionAsColumns().add( e1 );
holder2.getEntityWithNoArgFunctionAsColumns().add( e2 );
holder1.setNextHolder( holder2 );
s.save( holder1 );
}
);
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); inTransaction(
CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery( s -> {
EntityWithFunctionAsColumnHolder.class ); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class ); CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) EntityWithFunctionAsColumnHolder.class );
.fetch( "nextHolder", JoinType.LEFT ) Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class );
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT )
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); .fetch( "nextHolder", JoinType.LEFT )
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT );
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class ) // holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )
// .add( Restrictions.isNotNull( "nextHolder" )) // .add( Restrictions.isNotNull( "nextHolder" ))
@ -287,19 +295,23 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
// .setFetchMode( "nextHolder", FetchMode.JOIN ) // .setFetchMode( "nextHolder", FetchMode.JOIN )
// .setFetchMode( "nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN ) // .setFetchMode( "nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN )
// .uniqueResult(); // .uniqueResult();
assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) );
assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) ); assertTrue( Hibernate.isInitialized( holder1.getNextHolder()
assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() ); .getEntityWithNoArgFunctionAsColumns() ) );
e1 = ( EntityWithNoArgFunctionAsColumn ) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next(); assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() );
assertEquals( "blah blah blah", e1.getCurrentDate() ); EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns()
assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() ); .iterator()
e2 = ( EntityWithNoArgFunctionAsColumn ) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns().iterator().next(); .next();
assertEquals( "yadda yadda yadda", e2.getCurrentDate() ); assertEquals( "blah blah blah", e1.getCurrentDate() );
t.commit(); assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() );
s.close(); EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns()
.iterator()
.next();
assertEquals( "yadda yadda yadda", e2.getCurrentDate() );
cleanup(); }
);
} }
@Test @Test
@ -310,59 +322,68 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
return; return;
} }
Session s = openSession();
Transaction t = s.beginTransaction();
EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn(); EntityWithNoArgFunctionAsColumn e1 = new EntityWithNoArgFunctionAsColumn();
e1.setCurrentDate( "blah blah blah" );
EntityWithNoArgFunctionAsColumn e2 = new EntityWithNoArgFunctionAsColumn(); 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(); inTransaction(
t = s.beginTransaction(); s -> {
List results = s.createQuery( e1.setCurrentDate( "blah blah blah" );
"select str(current_date), currentDate from EntityWithNoArgFunctionAsColumn" e2.setCurrentDate( "yadda yadda yadda" );
).list(); EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder();
assertEquals( 2, results.size() ); holder1.getEntityWithNoArgFunctionAsColumns().add( e1 );
assertEquals( ( ( Object[] ) results.get( 0 ) )[ 0 ], ( ( Object[] ) results.get( 1 ) )[ 0 ] ); EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder();
assertTrue( ! ( ( Object[] ) results.get( 0 ) )[ 0 ].equals( ( ( Object[] ) results.get( 0 ) )[ 1 ] ) ); holder2.getEntityWithNoArgFunctionAsColumns().add( e2 );
assertTrue( ! ( ( Object[] ) results.get( 1 ) )[ 0 ].equals( ( ( Object[] ) results.get( 1 ) )[ 1 ] ) ); holder1.setNextHolder( holder2 );
assertTrue( ( ( Object[] ) results.get( 0 ) )[ 1 ].equals( e1.getCurrentDate() ) || s.save( holder1 );
( ( Object[] ) results.get( 0 ) )[ 1 ].equals( e2.getCurrentDate() ) ); }
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(); inTransaction(
s -> {
List results = s.createQuery(
"select str(current_date), currentDate from EntityWithNoArgFunctionAsColumn"
).list();
assertEquals( 2, results.size() );
assertEquals( ( (Object[]) results.get( 0 ) )[0], ( (Object[]) results.get( 1 ) )[0] );
assertTrue( !( (Object[]) results.get( 0 ) )[0].equals( ( (Object[]) results.get( 0 ) )[1] ) );
assertTrue( !( (Object[]) results.get( 1 ) )[0].equals( ( (Object[]) results.get( 1 ) )[1] ) );
assertTrue( ( (Object[]) results.get( 0 ) )[1].equals( e1.getCurrentDate() ) ||
( (Object[]) results.get( 0 ) )[1].equals( e2.getCurrentDate() ) );
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] ) );
}
);
} }
private void cleanup() { @After
Session s = openSession(); public void cleanup() {
Transaction t = s.beginTransaction(); SQLFunction function = sessionFactory().getSqlFunctionRegistry().findSQLFunction( "current_date" );
s.createQuery( "delete from EntityWithArgFunctionAsColumn" ).executeUpdate(); if ( function == null || function.hasParenthesesIfNoArguments() ) {
t.commit(); SkipLog.reportSkip( "current_date reuires ()", "tests noarg function that does not require ()" );
s.close(); return;
}
inTransaction(
s -> {
s.createQuery( "delete from EntityWithArgFunctionAsColumn" ).executeUpdate();
s = openSession(); }
t = s.beginTransaction(); );
s.createQuery( "delete from EntityWithNoArgFunctionAsColumn" ).executeUpdate();
t.commit();
s.close();
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
s.createQuery( "delete from EntityWithFunctionAsColumnHolder where nextHolder is not null" ).executeUpdate(); s.createQuery( "delete from EntityWithNoArgFunctionAsColumn" ).executeUpdate();
s.createQuery( "delete from EntityWithFunctionAsColumnHolder" ).executeUpdate();
t.commit(); }
s.close(); );
inTransaction(
s -> {
s.createQuery( "delete from EntityWithFunctionAsColumnHolder where nextHolder is not null" )
.executeUpdate();
s.createQuery( "delete from EntityWithFunctionAsColumnHolder" ).executeUpdate();
}
);
} }
} }

View File

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

View File

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

View File

@ -7,15 +7,15 @@
package org.hibernate.test.subselectfetch; package org.hibernate.test.subselectfetch;
import java.util.List; 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.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test; import org.junit.Test;
@ -41,347 +41,369 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testSubselectFetchHql() { public void testSubselectFetchHql() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
Parent p = new Parent("foo"); Parent p = new Parent( "foo" );
p.getChildren().add( new Child("foo1") ); p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child("foo2") ); p.getChildren().add( new Child( "foo2" ) );
Parent q = new Parent("bar"); Parent q = new Parent( "bar" );
q.getChildren().add( new Child("bar1") ); q.getChildren().add( new Child( "bar1" ) );
q.getChildren().add( new Child("bar2") ); q.getChildren().add( new Child( "bar2" ) );
q.getMoreChildren().addAll( p.getChildren() ); q.getMoreChildren().addAll( p.getChildren() );
s.persist(p); s.persist( p );
s.persist(q); s.persist( q );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
sessionFactory().getStatistics().clear();
sessionFactory().getStatistics().clear(); List parents = s.createQuery( "from Parent where name between 'bar' and 'foo' order by name desc" )
.list();
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
List parents = s.createQuery("from Parent where name between 'bar' and 'foo' order by name desc") assertFalse( Hibernate.isInitialized( p.getChildren() ) );
.list(); assertFalse( Hibernate.isInitialized( q.getChildren() ) );
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
assertFalse( Hibernate.isInitialized( p.getChildren() ) ); assertEquals( p.getChildren().size(), 2 );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
assertEquals( p.getChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); assertTrue( Hibernate.isInitialized( q.getChildren() ) );
assertTrue( Hibernate.isInitialized( q.getChildren() ) ); assertEquals( q.getChildren().size(), 2 );
assertEquals( q.getChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); assertEquals( p.getMoreChildren().size(), 0 );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertEquals( p.getMoreChildren().size(), 0 ); assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); assertEquals( q.getMoreChildren().size(), 2 );
assertEquals( q.getMoreChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() );
assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); Child c = (Child) p.getChildren().get( 0 );
c.getFriends().size();
Child c = (Child) p.getChildren().get(0); s.delete( p );
c.getFriends().size(); s.delete( q );
}
s.delete(p); );
s.delete(q);
t.commit();
s.close();
} }
@Test @Test
public void testSubselectFetchNamedParam() { public void testSubselectFetchNamedParam() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
Parent p = new Parent("foo"); Parent p = new Parent( "foo" );
p.getChildren().add( new Child("foo1") ); p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child("foo2") ); p.getChildren().add( new Child( "foo2" ) );
Parent q = new Parent("bar"); Parent q = new Parent( "bar" );
q.getChildren().add( new Child("bar1") ); q.getChildren().add( new Child( "bar1" ) );
q.getChildren().add( new Child("bar2") ); q.getChildren().add( new Child( "bar2" ) );
q.getMoreChildren().addAll( p.getChildren() ); q.getMoreChildren().addAll( p.getChildren() );
s.persist(p); s.persist( p );
s.persist(q); s.persist( q );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
sessionFactory().getStatistics().clear();
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();
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
List parents = s.createQuery("from Parent where name between :bar and :foo order by name desc") assertFalse( Hibernate.isInitialized( p.getChildren() ) );
.setParameter("bar", "bar") assertFalse( Hibernate.isInitialized( q.getChildren() ) );
.setParameter("foo", "foo")
.list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
assertFalse( Hibernate.isInitialized( p.getChildren() ) ); assertEquals( p.getChildren().size(), 2 );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
assertEquals( p.getChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); assertTrue( Hibernate.isInitialized( q.getChildren() ) );
assertTrue( Hibernate.isInitialized( q.getChildren() ) ); assertEquals( q.getChildren().size(), 2 );
assertEquals( q.getChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); assertEquals( p.getMoreChildren().size(), 0 );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertEquals( p.getMoreChildren().size(), 0 ); assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); assertEquals( q.getMoreChildren().size(), 2 );
assertEquals( q.getMoreChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() );
assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); Child c = (Child) p.getChildren().get( 0 );
c.getFriends().size();
Child c = (Child) p.getChildren().get(0); s.delete( p );
c.getFriends().size(); s.delete( q );
}
s.delete(p); );
s.delete(q);
t.commit();
s.close();
} }
@Test @Test
public void testSubselectFetchPosParam() { public void testSubselectFetchPosParam() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
Parent p = new Parent("foo"); Parent p = new Parent( "foo" );
p.getChildren().add( new Child("foo1") ); p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child("foo2") ); p.getChildren().add( new Child( "foo2" ) );
Parent q = new Parent("bar"); Parent q = new Parent( "bar" );
q.getChildren().add( new Child("bar1") ); q.getChildren().add( new Child( "bar1" ) );
q.getChildren().add( new Child("bar2") ); q.getChildren().add( new Child( "bar2" ) );
q.getMoreChildren().addAll( p.getChildren() ); q.getMoreChildren().addAll( p.getChildren() );
s.persist(p); s.persist( p );
s.persist(q); s.persist( q );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
sessionFactory().getStatistics().clear();
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();
Parent p = (Parent) parents.get( 0 );
Parent q = (Parent) parents.get( 1 );
List parents = s.createQuery("from Parent where name between ?1 and ?2 order by name desc") assertFalse( Hibernate.isInitialized( p.getChildren() ) );
.setParameter(1, "bar") assertFalse( Hibernate.isInitialized( q.getChildren() ) );
.setParameter(2, "foo")
.list();
p = (Parent) parents.get(0);
q = (Parent) parents.get(1);
assertFalse( Hibernate.isInitialized( p.getChildren() ) ); assertEquals( p.getChildren().size(), 2 );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
assertEquals( p.getChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); assertTrue( Hibernate.isInitialized( q.getChildren() ) );
assertTrue( Hibernate.isInitialized( q.getChildren() ) ); assertEquals( q.getChildren().size(), 2 );
assertEquals( q.getChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); assertEquals( p.getMoreChildren().size(), 0 );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertEquals( p.getMoreChildren().size(), 0 ); assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); assertEquals( q.getMoreChildren().size(), 2 );
assertEquals( q.getMoreChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() );
assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); Child c = (Child) p.getChildren().get( 0 );
c.getFriends().size();
Child c = (Child) p.getChildren().get(0); s.delete( p );
c.getFriends().size(); s.delete( q );
}
s.delete(p); );
s.delete(q);
t.commit();
s.close();
} }
@Test @Test
public void testSubselectFetchWithLimit() { 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(); Parent parent = new Parent( "foo" );
t = s.beginTransaction(); 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 );
}
);
sessionFactory().getStatistics().clear(); inTransaction(
s -> {
sessionFactory().getStatistics().clear();
List parents = s.createQuery("from Parent order by name desc") List parents = s.createQuery( "from Parent order by name desc" )
.setMaxResults(2) .setMaxResults( 2 )
.list(); .list();
p = (Parent) parents.get(0); Parent p = (Parent) parents.get( 0 );
q = (Parent) parents.get(1); Parent q = (Parent) parents.get( 1 );
assertFalse( Hibernate.isInitialized( p.getChildren() ) ); assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( q.getChildren() ) ); assertFalse( Hibernate.isInitialized( q.getChildren() ) );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertEquals( p.getMoreChildren().size(), 0 ); assertEquals( p.getMoreChildren().size(), 0 );
assertEquals( p.getChildren().size(), 2 ); assertEquals( p.getChildren().size(), 2 );
assertTrue( Hibernate.isInitialized( q.getChildren() ) ); assertTrue( Hibernate.isInitialized( q.getChildren() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); 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() ) ); assertTrue( Hibernate.isInitialized( r.getChildren() ) );
assertFalse( Hibernate.isInitialized( r.getMoreChildren() ) ); assertFalse( Hibernate.isInitialized( r.getMoreChildren() ) );
assertEquals( r.getChildren().size(), 1 ); assertEquals( r.getChildren().size(), 1 );
assertEquals( r.getMoreChildren().size(), 0 ); assertEquals( r.getMoreChildren().size(), 0 );
s.delete(p); s.delete( p );
s.delete(q); s.delete( q );
s.delete(r); s.delete( r );
}
t.commit(); );
s.close();
} }
@Test @Test
public void testManyToManyCriteriaJoin() { public void testManyToManyCriteriaJoin() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
Parent p = new Parent("foo"); Parent p = new Parent( "foo" );
p.getChildren().add( new Child("foo1") ); p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child("foo2") ); p.getChildren().add( new Child( "foo2" ) );
Parent q = new Parent("bar"); Parent q = new Parent( "bar" );
q.getChildren().add( new Child("bar1") ); q.getChildren().add( new Child( "bar1" ) );
q.getChildren().add( new Child("bar2") ); q.getChildren().add( new Child( "bar2" ) );
q.getMoreChildren().addAll( p.getChildren() ); q.getMoreChildren().addAll( p.getChildren() );
s.persist(p); s.persist( p );
s.persist(q); s.persist( q );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); 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) s.createQuery( criteria ).list();
.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) ); criteria = criteriaBuilder.createQuery( Parent.class );
s.delete( parents.get(1) ); root = criteria.from( Parent.class );
Join<Object, Object> moreChildren = root.join( "moreChildren", JoinType.LEFT );
friends = moreChildren.join( "friends", JoinType.LEFT );
t.commit(); root.fetch( "moreChildren", JoinType.LEFT ).fetch(
s.close(); "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 ) );
}
);
} }
@Test @Test
public void testSubselectFetchCriteria() { public void testSubselectFetchCriteria() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
Parent p = new Parent("foo"); Parent p = new Parent( "foo" );
p.getChildren().add( new Child("foo1") ); p.getChildren().add( new Child( "foo1" ) );
p.getChildren().add( new Child("foo2") ); p.getChildren().add( new Child( "foo2" ) );
Parent q = new Parent("bar"); Parent q = new Parent( "bar" );
q.getChildren().add( new Child("bar1") ); q.getChildren().add( new Child( "bar1" ) );
q.getChildren().add( new Child("bar2") ); q.getChildren().add( new Child( "bar2" ) );
q.getMoreChildren().addAll( p.getChildren() ); q.getMoreChildren().addAll( p.getChildren() );
s.persist(p); s.persist( p );
s.persist(q); s.persist( q );
t.commit(); }
s.close(); );
s = openSession();
t = s.beginTransaction();
sessionFactory().getStatistics().clear(); inTransaction(
s -> {
sessionFactory().getStatistics().clear();
List parents = s.createCriteria(Parent.class) CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
.add( Property.forName("name").between("bar", "foo") ) CriteriaQuery<Parent> criteria = criteriaBuilder.createQuery( Parent.class );
.addOrder( Order.desc("name") ) Root<Parent> root = criteria.from( Parent.class );
.list(); criteria.where( criteriaBuilder.between( root.get( "name" ), "bar", "foo" ) );
p = (Parent) parents.get(0); criteria.orderBy( criteriaBuilder.desc( root.get( "name" ) ) );
q = (Parent) parents.get(1);
assertFalse( Hibernate.isInitialized( p.getChildren() ) ); List parents = s.createQuery( criteria ).list();
assertFalse( Hibernate.isInitialized( q.getChildren() ) ); // 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 );
assertEquals( p.getChildren().size(), 2 ); assertFalse( Hibernate.isInitialized( p.getChildren() ) );
assertFalse( Hibernate.isInitialized( q.getChildren() ) );
assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); assertEquals( p.getChildren().size(), 2 );
assertTrue( Hibernate.isInitialized( q.getChildren() ) ); assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) );
assertEquals( q.getChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getChildren() ) );
assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); assertEquals( q.getChildren().size(), 2 );
assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertEquals( p.getMoreChildren().size(), 0 ); assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) );
assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); assertEquals( p.getMoreChildren().size(), 0 );
assertEquals( q.getMoreChildren().size(), 2 ); assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) );
assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); assertEquals( q.getMoreChildren().size(), 2 );
assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) );
Child c = (Child) p.getChildren().get(0); assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() );
c.getFriends().size();
s.delete(p); Child c = (Child) p.getChildren().get( 0 );
s.delete(q); c.getFriends().size();
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.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.hibernate.Hibernate; 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.HSQLDialect;
import org.hibernate.dialect.TeradataDialect; import org.hibernate.dialect.TeradataDialect;
@ -41,78 +40,96 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
comment = "SQL uses Teradata reserved word: title" comment = "SQL uses Teradata reserved word: title"
) )
public void testUnionSubclass() { public void testUnionSubclass() {
Session s = openSession();
Transaction t = s.beginTransaction();
Employee mark = new Employee(); inTransaction(
mark.setName("Mark"); s -> {
mark.setTitle("internal sales"); Employee mark = new Employee();
mark.setSex('M'); mark.setName( "Mark" );
mark.setAddress("buckhead"); mark.setTitle( "internal sales" );
mark.setZip("30305"); mark.setSex( 'M' );
mark.setCountry("USA"); mark.setAddress( "buckhead" );
mark.setZip( "30305" );
mark.setCountry( "USA" );
Customer joe = new Customer(); Customer joe = new Customer();
joe.setName("Joe"); joe.setName( "Joe" );
joe.setAddress("San Francisco"); joe.setAddress( "San Francisco" );
joe.setZip("XXXXX"); joe.setZip( "XXXXX" );
joe.setCountry("USA"); joe.setCountry( "USA" );
joe.setComments("Very demanding"); joe.setComments( "Very demanding" );
joe.setSex('M'); joe.setSex( 'M' );
joe.setSalesperson(mark); joe.setSalesperson( mark );
Person yomomma = new Person(); Person yomomma = new Person();
yomomma.setName("mum"); yomomma.setName( "mum" );
yomomma.setSex('F'); yomomma.setSex( 'F' );
s.save(yomomma); s.save( yomomma );
s.save(mark); s.save( mark );
s.save(joe); s.save( joe );
assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); assertEquals( s.createQuery( "from java.io.Serializable" ).list().size(), 0 );
assertEquals( s.createQuery("from Person").list().size(), 3 ); 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 = Customer" ).list().size(), 1 );
assertEquals( s.createQuery("from Person p where p.class = Person").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" ).setParameter(
assertEquals( s.createQuery("from Person p where type(p) in :who").setParameterList("who", new Class[] {Customer.class, Person.class}).list().size(), 2 ); "who",
s.clear(); 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(); List customers = s.createQuery( "from Customer c left join fetch c.salesperson" ).list();
for ( Object customer : customers ) { for ( Object customer : customers ) {
Customer c = (Customer) customer; Customer c = (Customer) customer;
assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); assertTrue( 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(); customers = s.createQuery( "from Customer" ).list();
for ( Object customer : customers ) { for ( Object customer : customers ) {
Customer c = (Customer) customer; Customer c = (Customer) customer;
assertFalse( 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();
mark = (Employee) s.get( Employee.class, Long.valueOf( mark.getId() ) ); mark = s.get( Employee.class, Long.valueOf( mark.getId() ) );
joe = (Customer) s.get( Customer.class, Long.valueOf( joe.getId() ) ); joe = s.get( Customer.class, Long.valueOf( 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.createCriteria( Person.class ).add( CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
Restrictions.in( "address", new Address[] { mark.getAddress(), CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
joe.getAddress() } ) ).list(); Root<Person> root = criteria.from( Person.class );
s.delete(mark); CriteriaBuilder.In<Object> addresses = criteriaBuilder.in( root.get( "address" ) );
s.delete(joe); addresses.value( new Address[] { mark.getAddress(), joe.getAddress() } );
s.delete(yomomma); criteria.where( addresses );
assertTrue( s.createQuery("from Person").list().isEmpty() );
t.commit(); s.createQuery( criteria ).list();
s.close(); // 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() );
}
);
} }
@Test @Test
@ -126,40 +143,47 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
return; // TODO : why?? return; // TODO : why??
} }
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("select salary from Person where salary > 100").list(); result = s.createQuery( criteria ).list();
assertEquals( result.size(), 1 );
assertEquals( ( (BigDecimal) result.get(0) ).intValue(), 1000 );
s.delete(p); // result = s.createCriteria( Person.class )
s.delete(q); // .add( Property.forName( "salary" ).gt( new BigDecimal( 100 ) ) )
t.commit(); // .list();
s.close(); assertEquals( result.size(), 1 );
assertSame( result.get( 0 ), q );
result = s.createQuery( "select salary from Person where salary > 100" ).list();
assertEquals( result.size(), 1 );
assertEquals( ( (BigDecimal) result.get( 0 ) ).intValue(), 1000 );
s.delete( p );
s.delete( q );
}
);
} }
@Test @Test
@ -169,87 +193,115 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
comment = "SQL uses Teradata reserved word: title" comment = "SQL uses Teradata reserved word: title"
) )
public void testCustomColumnReadAndWrite() { public void testCustomColumnReadAndWrite() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
final double HEIGHT_INCHES = 73; final double HEIGHT_INCHES = 73;
final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d; final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
Person p = new Person(); Person p = new Person();
p.setName("Emmanuel"); p.setName( "Emmanuel" );
p.setSex('M'); p.setSex( 'M' );
p.setHeightInches(HEIGHT_INCHES); p.setHeightInches( HEIGHT_INCHES );
s.persist(p); s.persist( p );
final double PASSWORD_EXPIRY_WEEKS = 4; final double PASSWORD_EXPIRY_WEEKS = 4;
final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d; final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d;
Employee e = new Employee(); Employee e = new Employee();
e.setName("Steve"); e.setName( "Steve" );
e.setSex('M'); e.setSex( 'M' );
e.setTitle("Mr"); e.setTitle( "Mr" );
e.setPasswordExpiryDays(PASSWORD_EXPIRY_DAYS); e.setPasswordExpiryDays( PASSWORD_EXPIRY_DAYS );
s.persist(e); s.persist( e );
s.flush(); s.flush();
// Test value conversion during insert // Test value conversion during insert
// Value returned by Oracle native query is a Types.NUMERIC, which is mapped to a BigDecimalType; // 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. // Cast returned value to Number then call Number.doubleValue() so it works on all dialects.
Double heightViaSql = Double heightViaSql =
( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() ).doubleValue(); ( (Number) s.createNativeQuery(
assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); "select height_centimeters from UPerson where name='Emmanuel'" )
Double expiryViaSql = .uniqueResult() ).doubleValue();
( (Number)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?") assertEquals( HEIGHT_CENTIMETERS, heightViaSql, 0.01d );
.setLong(1, e.getId()) Double expiryViaSql =
.uniqueResult() ( (Number) s.createNativeQuery( "select pwd_expiry_weeks from UEmployee where person_id=?" )
).doubleValue(); .setParameter( 1, e.getId() )
assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d); .uniqueResult()
).doubleValue();
assertEquals( PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d );
// Test projection // Test projection
Double heightViaHql = (Double)s.createQuery("select p.heightInches from Person p where p.name = 'Emmanuel'").uniqueResult(); Double heightViaHql = (Double) s.createQuery(
assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d); "select p.heightInches from Person p where p.name = 'Emmanuel'" )
Double expiryViaHql = (Double)s.createQuery("select e.passwordExpiryDays from Employee e where e.name = 'Steve'").uniqueResult(); .uniqueResult();
assertEquals(PASSWORD_EXPIRY_DAYS, expiryViaHql, 0.01d); assertEquals( HEIGHT_INCHES, heightViaHql, 0.01d );
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 // Test restriction and entity load via criteria
p = (Person)s.createCriteria(Person.class) CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
.add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d)) CriteriaQuery<Person> personCriteria = criteriaBuilder.createQuery( Person.class );
.uniqueResult(); Root<Person> personRoot = personCriteria.from( Person.class );
assertEquals(HEIGHT_INCHES, p.getHeightInches(), 0.01d); personCriteria.where( criteriaBuilder.between(
e = (Employee)s.createCriteria(Employee.class) personRoot.get( "heightInches" ),
.add(Restrictions.between("passwordExpiryDays", PASSWORD_EXPIRY_DAYS - 0.01d, PASSWORD_EXPIRY_DAYS + 0.01d)) HEIGHT_INCHES - 0.01d,
.uniqueResult(); HEIGHT_INCHES + 0.01d
assertEquals(PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d); ) );
// Test predicate and entity load via HQL p = s.createQuery( personCriteria ).uniqueResult();
p = (Person)s.createQuery("from Person p where p.heightInches between ?1 and ?2") // p = (Person) s.createCriteria( Person.class )
.setDouble(1, HEIGHT_INCHES - 0.01d) // .add( Restrictions.between( "heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d ) )
.setDouble(2, HEIGHT_INCHES + 0.01d) // .uniqueResult();
.uniqueResult(); assertEquals( HEIGHT_INCHES, p.getHeightInches(), 0.01d );
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)
.uniqueResult();
assertEquals(PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d);
// Test update CriteriaQuery<Employee> employeeCriteria = criteriaBuilder.createQuery( Employee.class );
p.setHeightInches(1); Root<Employee> employeeRoot = employeeCriteria.from( Employee.class );
e.setPasswordExpiryDays(7); employeeCriteria.where( criteriaBuilder.between(
s.flush(); employeeRoot.get( "passwordExpiryDays" ),
heightViaSql = PASSWORD_EXPIRY_DAYS - 0.01d,
( (Number)s.createSQLQuery("select height_centimeters from UPerson where name='Emmanuel'").uniqueResult() ) PASSWORD_EXPIRY_DAYS - 0.01d
.doubleValue(); ) );
assertEquals(2.54d, heightViaSql, 0.01d);
expiryViaSql =
( (Number)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?")
.setLong(1, e.getId())
.uniqueResult()
).doubleValue();
assertEquals(1d, expiryViaSql, 0.01d);
s.delete(p);
s.delete(e);
t.commit();
s.close();
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" )
.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" )
.setParameter( 1, PASSWORD_EXPIRY_DAYS - 0.01d )
.setParameter( 2, PASSWORD_EXPIRY_DAYS + 0.01d )
.uniqueResult();
assertEquals( PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d );
// Test update
p.setHeightInches( 1 );
e.setPasswordExpiryDays( 7 );
s.flush();
heightViaSql =
( (Number) s.createNativeQuery(
"select height_centimeters from UPerson where name='Emmanuel'" )
.uniqueResult() )
.doubleValue();
assertEquals( 2.54d, heightViaSql, 0.01d );
expiryViaSql =
( (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 );
}
);
} }
} }