diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/components/ComponentCriteriaTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/components/ComponentCriteriaTest.java index 1a71a2159e..99f8eb268f 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/components/ComponentCriteriaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/components/ComponentCriteriaTest.java @@ -6,25 +6,21 @@ */ package org.hibernate.jpa.test.criteria.components; +import java.util.ArrayList; import java.util.List; -import java.util.Set; -import javax.persistence.EntityManager; -import javax.persistence.Query; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Fetch; import javax.persistence.criteria.Root; -import org.junit.Assert; -import org.junit.Test; - import org.hibernate.Hibernate; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.testing.TestForIssue; +import org.junit.Assert; +import org.junit.Test; -import static org.junit.Assert.assertFalse; +import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; import static org.junit.Assert.assertTrue; /** @@ -38,119 +34,102 @@ public class ComponentCriteriaTest extends BaseEntityManagerFunctionalTestCase { @Test public void testEmbeddableInPath() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); Client client = new Client( 111, "steve", "ebersole" ); - em.persist(client); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + em.persist( client ); + } ); + + doInJPA( this::entityManagerFactory, em -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery( Client.class ); + Root root = cq.from( Client.class ); + cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) ); + List list = em.createQuery( cq ).getResultList(); + Assert.assertEquals( 1, list.size() ); + } ); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery cq = cb.createQuery(Client.class); - Root root = cq.from(Client.class); - cq.where(cb.equal(root.get("name").get("firstName"), client.getName().getFirstName())); - List list = em.createQuery(cq).getResultList(); - Assert.assertEquals( 1, list.size() ); - em.getTransaction().commit(); - em.close(); - // HHH-5792 - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - TypedQuery< Client > q = em.createQuery( - "SELECT c FROM Client c JOIN c.name n WHERE n.firstName = '" - + client.getName().getFirstName() + "'", - Client.class ); - Assert.assertEquals( 1, q.getResultList().size() ); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + TypedQuery q = em.createQuery( + "SELECT c FROM Client c JOIN c.name n WHERE n.firstName = '" + + client.getName().getFirstName() + "'", + Client.class + ); + Assert.assertEquals( 1, q.getResultList().size() ); + } ); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - em.createQuery( "delete Client" ).executeUpdate(); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + em.createQuery( "delete Client" ).executeUpdate(); + } ); } @Test - @TestForIssue( jiraKey = "HHH-9642") + @TestForIssue(jiraKey = "HHH-9642") public void testOneToManyJoinFetchedInEmbeddable() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); Client client = new Client( 111, "steve", "ebersole" ); Alias alias = new Alias( "a", "guy", "work" ); client.getName().getAliases().add( alias ); - em.persist(client); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + em.persist( client ); + } ); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - CriteriaBuilder cb = em.getCriteriaBuilder(); - CriteriaQuery cq = cb.createQuery(Client.class); - Root root = cq.from(Client.class); - root.fetch( Client_.name ).fetch( Name_.aliases ); - cq.where(cb.equal(root.get("name").get("firstName"), client.getName().getFirstName())); - List list = em.createQuery(cq).getResultList(); - Assert.assertEquals( 1, list.size() ); - client = list.get( 0 ); - assertTrue( Hibernate.isInitialized( client.getName().getAliases() ) ); - em.getTransaction().commit(); - em.close(); + List list = new ArrayList<>(); + doInJPA( this::entityManagerFactory, em -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery cq = cb.createQuery( Client.class ); + Root root = cq.from( Client.class ); + root.fetch( Client_.name ).fetch( Name_.aliases ); + cq.where( cb.equal( root.get( "name" ).get( "firstName" ), client.getName().getFirstName() ) ); + list.addAll( em.createQuery( cq ).getResultList() ); + Assert.assertEquals( 1, list.size() ); + Client c = list.get( 0 ); + assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) ); + } ); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - TypedQuery< Client > q = em.createQuery( - "SELECT c FROM Client c JOIN FETCH c.name.aliases WHERE c.name.firstName = '" - + client.getName().getFirstName() + "'", - Client.class - ); - Assert.assertEquals( 1, q.getResultList().size() ); - client = list.get( 0 ); - assertTrue( Hibernate.isInitialized( client.getName().getAliases() ) ); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + TypedQuery q = em.createQuery( + "SELECT c FROM Client c JOIN FETCH c.name.aliases WHERE c.name.firstName = '" + + client.getName().getFirstName() + "'", + Client.class + ); + Assert.assertEquals( 1, q.getResultList().size() ); + Client c = list.get( 0 ); + assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) ); + } ); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - q = em.createQuery( - "SELECT c FROM Client c JOIN c.name n join FETCH n.aliases WHERE c.name.firstName = '" - + client.getName().getFirstName() + "'", - Client.class - ); - Assert.assertEquals( 1, q.getResultList().size() ); - client = list.get( 0 ); - assertTrue( Hibernate.isInitialized( client.getName().getAliases() ) ); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + TypedQuery q = em.createQuery( + "SELECT c FROM Client c JOIN c.name n join FETCH n.aliases WHERE c.name.firstName = '" + + client.getName().getFirstName() + "'", + Client.class + ); + Assert.assertEquals( 1, q.getResultList().size() ); + Client c = list.get( 0 ); + assertTrue( Hibernate.isInitialized( c.getName().getAliases() ) ); + } ); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - client = em.merge( client ); - em.remove( client ); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + Client c = em.merge( client ); + em.remove( c ); + } ); } @Test - @TestForIssue( jiraKey = "HHH-4586" ) + @TestForIssue(jiraKey = "HHH-4586") public void testParameterizedFunctions() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - CriteriaBuilder cb = em.getCriteriaBuilder(); - // lower - CriteriaQuery cq = cb.createQuery( Client.class ); - Root root = cq.from( Client.class ); - cq.where( cb.equal( cb.lower( root.get( Client_.name ).get( Name_.lastName ) ),"test" ) ); - em.createQuery( cq ).getResultList(); - // upper - cq = cb.createQuery( Client.class ); - root = cq.from( Client.class ); - cq.where( cb.equal( cb.upper( root.get( Client_.name ).get( Name_.lastName ) ),"test" ) ); - em.createQuery( cq ).getResultList(); - em.getTransaction().commit(); - em.close(); + doInJPA( this::entityManagerFactory, em -> { + CriteriaBuilder cb = em.getCriteriaBuilder(); + // lower + CriteriaQuery cq = cb.createQuery( Client.class ); + Root root = cq.from( Client.class ); + cq.where( cb.equal( cb.lower( root.get( Client_.name ).get( Name_.lastName ) ), "test" ) ); + em.createQuery( cq ).getResultList(); + // upper + cq = cb.createQuery( Client.class ); + root = cq.from( Client.class ); + cq.where( cb.equal( cb.upper( root.get( Client_.name ).get( Name_.lastName ) ), "test" ) ); + em.createQuery( cq ).getResultList(); + } + ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java index ff0d5ca953..3e08dde87a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/EnumTypeTest.java @@ -6,7 +6,11 @@ */ package org.hibernate.test.enums; -import org.hibernate.criterion.Restrictions; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.Session; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.type.descriptor.sql.BasicBinder; import org.hibernate.type.descriptor.sql.BasicExtractor; @@ -73,22 +77,34 @@ public class EnumTypeTest extends BaseCoreFunctionalTestCase { @Test @TestForIssue(jiraKey = "HHH-8153") public void hbmEnumTypeTest() { - doInHibernate( this::sessionFactory, s -> { - assertEquals( s.createCriteria( Person.class ) - .add( Restrictions.eq( "gender", Gender.MALE ) ) - .list().size(), 2 ); - assertEquals( s.createCriteria( Person.class ) - .add( Restrictions.eq( "gender", Gender.MALE ) ) - .add( Restrictions.eq( "hairColor", HairColor.BROWN ) ) - .list().size(), 1 ); - assertEquals( s.createCriteria( Person.class ) - .add( Restrictions.eq( "gender", Gender.FEMALE ) ) - .list().size(), 2 ); - assertEquals( s.createCriteria( Person.class ) - .add( Restrictions.eq( "gender", Gender.FEMALE ) ) - .add( Restrictions.eq( "hairColor", HairColor.BROWN ) ) - .list().size(), 1 ); - } ); + doInHibernate( + this::sessionFactory, + s -> { + assertEquals( getNumberOfPersonByGender( s, Gender.MALE ), 2 ); + assertEquals( getNumberOfPersonByGenderAndHairColor( s, Gender.MALE, HairColor.BROWN ), 1 ); + assertEquals( getNumberOfPersonByGender( s, Gender.FEMALE ), 2 ); + assertEquals( getNumberOfPersonByGenderAndHairColor( s, Gender.FEMALE, HairColor.BROWN ), 1 ); + } + ); + } + + private int getNumberOfPersonByGender(Session session, Gender value) { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + criteria.where( criteriaBuilder.equal( root.get( "gender" ), value ) ); + return session.createQuery( criteria ).list().size(); + } + + private int getNumberOfPersonByGenderAndHairColor(Session session, Gender gender, HairColor hairColor) { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + criteria.where( criteriaBuilder.and( + criteriaBuilder.equal( root.get( "gender" ), gender ), + criteriaBuilder.equal( root.get( "hairColor" ), hairColor ) + ) ); + return session.createQuery( criteria ).list().size(); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/test/enums/OrdinalEnumTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/enums/OrdinalEnumTypeTest.java index 5dcad2d552..47339ed592 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/enums/OrdinalEnumTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/enums/OrdinalEnumTypeTest.java @@ -12,7 +12,6 @@ import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.Id; -import org.hibernate.criterion.Restrictions; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.type.descriptor.sql.BasicBinder; import org.hibernate.type.descriptor.sql.BasicExtractor; @@ -27,7 +26,6 @@ import org.junit.Test; import org.jboss.logging.Logger; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** 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 17c5190888..d633266acf 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 @@ -288,24 +288,24 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase { .fetch( "nextHolder", JoinType.LEFT ) .fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT ); criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) ); - + EntityWithFunctionAsColumnHolder holder = s.createQuery( criteria ).uniqueResult(); // holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class ) // .add( Restrictions.isNotNull( "nextHolder" )) // .setFetchMode( "entityWithNoArgFunctionAsColumns", FetchMode.JOIN ) // .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() + assertTrue( Hibernate.isInitialized( holder.getEntityWithNoArgFunctionAsColumns() ) ); + assertTrue( Hibernate.isInitialized( holder.getNextHolder() ) ); + assertTrue( Hibernate.isInitialized( holder.getNextHolder() .getEntityWithNoArgFunctionAsColumns() ) ); - assertEquals( 1, holder1.getEntityWithNoArgFunctionAsColumns().size() ); - EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder1.getEntityWithNoArgFunctionAsColumns() + assertEquals( 1, holder.getEntityWithNoArgFunctionAsColumns().size() ); + EntityWithNoArgFunctionAsColumn e1 = (EntityWithNoArgFunctionAsColumn) holder.getEntityWithNoArgFunctionAsColumns() .iterator() .next(); assertEquals( "blah blah blah", e1.getCurrentDate() ); - assertEquals( 1, holder1.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() ); - EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder1.getNextHolder() ).getEntityWithNoArgFunctionAsColumns() + assertEquals( 1, holder.getNextHolder().getEntityWithNoArgFunctionAsColumns().size() ); + EntityWithNoArgFunctionAsColumn e2 = (EntityWithNoArgFunctionAsColumn) ( holder.getNextHolder() ).getEntityWithNoArgFunctionAsColumns() .iterator() .next(); assertEquals( "yadda yadda yadda", e2.getCurrentDate() ); 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 754241c9bf..0fb7b7b32b 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 @@ -296,32 +296,25 @@ public class SubselectFetchTest extends BaseCoreFunctionalTestCase { CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( Parent.class ); Root root = criteria.from( Parent.class ); - Join friends = root.join( "moreChildren", JoinType.INNER ) + root.join( "moreChildren", JoinType.INNER ) .join( "friends", JoinType.INNER ); - criteria.orderBy( criteriaBuilder.desc( friends.get( "name" ) ) ); + criteria.orderBy( criteriaBuilder.desc( root.get( "name" ) ) ); s.createQuery( criteria ).list(); - - - 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(); -// + + + criteria = criteriaBuilder.createQuery( Parent.class ); + root = criteria.from( Parent.class ); + root.fetch( "moreChildren", JoinType.LEFT ).fetch( "friends", JoinType.LEFT ); + criteria.orderBy( criteriaBuilder.desc( root.get( "name" ) ) ); + + List parents = s.createQuery( criteria ).list(); + // parents = s.createCriteria( Parent.class ) // .setFetchMode( "moreChildren", FetchMode.JOIN ) // .setFetchMode( "moreChildren.friends", FetchMode.JOIN ) diff --git a/hibernate-core/src/test/java/org/hibernate/test/where/hbm/WhereTest.java b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/WhereTest.java index 534064197d..071f983a9c 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/where/hbm/WhereTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/where/hbm/WhereTest.java @@ -9,18 +9,20 @@ package org.hibernate.test.where.hbm; import java.util.HashSet; 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.junit.After; import org.junit.Before; import org.junit.Test; -import org.hibernate.FetchMode; -import org.hibernate.criterion.Restrictions; import org.hibernate.query.NativeQuery; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.test.where.hbm.File; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; @@ -86,7 +88,7 @@ public class WhereTest extends BaseCoreFunctionalTestCase { final HashSet filesSet = new HashSet<>( files ); assertEquals( 1, filesSet.size() ); File parent = files.get( 0 ); - assertEquals( parent.getChildren().size(), 1 ); + assertEquals( 1, parent.getChildren().size() ); } ); } @@ -95,11 +97,19 @@ public class WhereTest extends BaseCoreFunctionalTestCase { public void testCriteria() { inTransaction( s -> { - File parent = (File) s.createCriteria( File.class ) - .setFetchMode( "children", FetchMode.JOIN ) - .add( Restrictions.isNull( "parent" ) ) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( File.class ); + Root root = criteria.from( File.class ); + root.fetch( "children", JoinType.LEFT ); + criteria.where( criteriaBuilder.isNull( root.get("parent") )); + File parent = s.createQuery( criteria ).uniqueResult(); assertEquals( parent.getChildren().size(), 1 ); + +// File parent = (File) s.createCriteria( File.class ) +// .setFetchMode( "children", FetchMode.JOIN ) +// .add( Restrictions.isNull( "parent" ) ) +// .uniqueResult(); + assertEquals( 1, parent.getChildren().size() ); } ); } @@ -115,7 +125,7 @@ public class WhereTest extends BaseCoreFunctionalTestCase { File parent = (File) ( (Object[]) query.list().get( 0 ) )[0]; // @Where should not be applied - assertEquals( parent.getChildren().size(), 2 ); + assertEquals( 2, parent.getChildren().size() ); } ); }