From 3d5209d1058ec329a3d9c5da45c05bee4656cdb4 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 12 Jul 2019 17:06:37 +0100 Subject: [PATCH] 6 - SQM based on JPA type system --- .../idmanytoone/IdManyToOneTest.java | 74 ++-- .../OptionalOneToOneMappedByTest.java | 63 +++- .../onetoone/OptionalOneToOnePKJCTest.java | 29 +- .../collection/set/PersistentSetTest.java | 62 +++- .../CompositeElementTest.java | 76 ++-- .../JoinSelfReferentialFetchProfileTest.java | 24 +- .../joinedsubclass/JoinedSubclassTest.java | 346 ++++++++++-------- .../bidir/embedded/KeyManyToOneTest.java | 81 ++-- .../internal/AliasWithCriterionTest.java | 0 9 files changed, 443 insertions(+), 312 deletions(-) rename hibernate-core/src/{test/java => test_legacy}/org/hibernate/query/criteria/internal/AliasWithCriterionTest.java (100%) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java index a1ef198b3f..05cabc48ed 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/idmanytoone/IdManyToOneTest.java @@ -6,11 +6,15 @@ */ package org.hibernate.test.annotations.idmanytoone; +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.Session; -import org.hibernate.Transaction; import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl; import org.hibernate.cfg.Configuration; -import org.hibernate.criterion.Restrictions; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; @@ -23,7 +27,7 @@ import static org.junit.Assert.assertEquals; */ public class IdManyToOneTest extends BaseCoreFunctionalTestCase { @Test - public void testFkCreationOrdering() throws Exception { + public void testFkCreationOrdering() { //no real test case, the sessionFactory building is tested Session s = openSession(); s.close(); @@ -31,47 +35,49 @@ public class IdManyToOneTest extends BaseCoreFunctionalTestCase { @Test public void testIdClassManyToOne() { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - Store store = new Store(); - Customer customer = new Customer(); - s.persist( store ); - s.persist( customer ); - StoreCustomer sc = new StoreCustomer( store, customer ); - s.persist( sc ); - s.flush(); - s.clear(); - - store = (Store) s.get(Store.class, store.id ); - assertEquals( 1, store.customers.size() ); - assertEquals( customer.id, store.customers.iterator().next().customer.id ); - tx.rollback(); + inTransaction( s-> { + Store store = new Store(); + Customer customer = new Customer(); + s.persist( store ); + s.persist( customer ); + StoreCustomer sc = new StoreCustomer( store, customer ); + s.persist( sc ); + s.flush(); + s.clear(); + store = s.get(Store.class, store.id ); + assertEquals( 1, store.customers.size() ); + assertEquals( customer.id, store.customers.iterator().next().customer.id ); + } ); //TODO test Customers / ShoppingBaskets / BasketItems testIdClassManyToOneWithReferenceColumn - s.close(); } @Test @TestForIssue( jiraKey = "HHH-7767" ) public void testCriteriaRestrictionOnIdManyToOne() { - Session s = openSession(); - s.beginTransaction(); + inTransaction( s -> { + s.createQuery( "from Course c join c.students cs join cs.student s where s.name = 'Foo'" ).list(); - s.createQuery( "from Course c join c.students cs join cs.student s where s.name = 'Foo'" ).list(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Course.class ); + Root root = criteria.from( Course.class ); + Join students = root.join( "students", JoinType.INNER ); + Join student = students.join( "student", JoinType.INNER ); + criteria.where( criteriaBuilder.equal( student.get( "name" ), "Foo" ) ); + session.createQuery( criteria ).list(); +// Criteria criteria = s.createCriteria( Course.class ); +// criteria.createCriteria( "students" ).createCriteria( "student" ).add( Restrictions.eq( "name", "Foo" ) ); +// criteria.list(); - Criteria criteria = s.createCriteria( Course.class ); - criteria.createCriteria( "students" ).createCriteria( "student" ).add( Restrictions.eq( "name", "Foo" ) ); - criteria.list(); +// CriteriaQuery criteria2 = criteriaBuilder.createQuery( Course.class ); - Criteria criteria2 = s.createCriteria( Course.class ); - criteria2.createAlias( "students", "cs" ); - criteria2.add( Restrictions.eq( "cs.value", "Bar" ) ); - criteria2.createAlias( "cs.student", "s" ); - criteria2.add( Restrictions.eq( "s.name", "Foo" ) ); - criteria2.list(); - - s.getTransaction().commit(); - s.close(); +// Criteria criteria2 = s.createCriteria( Course.class ); +// criteria2.createAlias( "students", "cs" ); +// criteria2.add( Restrictions.eq( "cs.value", "Bar" ) ); +// criteria2.createAlias( "cs.student", "s" ); +// criteria2.add( Restrictions.eq( "s.name", "Foo" ) ); +// criteria2.list(); + } ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOneMappedByTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOneMappedByTest.java index db6c732357..f5cc6d670f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOneMappedByTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOneMappedByTest.java @@ -8,8 +8,10 @@ package org.hibernate.test.annotations.onetoone; import java.util.concurrent.atomic.AtomicReference; import javax.persistence.PersistenceException; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; -import org.hibernate.criterion.Restrictions; import org.hibernate.id.IdentifierGenerationException; import org.hibernate.testing.TestForIssue; @@ -49,7 +51,7 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase { } @Test - public void testBidirAssignedId() throws Exception { + public void testBidirAssignedId() { doInHibernate( this::sessionFactory, session -> { PartyAffiliate affiliate = new PartyAffiliate(); affiliate.partyId = "id"; @@ -58,10 +60,16 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase { } ); doInHibernate( this::sessionFactory, session -> { - PartyAffiliate affiliate = (PartyAffiliate) session.createCriteria( - PartyAffiliate.class ) - .add( Restrictions.idEq( "id" ) ) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( PartyAffiliate.class ); + Root root = criteria.from( PartyAffiliate.class ); + criteria.where( criteriaBuilder.equal( root.get("partyId"), "id" ) ); + + PartyAffiliate affiliate = session.createQuery( criteria ).uniqueResult(); +// PartyAffiliate affiliate = (PartyAffiliate) session.createCriteria( +// PartyAffiliate.class ) +// .add( Restrictions.idEq( "id" ) ) +// .uniqueResult(); assertNotNull( affiliate ); assertEquals( "id", affiliate.partyId ); assertNull( affiliate.party ); @@ -79,7 +87,7 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase { } @Test - public void testBidirDefaultIdGenerator() throws Exception { + public void testBidirDefaultIdGenerator() { PersonAddress _personAddress = doInHibernate( this::sessionFactory, session -> { @@ -93,10 +101,15 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase { ); doInHibernate( this::sessionFactory, session -> { - PersonAddress personAddress = (PersonAddress) session.createCriteria( - PersonAddress.class ) - .add( Restrictions.idEq( _personAddress.getId() ) ) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( PersonAddress.class ); + Root root = criteria.from( PersonAddress.class ); + criteria.where( criteriaBuilder.equal( root.get("id"), _personAddress.getId()) ); + PersonAddress personAddress = session.createQuery( criteria ).uniqueResult(); +// PersonAddress personAddress = (PersonAddress) session.createCriteria( +// PersonAddress.class ) +// .add( Restrictions.idEq( _personAddress.getId() ) ) +// .uniqueResult(); assertNotNull( personAddress ); assertNull( personAddress.getPerson() ); } ); @@ -114,7 +127,7 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase { @Test @TestForIssue(jiraKey = "HHH-5757") - public void testBidirQueryEntityProperty() throws Exception { + public void testBidirQueryEntityProperty() { AtomicReference personHolder = new AtomicReference<>(); @@ -135,11 +148,16 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase { } ); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); doInHibernate( this::sessionFactory, session -> { - PersonAddress personAddress = (PersonAddress) session.createCriteria( - PersonAddress.class ) - .add( Restrictions.idEq( _personAddress.getId() ) ) - .uniqueResult(); + CriteriaQuery criteria = criteriaBuilder.createQuery( PersonAddress.class ); + Root root = criteria.from( PersonAddress.class ); + criteria.where( criteriaBuilder.equal( root.get("id"), _personAddress.getId()) ); + PersonAddress personAddress = session.createQuery( criteria ).uniqueResult(); +// PersonAddress personAddress = (PersonAddress) session.createCriteria( +// PersonAddress.class ) +// .add( Restrictions.idEq( _personAddress.getId() ) ) +// .uniqueResult(); assertNotNull( personAddress ); assertNotNull( personAddress.getPerson() ); } ); @@ -147,15 +165,20 @@ public class OptionalOneToOneMappedByTest extends BaseCoreFunctionalTestCase { doInHibernate( this::sessionFactory, session -> { Person person = personHolder.get(); // this call throws GenericJDBCException - PersonAddress personAddress = (PersonAddress) session.createQuery( + PersonAddress personAddress = session.createQuery( "select pa from PersonAddress pa where pa.person = :person", PersonAddress.class ) .setParameter( "person", person ) .getSingleResult(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + criteria.where( criteriaBuilder.equal( root.get("personAddress"), personAddress ) ); + + session.createQuery( criteria ).uniqueResult(); // the other way should also work - person = (Person) session.createCriteria( Person.class ) - .add( Restrictions.eq( "personAddress", personAddress ) ) - .uniqueResult(); +// person = (Person) session.createCriteria( Person.class ) +// .add( Restrictions.eq( "personAddress", personAddress ) ) +// .uniqueResult(); session.delete( personAddress ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOnePKJCTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOnePKJCTest.java index c895f37f79..3db7e2402e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOnePKJCTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/OptionalOneToOnePKJCTest.java @@ -7,10 +7,12 @@ package org.hibernate.test.annotations.onetoone; import javax.persistence.PersistenceException; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.criterion.Restrictions; import org.hibernate.id.IdentifierGenerationException; import org.hibernate.testing.TestForIssue; @@ -97,9 +99,15 @@ public class OptionalOneToOnePKJCTest extends BaseCoreFunctionalTestCase { s = openSession(); s.getTransaction().begin(); - owner = ( Owner ) s.createCriteria( Owner.class ) - .add( Restrictions.idEq( owner.getId() ) ) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Owner.class ); + Root root = criteria.from( Owner.class ); + criteria.where( criteriaBuilder.equal( root.get( "id" ), owner.getId() ) ); + + owner = s.createQuery( criteria ).uniqueResult(); +// owner = ( Owner ) s.createCriteria( Owner.class ) +// .add( Restrictions.idEq( owner.getId() ) ) +// .uniqueResult(); assertNotNull( owner ); assertNull( owner.getAddress() ); s.delete( owner ); @@ -127,9 +135,16 @@ public class OptionalOneToOnePKJCTest extends BaseCoreFunctionalTestCase { s = openSession(); s.getTransaction().begin(); - party = ( Party ) s.createCriteria( Party.class ) - .add( Restrictions.idEq( "id" ) ) - .uniqueResult(); + + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Party.class ); + Root root = criteria.from( Party.class ); + criteria.where( criteriaBuilder.equal( root.get( "partyId" ), "id" ) ); + + party = s.createQuery( criteria ).uniqueResult(); +// party = ( Party ) s.createCriteria( Party.class ) +// .add( Restrictions.idEq( "id" ) ) +// .uniqueResult(); assertNotNull( party ); assertEquals( "id", party.partyId ); assertNull( party.partyAffiliate ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java b/hibernate-core/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java index f365c6c45a..c84dbf7a5d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java @@ -9,11 +9,15 @@ package org.hibernate.test.collection.set; import java.util.HashSet; import java.util.Map; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + import org.hibernate.CacheMode; import org.hibernate.Session; import org.hibernate.cfg.AvailableSettings; import org.hibernate.collection.internal.PersistentSet; -import org.hibernate.criterion.Restrictions; +import org.hibernate.query.Query; import org.hibernate.stat.CollectionStatistics; import org.hibernate.testing.FailureExpected; @@ -146,7 +150,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session = openSession(); session.beginTransaction(); - parent = ( Parent ) session.get( Parent.class, "p1" ); + parent = session.get( Parent.class, "p1" ); assertEquals( 1, parent.getChildren().size() ); session.getTransaction().commit(); session.close(); @@ -243,7 +247,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session = openSession(); session.beginTransaction(); - container = ( Container ) session.get( Container.class, container.getId() ); + container = session.get( Container.class, container.getId() ); assertEquals( 1, container.getContents().size() ); session.delete( container ); session.getTransaction().commit(); @@ -268,7 +272,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session = openSession(); session.beginTransaction(); - container = ( Container ) session.get( Container.class, container.getId() ); + container = session.get( Container.class, container.getId() ); assertEquals( 1, container.getContents().size() ); session.getTransaction().commit(); session.close(); @@ -279,7 +283,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session = openSession(); session.beginTransaction(); - container = ( Container ) session.get( Container.class, container.getId() ); + container = session.get( Container.class, container.getId() ); assertEquals( 1, container.getContents().size() ); session.delete( container ); session.getTransaction().commit(); @@ -305,7 +309,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session = openSession(); session.beginTransaction(); - parent = ( Parent ) session.get( Parent.class, parent.getName() ); + parent = session.get( Parent.class, parent.getName() ); assertTrue( parent.getChildren().contains( child ) ); assertTrue( parent.getChildren().contains( otherChild ) ); session.getTransaction().commit(); @@ -313,22 +317,34 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session = openSession(); session.beginTransaction(); - child = ( Child ) session.get( Child.class, child.getName() ); + child = session.get( Child.class, child.getName() ); assertTrue( child.getParent().getChildren().contains( child ) ); session.clear(); - child = ( Child ) session.createCriteria( Child.class, child.getName() ) - .setCacheable( true ) - .add( Restrictions.idEq( "c1" ) ) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Child.class ); + Root root = criteria.from( Child.class ); + criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" )); + child = session.createQuery( criteria ).uniqueResult(); +// child = ( Child ) session.createCriteria( Child.class, child.getName() ) +// .setCacheable( true ) +// .add( Restrictions.idEq( "c1" ) ) +// .uniqueResult(); assertTrue( child.getParent().getChildren().contains( child ) ); assertTrue( child.getParent().getChildren().contains( otherChild ) ); session.clear(); - child = ( Child ) session.createCriteria( Child.class, child.getName() ) - .setCacheable( true ) - .add( Restrictions.idEq( "c1" ) ) - .uniqueResult(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery( Child.class ); + Root childRoot = criteriaQuery.from( Child.class ); + criteriaQuery.where( criteriaBuilder.equal( childRoot.get( "name" ), "c1" )); + Query query = session.createQuery( criteriaQuery ); + query.setCacheable( true ); + child = query.uniqueResult(); + +// child = ( Child ) session.createCriteria( Child.class, child.getName() ) +// .setCacheable( true ) +// .add( Restrictions.idEq( "c1" ) ) +// .uniqueResult(); assertTrue( child.getParent().getChildren().contains( child ) ); assertTrue( child.getParent().getChildren().contains( otherChild ) ); session.clear(); @@ -366,7 +382,7 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session = openSession(); session.beginTransaction(); session.setCacheMode( CacheMode.IGNORE ); - parent = ( Parent ) session.get( Parent.class, parent.getName() ); + parent = session.get( Parent.class, parent.getName() ); assertTrue( parent.getChildren().contains( child ) ); assertTrue( parent.getChildren().contains( otherChild ) ); session.getTransaction().commit(); @@ -375,13 +391,19 @@ public class PersistentSetTest extends BaseNonConfigCoreFunctionalTestCase { session.beginTransaction(); session.setCacheMode( CacheMode.IGNORE ); - child = ( Child ) session.get( Child.class, child.getName() ); + child = session.get( Child.class, child.getName() ); assertTrue( child.getParent().getChildren().contains( child ) ); session.clear(); - child = ( Child ) session.createCriteria( Child.class, child.getName() ) - .add( Restrictions.idEq( "c1" ) ) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Child.class ); + Root root = criteria.from( Child.class ); + criteria.where( criteriaBuilder.equal( root.get( "name" ), "c1" ) ); + + child = session.createQuery( criteria ).uniqueResult(); +// child = ( Child ) session.createCriteria( Child.class, child.getName() ) +// .add( Restrictions.idEq( "c1" ) ) +// .uniqueResult(); assertTrue( child.getParent().getChildren().contains( child ) ); assertTrue( child.getParent().getChildren().contains( otherChild ) ); session.clear(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/compositeelement/CompositeElementTest.java b/hibernate-core/src/test/java/org/hibernate/test/compositeelement/CompositeElementTest.java index 7723b45599..0b43b91b31 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/compositeelement/CompositeElementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/compositeelement/CompositeElementTest.java @@ -8,10 +8,13 @@ package org.hibernate.test.compositeelement; import java.util.ArrayList; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.boot.Metadata; -import org.hibernate.criterion.Restrictions; import org.hibernate.dialect.function.SQLFunction; import org.hibernate.mapping.Collection; import org.hibernate.mapping.Component; @@ -88,42 +91,51 @@ public class CompositeElementTest extends BaseNonConfigCoreFunctionalTestCase { @Test public void testCustomColumnReadAndWrite() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Child c = new Child( "Child One" ); - c.setPosition( 1 ); - Parent p = new Parent( "Parent" ); - p.getChildren().add( c ); - c.setParent( p ); - s.save( p ); - s.flush(); + inTransaction( s -> { + Child c = new Child( "Child One" ); + c.setPosition( 1 ); + Parent p = new Parent( "Parent" ); + p.getChildren().add( c ); + c.setParent( p ); + s.save( p ); + s.flush(); - // Oracle returns BigDecimaal while other dialects return Integer; - // casting to Number so it works on all dialects - Number sqlValue = ((Number) s.createSQLQuery("select child_position from ParentChild c where c.name='Child One'") - .uniqueResult()); - assertEquals( 0, sqlValue.intValue() ); + // Oracle returns BigDecimaal while other dialects return Integer; + // casting to Number so it works on all dialects + Number sqlValue = ( (Number) s.createNativeQuery( + "select child_position from ParentChild c where c.name='Child One'" ) + .uniqueResult() ); + assertEquals( 0, sqlValue.intValue() ); - Integer hqlValue = (Integer)s.createQuery("select c.position from Parent p join p.children c where p.name='Parent'") - .uniqueResult(); - assertEquals( 1, hqlValue.intValue() ); + Integer hqlValue = (Integer) s.createQuery( + "select c.position from Parent p join p.children c where p.name='Parent'" ) + .uniqueResult(); + assertEquals( 1, hqlValue.intValue() ); - p = (Parent)s.createCriteria(Parent.class).add(Restrictions.eq("name", "Parent")).uniqueResult(); - c = (Child)p.getChildren().iterator().next(); - assertEquals( 1, c.getPosition() ); +// p = (Parent) s.createCriteria( Parent.class ).add( Restrictions.eq( "name", "Parent" ) ).uniqueResult(); - p = (Parent)s.createQuery("from Parent p join p.children c where c.position = 1").uniqueResult(); - c = (Child)p.getChildren().iterator().next(); - assertEquals( 1, c.getPosition() ); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Parent.class ); + Root root = criteria.from( Parent.class ); + criteria.where( criteriaBuilder.equal( root.get( "name" ),"Parent" ) ); - c.setPosition( 2 ); - s.flush(); - sqlValue = ( (Number) s.createSQLQuery("select child_position from ParentChild c where c.name='Child One'") - .uniqueResult() ); - assertEquals( 1, sqlValue.intValue() ); - s.delete( p ); - t.commit(); - s.close(); + p = s.createQuery( criteria ).uniqueResult(); + + c = (Child) p.getChildren().iterator().next(); + assertEquals( 1, c.getPosition() ); + + p = (Parent) s.createQuery( "from Parent p join p.children c where c.position = 1" ).uniqueResult(); + c = (Child) p.getChildren().iterator().next(); + assertEquals( 1, c.getPosition() ); + + c.setPosition( 2 ); + s.flush(); + sqlValue = ( (Number) s.createNativeQuery( + "select child_position from ParentChild c where c.name='Child One'" ) + .uniqueResult() ); + assertEquals( 1, sqlValue.intValue() ); + s.delete( p ); + } ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/fetchprofiles/join/selfReferencing/JoinSelfReferentialFetchProfileTest.java b/hibernate-core/src/test/java/org/hibernate/test/fetchprofiles/join/selfReferencing/JoinSelfReferentialFetchProfileTest.java index 804f16d7b9..c773475f1a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/fetchprofiles/join/selfReferencing/JoinSelfReferentialFetchProfileTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/fetchprofiles/join/selfReferencing/JoinSelfReferentialFetchProfileTest.java @@ -6,8 +6,9 @@ */ package org.hibernate.test.fetchprofiles.join.selfReferencing; -import org.hibernate.Session; -import org.hibernate.criterion.Restrictions; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; import org.junit.Test; @@ -24,13 +25,16 @@ public class JoinSelfReferentialFetchProfileTest extends BaseCoreFunctionalTestC @Test public void testEnablingJoinFetchProfileAgainstSelfReferentialAssociation() { - Session s = openSession(); - s.beginTransaction(); - s.enableFetchProfile( Employee.FETCH_PROFILE_TREE ); - s.createCriteria( Employee.class ) - .add( Restrictions.isNull( "manager" ) ) - .list(); - s.getTransaction().commit(); - s.close(); + inTransaction( s-> { + s.enableFetchProfile( Employee.FETCH_PROFILE_TREE ); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Employee.class ); + Root root = criteria.from( Employee.class ); + criteria.where( criteriaBuilder.isNull( root.get( "manager" ) ) ); + s.createQuery( criteria ).list(); +// s.createCriteria( Employee.class ) +// .add( Restrictions.isNull( "manager" ) ) +// .list(); + } ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java index 563ec5d022..3bd4b084fb 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/joinedsubclass/JoinedSubclassTest.java @@ -5,18 +5,24 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.test.joinedsubclass; + import java.math.BigDecimal; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + import org.junit.Test; import org.hibernate.Hibernate; import org.hibernate.LockMode; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.criterion.Property; -import org.hibernate.criterion.Restrictions; +import org.hibernate.query.Query; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; @@ -37,77 +43,94 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { @Test public void testJoinedSubclass() { - Session s = openSession(); - Transaction t = s.beginTransaction(); + 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"); + 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); + 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'); + Person yomomma = new Person(); + yomomma.setName( "mum" ); + yomomma.setSex( 'F' ); - s.save(yomomma); - s.save(mark); - s.save(joe); + s.save( yomomma ); + s.save( mark ); + s.save( joe ); - assertEquals( s.createQuery("from java.io.Serializable").list().size(), 0 ); + assertEquals( s.createQuery( "from java.io.Serializable" ).list().size(), 0 ); - assertEquals( s.createQuery("from Person").list().size(), 3 ); - assertEquals( s.createQuery("from Person 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(); + 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 ( Iterator iter = customers.iterator(); iter.hasNext(); ) { - Customer c = (Customer) iter.next(); - assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); - assertEquals( c.getSalesperson().getName(), "Mark" ); - } - assertEquals( customers.size(), 1 ); - s.clear(); + List customers = s.createQuery( "from Customer c left join fetch c.salesperson" ).list(); + for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { + Customer c = (Customer) iter.next(); + assertTrue( Hibernate.isInitialized( c.getSalesperson() ) ); + assertEquals( c.getSalesperson().getName(), "Mark" ); + } + assertEquals( customers.size(), 1 ); + s.clear(); - customers = s.createQuery("from Customer").list(); - for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { - Customer c = (Customer) iter.next(); - assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); - assertEquals( c.getSalesperson().getName(), "Mark" ); - } - assertEquals( customers.size(), 1 ); - s.clear(); + customers = s.createQuery( "from Customer" ).list(); + for ( Iterator iter = customers.iterator(); iter.hasNext(); ) { + Customer c = (Customer) iter.next(); + assertFalse( Hibernate.isInitialized( c.getSalesperson() ) ); + assertEquals( c.getSalesperson().getName(), "Mark" ); + } + assertEquals( customers.size(), 1 ); + s.clear(); - mark = (Employee) s.get( Employee.class, new Long( mark.getId() ) ); - joe = (Customer) s.get( Customer.class, new Long( joe.getId() ) ); + mark = (Employee) s.get( Employee.class, new Long( mark.getId() ) ); + joe = (Customer) s.get( Customer.class, new Long( joe.getId() ) ); - mark.setZip("30306"); - assertEquals( s.createQuery("from Person p where p.address.zip = '30306'" ).list().size(),1 ); + mark.setZip( "30306" ); + assertEquals( s.createQuery( "from Person p where p.address.zip = '30306'" ).list().size(), 1 ); - s.createCriteria( Person.class ).add( - Restrictions.in( "address", mark.getAddress(), joe.getAddress() ) ).list(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + CriteriaBuilder.In
in = criteriaBuilder.in( root.get( "address" ) ); + in.value( mark.getAddress() ); + in.value( joe.getAddress() ); - s.delete(mark); - s.delete(joe); - s.delete(yomomma); - assertTrue( s.createQuery("from Person").list().isEmpty() ); - t.commit(); - s.close(); + criteria.where( in ); + Query query = s.createQuery( criteria ); + query.list(); +// s.createCriteria( Person.class ).add( +// Restrictions.in( "address", mark.getAddress(), joe.getAddress() ) ).list(); + + s.delete( mark ); + s.delete( joe ); + s.delete( yomomma ); + assertTrue( s.createQuery( "from Person" ).list().isEmpty() ); + } ); } @Test @@ -124,15 +147,15 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { s = openSession(); s.beginTransaction(); - Customer c = ( Customer ) s.get( Customer.class, new Long( e.getId() ) ); + Customer c = s.get( Customer.class, new Long( e.getId() ) ); s.getTransaction().commit(); s.close(); assertNull( c ); s = openSession(); s.beginTransaction(); - e = ( Employee ) s.get( Employee.class, new Long( e.getId() ) ); - c = ( Customer ) s.get( Customer.class, new Long( e.getId() ) ); + e = s.get( Employee.class, new Long( e.getId() ) ); + c = s.get( Customer.class, new Long( e.getId() ) ); s.getTransaction().commit(); s.close(); assertNotNull( e ); @@ -147,41 +170,47 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { @Test public void testQuerySubclassAttribute() { - 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); + 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 ); - List result = s.createQuery("from Person where salary > 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("from Person where salary > 100 or name like 'E%'").list(); - assertEquals( result.size(), 2 ); + result = s.createQuery( "from Person where salary > 100 or name like 'E%'" ).list(); + assertEquals( result.size(), 2 ); - result = s.createCriteria(Person.class) - .add( Property.forName("salary").gt( new BigDecimal(100) ) ) - .list(); - assertEquals( result.size(), 1 ); - assertSame( result.get(0), q ); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); - //TODO: make this work: + 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 ); + + //TODO: make this work: /*result = s.createQuery("select salary from Person where salary > 100").list(); assertEquals( result.size(), 1 ); assertEquals( result.get(0), new BigDecimal(1000) );*/ - s.delete(p); - s.delete(q); - t.commit(); - s.close(); + s.delete( p ); + s.delete( q ); + } ); } @Test @@ -191,81 +220,100 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { 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); + 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); + 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 JPerson where name='Emmanuel'").uniqueResult() ) + ( (Number) s.createNativeQuery( "select height_centimeters from JPerson where name='Emmanuel'" ) + .uniqueResult() ) .doubleValue(); - assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); + assertEquals( HEIGHT_CENTIMETERS, heightViaSql, 0.01d ); Double expiryViaSql = - ( (Number)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?") - .setLong(1, e.getId()) + ( (Number) s.createNativeQuery( "select pwd_expiry_weeks from JEmployee where person_id=?" ) + .setParameter( 1, e.getId() ) .uniqueResult() ).doubleValue(); - assertEquals(PASSWORD_EXPIRY_WEEKS, expiryViaSql, 0.01d); - + 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); - + 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); - +// p = (Person)s.createCriteria(Person.class) +// .add(Restrictions.between("heightInches", HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d)) +// .uniqueResult(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + Root root = criteria.from( Person.class ); + criteria.where( criteriaBuilder.between( root.get("heightInches"),HEIGHT_INCHES - 0.01d, HEIGHT_INCHES + 0.01d ) ) ; + p = s.createQuery( criteria).uniqueResult(); + + assertEquals( HEIGHT_INCHES, p.getHeightInches(), 0.01d ); + CriteriaQuery employeeCriteriaQuery = criteriaBuilder.createQuery( Employee.class ); + Root employeeRoot = employeeCriteriaQuery.from( Employee.class ); + employeeCriteriaQuery.where( criteriaBuilder.between( employeeRoot.get("passwordExpiryDays"), PASSWORD_EXPIRY_DAYS - 0.01d, + PASSWORD_EXPIRY_DAYS + 0.01d ) ); + e = s.createQuery( employeeCriteriaQuery ).uniqueResult(); +// e = (Employee) s.createCriteria( Employee.class ) +// .add( Restrictions.between( +// "passwordExpiryDays", +// PASSWORD_EXPIRY_DAYS - 0.01d, +// PASSWORD_EXPIRY_DAYS + 0.01d +// ) ) +// .uniqueResult(); + assertEquals( PASSWORD_EXPIRY_DAYS, e.getPasswordExpiryDays(), 0.01d ); + // Test predicate and entity load via HQL - p = (Person)s.createQuery("from Person p where p.heightInches between ?1 and ?2") - .setDouble(1, HEIGHT_INCHES - 0.01d) - .setDouble(2, HEIGHT_INCHES + 0.01d) - .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); - + 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); + p.setHeightInches( 1 ); + e.setPasswordExpiryDays( 7 ); s.flush(); heightViaSql = - ( (Number)s.createSQLQuery("select height_centimeters from JPerson where name='Emmanuel'").uniqueResult() ) + ( (Number) s.createNativeQuery( "select height_centimeters from JPerson where name='Emmanuel'" ) + .uniqueResult() ) .doubleValue(); - assertEquals(2.54d, heightViaSql, 0.01d); + assertEquals( 2.54d, heightViaSql, 0.01d ); expiryViaSql = - ( (Number)s.createSQLQuery("select pwd_expiry_weeks from JEmployee where person_id=?") - .setLong(1, e.getId()) + ( (Number) s.createNativeQuery( "select pwd_expiry_weeks from JEmployee where person_id=?" ) + .setParameter( 1, e.getId() ) .uniqueResult() ).doubleValue(); - assertEquals(1d, expiryViaSql, 0.01d); - s.delete(p); - s.delete(e); + assertEquals( 1d, expiryViaSql, 0.01d ); + s.delete( p ); + s.delete( e ); t.commit(); s.close(); - + } @Test @@ -273,15 +321,15 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { Session s = openSession(); Transaction t = s.beginTransaction(); Person p = new Person(); - p.setName("Emmanuel"); - p.setSex('M'); - s.persist(p); + 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); + q.setName( "Steve" ); + q.setSex( 'M' ); + q.setTitle( "Mr" ); + q.setSalary( new BigDecimal( 1000 ) ); + s.persist( q ); t.commit(); s.close(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java b/hibernate-core/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java index 89d40897f4..0fa0077b81 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/keymanytoone/bidir/embedded/KeyManyToOneTest.java @@ -7,12 +7,17 @@ package org.hibernate.test.keymanytoone.bidir.embedded; 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.Test; import org.hibernate.Session; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; -import org.hibernate.criterion.Restrictions; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; @@ -35,59 +40,55 @@ public class KeyManyToOneTest extends BaseCoreFunctionalTestCase { @Test public void testCriteriaRestrictionOnKeyManyToOne() { - Session s = openSession(); - s.beginTransaction(); - s.createQuery( "from Order o where o.customer.name = 'Acme'" ).list(); - Criteria criteria = s.createCriteria( Order.class ); - criteria.createCriteria( "customer" ).add( Restrictions.eq( "name", "Acme" ) ); - criteria.list(); - s.getTransaction().commit(); - s.close(); + inTransaction( s -> { + s.createQuery( "from Order o where o.customer.name = 'Acme'" ).list(); +// Criteria criteria = s.createCriteria( Order.class ); +// criteria.createCriteria( "customer" ).add( Restrictions.eq( "name", "Acme" ) ); +// criteria.list(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Order.class ); + Root root = criteria.from( Order.class ); + Join customer = root.join( "customer", JoinType.INNER ); + criteria.where( criteriaBuilder.equal( customer.get( "name" ), "Acme" ) ); + s.createQuery( criteria ).list(); + } ); } @Test public void testSaveCascadedToKeyManyToOne() { // test cascading a save to an association with a key-many-to-one which refers to a // just saved entity - Session s = openSession(); - s.beginTransaction(); - Customer cust = new Customer( "Acme, Inc." ); - Order order = new Order( cust, 1 ); - cust.getOrders().add( order ); - sessionFactory().getStatistics().clear(); - s.save( cust ); - s.flush(); - assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() ); - s.delete( cust ); - s.getTransaction().commit(); - s.close(); + inTransaction( s -> { + Customer cust = new Customer( "Acme, Inc." ); + Order order = new Order( cust, 1 ); + cust.getOrders().add( order ); + sessionFactory().getStatistics().clear(); + s.save( cust ); + s.flush(); + assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() ); + s.delete( cust ); + } ); } @Test public void testQueryingOnMany2One() { - Session s = openSession(); - s.beginTransaction(); Customer cust = new Customer( "Acme, Inc." ); Order order = new Order( cust, 1 ); - cust.getOrders().add( order ); - s.save( cust ); - s.getTransaction().commit(); - s.close(); + inTransaction( s -> { + cust.getOrders().add( order ); + s.save( cust ); + } ); - s = openSession(); - s.beginTransaction(); - List results = s.createQuery( "from Order o where o.customer.name = :name" ) - .setParameter( "name", cust.getName() ) - .list(); - assertEquals( 1, results.size() ); - s.getTransaction().commit(); - s.close(); + inTransaction( s -> { + List results = s.createQuery( "from Order o where o.customer.name = :name" ) + .setParameter( "name", cust.getName() ) + .list(); + assertEquals( 1, results.size() ); + } ); - s = openSession(); - s.beginTransaction(); - s.delete( cust ); - s.getTransaction().commit(); - s.close(); + inTransaction( s -> { + s.delete( cust ); + } ); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/query/criteria/internal/AliasWithCriterionTest.java b/hibernate-core/src/test_legacy/org/hibernate/query/criteria/internal/AliasWithCriterionTest.java similarity index 100% rename from hibernate-core/src/test/java/org/hibernate/query/criteria/internal/AliasWithCriterionTest.java rename to hibernate-core/src/test_legacy/org/hibernate/query/criteria/internal/AliasWithCriterionTest.java