From d1b86c2a72f9472df5b045fd7668c9281cc900a4 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 16 Jul 2019 15:42:50 +0100 Subject: [PATCH] 6 - SQM based on JPA type system --- .../test/hql/FunctionNameAsColumnTest.java | 489 +++++++------- .../onetoone/formula/OneToOneFormulaTest.java | 1 - .../test/subselect/SubselectTest.java | 197 +++--- .../subselectfetch/SubselectFetchTest.java | 618 +++++++++--------- .../unionsubclass2/UnionSubclassTest.java | 428 ++++++------ 5 files changed, 919 insertions(+), 814 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java b/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java index 3e90cd8576..17c5190888 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/hql/FunctionNameAsColumnTest.java @@ -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; @@ -36,9 +37,9 @@ import static org.junit.Assert.assertTrue; * * @author Gail Badner */ -@SkipForDialect( value = SybaseASE15Dialect.class, jiraKey = "HHH-6426") -@SkipForDialect( value = PostgresPlusDialect.class, comment = "Almost all of the tests result in 'ambiguous column' errors.") -public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { +@SkipForDialect(value = SybaseASE15Dialect.class, jiraKey = "HHH-6426") +@SkipForDialect(value = PostgresPlusDialect.class, comment = "Almost all of the tests result in 'ambiguous column' errors.") +public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { @Override public String[] getMappings() { return new String[] { @@ -54,128 +55,126 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { @Test public void testGetOneColumnSameNameAsArgFunctionHQL() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn(); - e.setLower( 3 ); - e.setUpper( " abc " ); - s.persist( e ); - t.commit(); - s.close(); + inTransaction( + s -> { + EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn(); + e.setLower( 3 ); + e.setUpper( " abc " ); + s.persist( e ); + } + ); - s = openSession(); - t = s.beginTransaction(); - e = ( EntityWithArgFunctionAsColumn ) s.createQuery( "from EntityWithArgFunctionAsColumn" ).uniqueResult(); - assertEquals( 3, e.getLower() ); - assertEquals( " abc ", e.getUpper() ); - t.commit(); - s.close(); - - cleanup(); + inTransaction( + s -> { + EntityWithArgFunctionAsColumn e = (EntityWithArgFunctionAsColumn) s.createQuery( + "from EntityWithArgFunctionAsColumn" ).uniqueResult(); + assertEquals( 3, e.getLower() ); + assertEquals( " abc ", e.getUpper() ); + } + ); } @Test public void testGetOneColumnSameNameAsArgFunctionCriteria() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn(); - e.setLower( 3 ); - e.setUpper( " abc " ); - s.persist( e ); - t.commit(); - s.close(); + inTransaction( + s -> { + EntityWithArgFunctionAsColumn e = new EntityWithArgFunctionAsColumn(); + e.setLower( 3 ); + e.setUpper( " abc " ); + s.persist( e ); + } + ); - s = openSession(); - t = s.beginTransaction(); - CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); - CriteriaQuery criteria = criteriaBuilder.createQuery( EntityWithArgFunctionAsColumn.class ); - criteria.from( EntityWithArgFunctionAsColumn.class ); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery 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(); + assertEquals( 3, e.getLower() ); + assertEquals( " abc ", e.getUpper() ); + } + ); } @Test public void testGetMultiColumnSameNameAsArgFunctionHQL() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn(); - e1.setLower( 3 ); - e1.setUpper( " abc " ); - EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn(); - e2.setLower( 999 ); - e2.setUpper( " xyz " ); - EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); - holder1.getEntityWithArgFunctionAsColumns().add( e1 ); - EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); - holder2.getEntityWithArgFunctionAsColumns().add( e2 ); - holder1.setNextHolder( holder2 ); - s.save( holder1 ); - t.commit(); - s.close(); + inTransaction( + s -> { + EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn(); + e1.setLower( 3 ); + e1.setUpper( " abc " ); + EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn(); + e2.setLower( 999 ); + e2.setUpper( " xyz " ); + EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); + holder1.getEntityWithArgFunctionAsColumns().add( e1 ); + EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); + holder2.getEntityWithArgFunctionAsColumns().add( e2 ); + holder1.setNextHolder( holder2 ); + s.save( holder1 ); + } + ); - s = openSession(); - t = s.beginTransaction(); - 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" ) - .uniqueResult(); - assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) ); - assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() ); - 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(); - assertEquals( 999, e2.getLower() ); - assertEquals( " xyz ", e2.getUpper() ); - t.commit(); - s.close(); - - cleanup(); + 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" ) + .uniqueResult(); + assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) ); + assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() ); + EntityWithArgFunctionAsColumn e1 = (EntityWithArgFunctionAsColumn) holder1.getEntityWithArgFunctionAsColumns().iterator().next(); + assertEquals( 3, e1.getLower() ); + assertEquals( " abc ", e1.getUpper() ); + assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() ); + EntityWithArgFunctionAsColumn e2 = (EntityWithArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns() + .iterator() + .next(); + assertEquals( 999, e2.getLower() ); + assertEquals( " xyz ", e2.getUpper() ); + } + ); } @Test public void testGetMultiColumnSameNameAsArgFunctionCriteria() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn(); - e1.setLower( 3 ); - e1.setUpper( " abc " ); - EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn(); - e2.setLower( 999 ); - e2.setUpper( " xyz " ); - EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); - holder1.getEntityWithArgFunctionAsColumns().add( e1 ); - EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); - holder2.getEntityWithArgFunctionAsColumns().add( e2 ); - holder1.setNextHolder( holder2 ); - s.save( holder1 ); - t.commit(); - s.close(); + inTransaction( + s -> { + EntityWithArgFunctionAsColumn e1 = new EntityWithArgFunctionAsColumn(); + e1.setLower( 3 ); + e1.setUpper( " abc " ); + EntityWithArgFunctionAsColumn e2 = new EntityWithArgFunctionAsColumn(); + e2.setLower( 999 ); + e2.setUpper( " xyz " ); + EntityWithFunctionAsColumnHolder holder1 = new EntityWithFunctionAsColumnHolder(); + holder1.getEntityWithArgFunctionAsColumns().add( e1 ); + EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); + holder2.getEntityWithArgFunctionAsColumns().add( e2 ); + holder1.setNextHolder( holder2 ); + s.save( holder1 ); + } + ); - s = openSession(); - t = s.beginTransaction(); - CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); - CriteriaQuery criteria = criteriaBuilder.createQuery( - EntityWithFunctionAsColumnHolder.class ); - Root root = criteria.from( EntityWithFunctionAsColumnHolder.class ); - root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) - .fetch( "nextHolder", JoinType.LEFT ) - .fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); - criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( + EntityWithFunctionAsColumnHolder.class ); + Root root = criteria.from( EntityWithFunctionAsColumnHolder.class ); + root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) + .fetch( "nextHolder", JoinType.LEFT ) + .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" )) @@ -183,21 +182,21 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { // .setFetchMode( "nextHolder", FetchMode.JOIN ) // .setFetchMode( "nextHolder.entityWithArgFunctionAsColumns", FetchMode.JOIN ) // .uniqueResult(); - assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) ); - assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() ); - 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(); - assertEquals( 999, e2.getLower() ); - assertEquals( " xyz ", e2.getUpper() ); - t.commit(); - s.close(); - - cleanup(); + assertTrue( Hibernate.isInitialized( holder1.getEntityWithArgFunctionAsColumns() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithArgFunctionAsColumns() ) ); + assertEquals( 1, holder1.getEntityWithArgFunctionAsColumns().size() ); + EntityWithArgFunctionAsColumn e1 = (EntityWithArgFunctionAsColumn) holder1.getEntityWithArgFunctionAsColumns().iterator().next(); + assertEquals( 3, e1.getLower() ); + assertEquals( " abc ", e1.getUpper() ); + assertEquals( 1, holder1.getNextHolder().getEntityWithArgFunctionAsColumns().size() ); + EntityWithArgFunctionAsColumn e2 = (EntityWithArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithArgFunctionAsColumns() + .iterator() + .next(); + assertEquals( 999, e2.getLower() ); + assertEquals( " xyz ", e2.getUpper() ); + } + ); } @Test @@ -208,42 +207,51 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { 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(); - 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(); - holder1.getEntityWithNoArgFunctionAsColumns().add( e1 ); - EntityWithFunctionAsColumnHolder holder2 = new EntityWithFunctionAsColumnHolder(); - holder2.getEntityWithNoArgFunctionAsColumns().add( e2 ); - holder1.setNextHolder( holder2 ); - s.save( holder1 ); - t.commit(); - s.close(); + 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" ) + .uniqueResult(); + assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) ); + assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() ); + s.getTransaction().commit(); + s.close(); - s = openSession(); - t = s.beginTransaction(); - 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" ) - .uniqueResult(); - assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) ); - assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() ); - t.commit(); - 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(); + EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns().iterator().next(); + assertEquals( "blah blah blah", e1.getCurrentDate() ); + assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() ); + EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns() + .iterator() + .next(); + assertEquals( "yadda yadda yadda", e2.getCurrentDate() ); + }catch (Exception e){ + if (s.getTransaction().isActive()){ + s.getTransaction().rollback(); + } + }finally { + if(s.isOpen()) { + s.close(); + } + } } @Test @@ -254,32 +262,32 @@ 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(); - 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 -> { + 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(); - CriteriaQuery criteria = criteriaBuilder.createQuery( - EntityWithFunctionAsColumnHolder.class ); - Root root = criteria.from( EntityWithFunctionAsColumnHolder.class ); - root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) - .fetch( "nextHolder", JoinType.LEFT ) - .fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); - criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( + EntityWithFunctionAsColumnHolder.class ); + Root root = criteria.from( EntityWithFunctionAsColumnHolder.class ); + root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ) + .fetch( "nextHolder", JoinType.LEFT ) + .fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); + criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); // holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class ) // .add( Restrictions.isNotNull( "nextHolder" )) @@ -287,19 +295,23 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { // .setFetchMode( "nextHolder", FetchMode.JOIN ) // .setFetchMode( "nextHolder.entityWithNoArgFunctionAsColumns", FetchMode.JOIN ) // .uniqueResult(); - assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); - assertTrue( Hibernate.isInitialized( holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns() ) ); - assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() ); - 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() ); - t.commit(); - s.close(); + assertTrue( Hibernate.isInitialized( holder1.getEntityWithNoArgFunctionAsColumns() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder() ) ); + assertTrue( Hibernate.isInitialized( holder1.getNextHolder() + .getEntityWithNoArgFunctionAsColumns() ) ); + assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() ); + EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns() + .iterator() + .next(); + assertEquals( "blah blah blah", e1.getCurrentDate() ); + assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() ); + EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns() + .iterator() + .next(); + assertEquals( "yadda yadda yadda", e2.getCurrentDate() ); - cleanup(); + } + ); } @Test @@ -310,59 +322,68 @@ 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(); - 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(); - 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 ] ) ); - t.commit(); - s.close(); + inTransaction( + s -> { + e1.setCurrentDate( "blah blah blah" ); + 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 ); + } + ); - 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() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - s.createQuery( "delete from EntityWithArgFunctionAsColumn" ).executeUpdate(); - t.commit(); - s.close(); + @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(); - s = openSession(); - t = s.beginTransaction(); - 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(); - s.createQuery( "delete from EntityWithFunctionAsColumnHolder" ).executeUpdate(); - t.commit(); - s.close(); + inTransaction( + s -> { + s.createQuery( "delete from EntityWithNoArgFunctionAsColumn" ).executeUpdate(); + + } + ); + + inTransaction( + s -> { + s.createQuery( "delete from EntityWithFunctionAsColumnHolder where nextHolder is not null" ) + .executeUpdate(); + s.createQuery( "delete from EntityWithFunctionAsColumnHolder" ).executeUpdate(); + } + ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java b/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java index 828e93bb5f..082ef7edcb 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/onetoone/formula/OneToOneFormulaTest.java @@ -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; diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselect/SubselectTest.java b/hibernate-core/src/test/java/org/hibernate/test/subselect/SubselectTest.java index 027f259ae4..6efb1cb1c3 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/subselect/SubselectTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/subselect/SubselectTest.java @@ -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; @@ -28,101 +29,111 @@ public class SubselectTest extends BaseCoreFunctionalTestCase { } @Test - @SuppressWarnings( {"unchecked"}) + @SuppressWarnings({ "unchecked" }) public void testEntitySubselect() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Human gavin = new Human(); - gavin.setName( "gavin" ); - gavin.setSex( 'M' ); - gavin.setAddress( "Melbourne, Australia" ); - Alien x23y4 = new Alien(); - x23y4.setIdentity( "x23y4$$hu%3" ); - x23y4.setPlanet( "Mars" ); - x23y4.setSpecies( "martian" ); - s.save(gavin); - s.save(x23y4); - s.flush(); - List beings = ( List) s.createQuery("from Being").list(); - for ( Being being : beings ) { - assertNotNull( being.getLocation() ); - assertNotNull( being.getIdentity() ); - assertNotNull( being.getSpecies() ); - } - s.clear(); - sessionFactory().getCache().evictEntityRegion( Being.class ); - Being gav = (Being) s.get(Being.class, gavin.getId()); - assertEquals( gav.getLocation(), gavin.getAddress() ); - assertEquals( gav.getSpecies(), "human" ); - assertEquals( gav.getIdentity(), gavin.getName() ); - s.clear(); - //test the tag: - gavin = (Human) 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(); + inTransaction( + s -> { + Human gavin = new Human(); + gavin.setName( "gavin" ); + gavin.setSex( 'M' ); + gavin.setAddress( "Melbourne, Australia" ); + Alien x23y4 = new Alien(); + x23y4.setIdentity( "x23y4$$hu%3" ); + x23y4.setPlanet( "Mars" ); + x23y4.setSpecies( "martian" ); + s.save( gavin ); + s.save( x23y4 ); + s.flush(); + List beings = (List) s.createQuery( "from Being" ).list(); + for ( Being being : beings ) { + assertNotNull( being.getLocation() ); + assertNotNull( being.getIdentity() ); + assertNotNull( being.getSpecies() ); + } + s.clear(); + sessionFactory().getCache().evictEntityRegion( Being.class ); + 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 tag: + 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() ); + + } + ); } @Test public void testCustomColumnReadAndWrite() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - final double HUMAN_INCHES = 73; - final double ALIEN_INCHES = 931; - final double HUMAN_CENTIMETERS = HUMAN_INCHES * 2.54d; - final double ALIEN_CENTIMETERS = ALIEN_INCHES * 2.54d; - Human gavin = new Human(); - gavin.setName( "gavin" ); - gavin.setSex( 'M' ); - gavin.setAddress( "Melbourne, Australia" ); - gavin.setHeightInches( HUMAN_INCHES ); - Alien x23y4 = new Alien(); - x23y4.setIdentity( "x23y4$$hu%3" ); - x23y4.setPlanet( "Mars" ); - x23y4.setSpecies( "martian" ); - x23y4.setHeightInches( ALIEN_INCHES ); - s.save(gavin); - s.save(x23y4); - s.flush(); - - // Test value conversion during insert - // 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(); - assertEquals(HUMAN_CENTIMETERS, humanHeightViaSql, 0.01d); - Double alienHeightViaSql = - ( (Number)s.createSQLQuery("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(); - 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(); - 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) - .uniqueResult(); - assertEquals(ALIEN_INCHES, b.getHeightInches(), 0.01d); - s.delete(gavin); - s.delete(x23y4); - t.commit(); - s.close(); - + inTransaction( + s -> { + final double HUMAN_INCHES = 73; + final double ALIEN_INCHES = 931; + final double HUMAN_CENTIMETERS = HUMAN_INCHES * 2.54d; + final double ALIEN_CENTIMETERS = ALIEN_INCHES * 2.54d; + Human gavin = new Human(); + gavin.setName( "gavin" ); + gavin.setSex( 'M' ); + gavin.setAddress( "Melbourne, Australia" ); + gavin.setHeightInches( HUMAN_INCHES ); + Alien x23y4 = new Alien(); + x23y4.setIdentity( "x23y4$$hu%3" ); + x23y4.setPlanet( "Mars" ); + x23y4.setSpecies( "martian" ); + x23y4.setHeightInches( ALIEN_INCHES ); + s.save( gavin ); + s.save( x23y4 ); + s.flush(); + + // Test value conversion during insert + // 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.createNativeQuery( "select height_centimeters from humans" ) + .uniqueResult() ).doubleValue(); + assertEquals( HUMAN_CENTIMETERS, humanHeightViaSql, 0.01d ); + Double alienHeightViaSql = + ( (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(); + assertEquals( HUMAN_INCHES, heightViaHql, 0.01d ); + + // Test restriction and entity load via criteria + + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Being.class ); + Root 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" ) + .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 ); + } + ); } } - diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchTest.java b/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchTest.java index 129574a320..754241c9bf 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchTest.java @@ -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,347 +41,369 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase { @Test public void testSubselectFetchHql() { - 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") ); - q.getMoreChildren().addAll( p.getChildren() ); - s.persist(p); - s.persist(q); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - - 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); + inTransaction( + s -> { + 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" ) ); + q.getMoreChildren().addAll( p.getChildren() ); + s.persist( p ); + s.persist( q ); + } + ); - assertFalse( Hibernate.isInitialized( p.getChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getChildren() ) ); + inTransaction( + s -> { + sessionFactory().getStatistics().clear(); - assertEquals( p.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); - - assertTrue( Hibernate.isInitialized( q.getChildren() ) ); - - assertEquals( q.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); - - assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + 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 ); - assertEquals( p.getMoreChildren().size(), 0 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); - - assertEquals( q.getMoreChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); - - assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + assertFalse( Hibernate.isInitialized( p.getChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getChildren() ) ); - Child c = (Child) p.getChildren().get(0); - c.getFriends().size(); + assertEquals( p.getChildren().size(), 2 ); - s.delete(p); - s.delete(q); + assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); - t.commit(); - s.close(); + assertTrue( Hibernate.isInitialized( q.getChildren() ) ); + + assertEquals( q.getChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); + + assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( p.getMoreChildren().size(), 0 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( q.getMoreChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); + + assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + + Child c = (Child) p.getChildren().get( 0 ); + c.getFriends().size(); + + s.delete( p ); + s.delete( q ); + } + ); } @Test public void testSubselectFetchNamedParam() { - 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") ); - q.getMoreChildren().addAll( p.getChildren() ); - s.persist(p); - s.persist(q); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - - 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); + inTransaction( + s -> { + 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" ) ); + q.getMoreChildren().addAll( p.getChildren() ); + s.persist( p ); + s.persist( q ); + } + ); - assertFalse( Hibernate.isInitialized( p.getChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getChildren() ) ); + inTransaction( + s -> { + sessionFactory().getStatistics().clear(); - assertEquals( p.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); - - assertTrue( Hibernate.isInitialized( q.getChildren() ) ); - - assertEquals( q.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); - - assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + 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 ); - assertEquals( p.getMoreChildren().size(), 0 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); - - assertEquals( q.getMoreChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); - - assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + assertFalse( Hibernate.isInitialized( p.getChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getChildren() ) ); - Child c = (Child) p.getChildren().get(0); - c.getFriends().size(); + assertEquals( p.getChildren().size(), 2 ); - s.delete(p); - s.delete(q); + assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); - t.commit(); - s.close(); + assertTrue( Hibernate.isInitialized( q.getChildren() ) ); + + assertEquals( q.getChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); + + assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( p.getMoreChildren().size(), 0 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( q.getMoreChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); + + assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + + Child c = (Child) p.getChildren().get( 0 ); + c.getFriends().size(); + + s.delete( p ); + s.delete( q ); + } + ); } @Test public void testSubselectFetchPosParam() { - 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") ); - q.getMoreChildren().addAll( p.getChildren() ); - s.persist(p); - s.persist(q); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - - 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); + inTransaction( + s -> { + 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" ) ); + q.getMoreChildren().addAll( p.getChildren() ); + s.persist( p ); + s.persist( q ); + } + ); - assertFalse( Hibernate.isInitialized( p.getChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getChildren() ) ); + inTransaction( + s -> { + sessionFactory().getStatistics().clear(); - assertEquals( p.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); - - assertTrue( Hibernate.isInitialized( q.getChildren() ) ); - - assertEquals( q.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); - - assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + 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 ); - assertEquals( p.getMoreChildren().size(), 0 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); - - assertEquals( q.getMoreChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); - - assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + assertFalse( Hibernate.isInitialized( p.getChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getChildren() ) ); - Child c = (Child) p.getChildren().get(0); - c.getFriends().size(); + assertEquals( p.getChildren().size(), 2 ); - s.delete(p); - s.delete(q); + assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); - t.commit(); - s.close(); + assertTrue( Hibernate.isInitialized( q.getChildren() ) ); + + assertEquals( q.getChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); + + assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( p.getMoreChildren().size(), 0 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( q.getMoreChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); + + assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + + Child c = (Child) p.getChildren().get( 0 ); + c.getFriends().size(); + + s.delete( p ); + s.delete( q ); + } + ); } @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(); - - 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); - assertFalse( Hibernate.isInitialized( p.getChildren() ) ); - assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); - assertEquals( p.getMoreChildren().size(), 0 ); - assertEquals( p.getChildren().size(), 2 ); - assertTrue( Hibernate.isInitialized( q.getChildren() ) ); - assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); - - assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); - - r = (Parent) s.get( Parent.class, r.getName() ); - assertTrue( Hibernate.isInitialized( r.getChildren() ) ); - assertFalse( Hibernate.isInitialized( r.getMoreChildren() ) ); - assertEquals( r.getChildren().size(), 1 ); - assertEquals( r.getMoreChildren().size(), 0 ); - s.delete(p); - s.delete(q); - s.delete(r); - - t.commit(); - s.close(); + 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(); + 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() ) ); + assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + assertEquals( p.getMoreChildren().size(), 0 ); + assertEquals( p.getChildren().size(), 2 ); + assertTrue( Hibernate.isInitialized( q.getChildren() ) ); + assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + + Parent r = s.get( Parent.class, thirdParent.getName() ); + assertTrue( Hibernate.isInitialized( r.getChildren() ) ); + assertFalse( Hibernate.isInitialized( r.getMoreChildren() ) ); + assertEquals( r.getChildren().size(), 1 ); + assertEquals( r.getMoreChildren().size(), 0 ); + + s.delete( p ); + s.delete( q ); + s.delete( r ); + } + ); } @Test public void testManyToManyCriteriaJoin() { - 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") ); - q.getMoreChildren().addAll( p.getChildren() ); - s.persist(p); - s.persist(q); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - - List parents = s.createCriteria(Parent.class) - .createCriteria("moreChildren") - .createCriteria("friends") - .addOrder( Order.desc("name") ) - .list(); + inTransaction( + s -> { + 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" ) ); + q.getMoreChildren().addAll( p.getChildren() ); + s.persist( p ); + s.persist( q ); + } + ); - parents = s.createCriteria(Parent.class) - .setFetchMode("moreChildren", FetchMode.JOIN) - .setFetchMode("moreChildren.friends", FetchMode.JOIN) - .addOrder( Order.desc("name") ) - .list(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Parent.class ); + Root root = criteria.from( Parent.class ); + Join friends = root.join( "moreChildren", JoinType.INNER ) + .join( "friends", JoinType.INNER ); + criteria.orderBy( criteriaBuilder.desc( friends.get( "name" ) ) ); - s.delete( parents.get(0) ); - s.delete( parents.get(1) ); + s.createQuery( criteria ).list(); - t.commit(); - s.close(); + + criteria = criteriaBuilder.createQuery( Parent.class ); + root = criteria.from( Parent.class ); + Join 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 ) ); + + } + ); } @Test public void testSubselectFetchCriteria() { - 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") ); - q.getMoreChildren().addAll( p.getChildren() ); - s.persist(p); - s.persist(q); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - - 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); + inTransaction( + s -> { + 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" ) ); + q.getMoreChildren().addAll( p.getChildren() ); + s.persist( p ); + s.persist( q ); + } + ); - assertFalse( Hibernate.isInitialized( p.getChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getChildren() ) ); - assertEquals( p.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); - - assertTrue( Hibernate.isInitialized( q.getChildren() ) ); - - assertEquals( q.getChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); - - assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); - assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + inTransaction( + s -> { + sessionFactory().getStatistics().clear(); - assertEquals( p.getMoreChildren().size(), 0 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); - - assertEquals( q.getMoreChildren().size(), 2 ); - - assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); - - assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Parent.class ); + Root root = criteria.from( Parent.class ); + criteria.where( criteriaBuilder.between( root.get( "name" ), "bar", "foo" ) ); + criteria.orderBy( criteriaBuilder.desc( root.get( "name" ) ) ); - Child c = (Child) p.getChildren().get(0); - c.getFriends().size(); + 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 ); - s.delete(p); - s.delete(q); + assertFalse( Hibernate.isInitialized( p.getChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getChildren() ) ); + + assertEquals( p.getChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( p.getChildren().iterator().next() ) ); + + assertTrue( Hibernate.isInitialized( q.getChildren() ) ); + + assertEquals( q.getChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getChildren().iterator().next() ) ); + + assertFalse( Hibernate.isInitialized( p.getMoreChildren() ) ); + assertFalse( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( p.getMoreChildren().size(), 0 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren() ) ); + + assertEquals( q.getMoreChildren().size(), 2 ); + + assertTrue( Hibernate.isInitialized( q.getMoreChildren().iterator().next() ) ); + + assertEquals( 3, sessionFactory().getStatistics().getPrepareStatementCount() ); + + Child c = (Child) p.getChildren().get( 0 ); + c.getFriends().size(); + + s.delete( p ); + s.delete( q ); + } + ); - t.commit(); - s.close(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java index f5c5adb4e8..22da12e1a9 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/unionsubclass2/UnionSubclassTest.java @@ -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,78 +40,96 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase { comment = "SQL uses Teradata reserved word: title" ) public void testUnionSubclass() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - - Employee mark = new Employee(); - mark.setName("Mark"); - mark.setTitle("internal sales"); - mark.setSex('M'); - mark.setAddress("buckhead"); - mark.setZip("30305"); - mark.setCountry("USA"); - - Customer joe = new Customer(); - joe.setName("Joe"); - joe.setAddress("San Francisco"); - joe.setZip("XXXXX"); - joe.setCountry("USA"); - joe.setComments("Very demanding"); - joe.setSex('M'); - joe.setSalesperson(mark); - - Person yomomma = new Person(); - yomomma.setName("mum"); - yomomma.setSex('F'); - - s.save(yomomma); - s.save(mark); - s.save(joe); - - assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); - - assertEquals( s.createQuery("from Person").list().size(), 3 ); - assertEquals( s.createQuery("from Person p where p.class = 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 ); - s.clear(); - List customers = s.createQuery("from Customer c left join fetch c.salesperson").list(); - for ( Object customer : customers ) { - Customer c = (Customer) customer; - assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); - assertEquals( c.getSalesperson().getName(), "Mark" ); - } - assertEquals( customers.size(), 1 ); - s.clear(); - - customers = s.createQuery("from Customer").list(); - for ( Object customer : customers ) { - Customer c = (Customer) customer; - assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); - assertEquals( c.getSalesperson().getName(), "Mark" ); - } - assertEquals( customers.size(), 1 ); - s.clear(); - + inTransaction( + s -> { + Employee mark = new Employee(); + mark.setName( "Mark" ); + mark.setTitle( "internal sales" ); + mark.setSex( 'M' ); + mark.setAddress( "buckhead" ); + mark.setZip( "30305" ); + mark.setCountry( "USA" ); - mark = (Employee) s.get( Employee.class, Long.valueOf( mark.getId() ) ); - joe = (Customer) 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 ); + Customer joe = new Customer(); + joe.setName( "Joe" ); + joe.setAddress( "San Francisco" ); + joe.setZip( "XXXXX" ); + joe.setCountry( "USA" ); + joe.setComments( "Very demanding" ); + joe.setSex( 'M' ); + joe.setSalesperson( mark ); - 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(); + Person yomomma = new Person(); + yomomma.setName( "mum" ); + yomomma.setSex( 'F' ); + + s.save( yomomma ); + s.save( mark ); + s.save( joe ); + + assertEquals( s.createQuery( "from java.io.Serializable" ).list().size(), 0 ); + + assertEquals( s.createQuery( "from Person" ).list().size(), 3 ); + assertEquals( s.createQuery( "from Person p where p.class = 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 ); + s.clear(); + + List customers = s.createQuery( "from Customer c left join fetch c.salesperson" ).list(); + for ( Object customer : customers ) { + Customer c = (Customer) customer; + assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); + assertEquals( c.getSalesperson().getName(), "Mark" ); + } + assertEquals( customers.size(), 1 ); + s.clear(); + + customers = s.createQuery( "from Customer" ).list(); + for ( Object customer : customers ) { + Customer c = (Customer) customer; + assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); + assertEquals( c.getSalesperson().getName(), "Mark" ); + } + assertEquals( customers.size(), 1 ); + s.clear(); + + + mark = s.get( Employee.class, 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 ); + + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + + CriteriaBuilder.In 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() ); + + } + ); } @Test @@ -125,41 +142,48 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase { if ( getDialect() instanceof HSQLDialect ) { return; // TODO : why?? } - - Session s = openSession(); - Transaction t = s.beginTransaction(); - Person p = new Person(); - p.setName("Emmanuel"); - p.setSex('M'); - s.persist(p); - Employee q = new Employee(); - q.setName("Steve"); - q.setSex('M'); - q.setTitle("Mr"); - q.setSalary( new BigDecimal(1000) ); - s.persist(q); - List result = s.createQuery("from Person where salary > 100").list(); - assertEquals( result.size(), 1 ); - assertSame( result.get(0), q ); - - result = s.createQuery("from Person where salary > 100 or name like 'E%'").list(); - assertEquals( result.size(), 2 ); + inTransaction( + s -> { + Person p = new Person(); + p.setName( "Emmanuel" ); + p.setSex( 'M' ); + s.persist( p ); + Employee q = new Employee(); + q.setName( "Steve" ); + q.setSex( 'M' ); + q.setTitle( "Mr" ); + q.setSalary( new BigDecimal( 1000 ) ); + s.persist( q ); - result = s.createCriteria(Person.class) - .add( Property.forName("salary").gt( new BigDecimal(100) ) ) - .list(); - assertEquals( result.size(), 1 ); - assertSame( result.get(0), q ); + List result = s.createQuery( "from Person where salary > 100" ).list(); + 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); - t.commit(); - s.close(); + result = s.createQuery( "from Person where salary > 100 or name like 'E%'" ).list(); + assertEquals( result.size(), 2 ); + + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root 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 ); + + 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 @@ -169,87 +193,115 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase { comment = "SQL uses Teradata reserved word: title" ) public void testCustomColumnReadAndWrite() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - final double HEIGHT_INCHES = 73; - final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d; - Person p = new Person(); - p.setName("Emmanuel"); - p.setSex('M'); - p.setHeightInches(HEIGHT_INCHES); - s.persist(p); - final double PASSWORD_EXPIRY_WEEKS = 4; - final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d; - Employee e = new Employee(); - e.setName("Steve"); - e.setSex('M'); - e.setTitle("Mr"); - e.setPasswordExpiryDays(PASSWORD_EXPIRY_DAYS); - s.persist(e); - s.flush(); - - // Test value conversion during insert - // 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(); - assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); - Double expiryViaSql = - ( (Number)s.createSQLQuery("select pwd_expiry_weeks from UEmployee where person_id=?") - .setLong(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(); - 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 - 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(); - 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) - .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) - .uniqueResult(); - assertEquals(PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d); - - // Test update - p.setHeightInches(1); - e.setPasswordExpiryDays(7); - s.flush(); - heightViaSql = - ( (Number)s.createSQLQuery("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()) - .uniqueResult() - ).doubleValue(); - assertEquals(1d, expiryViaSql, 0.01d); - s.delete(p); - s.delete(e); - t.commit(); - s.close(); - - } - - -} + inTransaction( + s -> { + final double HEIGHT_INCHES = 73; + final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d; + Person p = new Person(); + p.setName( "Emmanuel" ); + p.setSex( 'M' ); + p.setHeightInches( HEIGHT_INCHES ); + s.persist( p ); + final double PASSWORD_EXPIRY_WEEKS = 4; + final double PASSWORD_EXPIRY_DAYS = PASSWORD_EXPIRY_WEEKS * 7d; + Employee e = new Employee(); + e.setName( "Steve" ); + e.setSex( 'M' ); + e.setTitle( "Mr" ); + e.setPasswordExpiryDays( PASSWORD_EXPIRY_DAYS ); + s.persist( e ); + s.flush(); + // Test value conversion during insert + // 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.createNativeQuery( + "select height_centimeters from UPerson where name='Emmanuel'" ) + .uniqueResult() ).doubleValue(); + assertEquals( HEIGHT_CENTIMETERS, heightViaSql, 0.01d ); + Double expiryViaSql = + ( (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(); + 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 + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery personCriteria = criteriaBuilder.createQuery( Person.class ); + Root 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 ); + + CriteriaQuery employeeCriteria = criteriaBuilder.createQuery( Employee.class ); + Root 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" ) + .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 ); + } + ); + } +}