diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AllowableParameterType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AllowableParameterType.java index e8aa6523f1..3639f3d8aa 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AllowableParameterType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/AllowableParameterType.java @@ -11,6 +11,7 @@ import org.hibernate.NotYetImplementedFor6Exception; import org.hibernate.metamodel.model.mapping.spi.Writeable; import org.hibernate.query.Query; import org.hibernate.query.sqm.SqmExpressable; +import org.hibernate.type.Type; import org.hibernate.type.descriptor.java.JavaTypeDescriptor; /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BasicTypeImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BasicTypeImpl.java index a79074de27..154a3f1761 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BasicTypeImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/BasicTypeImpl.java @@ -61,4 +61,6 @@ public class BasicTypeImpl implements BasicDomainType, Serializable { throws SQLException { throw new NotYetImplementedFor6Exception( getClass() ); } + + } diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/paths/PluralAttributeExpressionsTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/paths/PluralAttributeExpressionsTest.java index 3b769ac0ce..6bda5a90d6 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/paths/PluralAttributeExpressionsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/jpa/test/criteria/paths/PluralAttributeExpressionsTest.java @@ -9,7 +9,10 @@ package org.hibernate.jpa.test.criteria.paths; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; +import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Expression; import javax.persistence.criteria.Root; import org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest; @@ -22,6 +25,7 @@ import org.hibernate.jpa.test.metamodel.MapEntityLocal; import org.hibernate.jpa.test.metamodel.MapEntity_; import org.hibernate.jpa.test.metamodel.Translation; import org.hibernate.query.criteria.HibernateCriteriaBuilder; +import org.hibernate.query.criteria.JpaExpression; import org.hibernate.testing.FailureExpected; import org.hibernate.testing.TestForIssue; @@ -90,7 +94,7 @@ public class PluralAttributeExpressionsTest extends AbstractMetamodelSpecificTes final Root root = criteria.from( MapEntity.class); criteria.select( root ) - .where( cb.isMapEmpty( root.get( MapEntity_.localized ) ) ); + .where( cb.isMapEmpty( (JpaExpression) root.get( MapEntity_.localized ) ) ); entityManager.createQuery( criteria ).getResultList(); }); @@ -164,7 +168,7 @@ public class PluralAttributeExpressionsTest extends AbstractMetamodelSpecificTes final Root root = criteria.from( MapEntity.class); criteria.select( root ) - .where( cb.gt( cb.mapSize( root.get( MapEntity_.localized ) ), 1 ) ); + .where( cb.gt( cb.mapSize( (JpaExpression) root.get( MapEntity_.localized ) ), 1 ) ); entityManager.createQuery( criteria ).getResultList(); }); @@ -188,7 +192,7 @@ public class PluralAttributeExpressionsTest extends AbstractMetamodelSpecificTes final Root
root = criteria.from( Article.class); criteria.select( root ) - .where( cb.gt( cb.mapSize( root.get( Article_.translations ) ), 1 ) ); + .where( cb.gt( cb.mapSize( (JpaExpression) root.get( Article_.translations ) ), 1 ) ); entityManager.createQuery( criteria ).getResultList(); }); diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/join/HHH3949Test.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/join/HHH3949Test.java index e39604755e..80244c1ea9 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/join/HHH3949Test.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/join/HHH3949Test.java @@ -20,6 +20,9 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.JoinType; import java.util.List; import static org.hibernate.Hibernate.isInitialized; @@ -93,7 +96,11 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { @SuppressWarnings( "unchecked" ) public void test3() { doInHibernate( this::sessionFactory, s -> { - List persons = (List) s.createCriteria( Person.class ).setFetchMode( "vehicle", FetchMode.JOIN ).list(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + criteria.from( Person.class ).fetch( "vehicle", JoinType.LEFT ); + List persons = s.createQuery( criteria ).list(); +// List persons = (List) s.createCriteria( Person.class ).setFetchMode( "vehicle", FetchMode.JOIN ).list(); for ( Person person : persons ) { if ( shouldHaveVehicle( person ) ) { assertNotNull( person.getVehicle() ); @@ -109,7 +116,11 @@ public class HHH3949Test extends BaseCoreFunctionalTestCase { List vehicles; try ( Session s = openSession() ) { - vehicles = (List) s.createCriteria( Vehicle.class ).setFetchMode( "driver", FetchMode.JOIN ).list(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Vehicle.class ); + criteria.from( Person.class ).fetch( "driver", JoinType.LEFT ); + vehicles = s.createQuery( criteria ).list(); +// vehicles = (List) s.createCriteria( Vehicle.class ).setFetchMode( "driver", FetchMode.JOIN ).list(); } for ( Vehicle vehicle : vehicles ) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java index 644b92fc21..9d7993aedf 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/lazyCache/InitFromCacheTest.java @@ -15,6 +15,8 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; @@ -41,7 +43,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; /** - * @author Steve Ebersole + * @author Steve EbersolenPropertyRefTest */ @RunWith( BytecodeEnhancerRunner.class ) public class InitFromCacheTest extends BaseCoreFunctionalTestCase { @@ -99,7 +101,11 @@ public class InitFromCacheTest extends BaseCoreFunctionalTestCase { sessionFactory().getStatistics().clear(); doInHibernate( this::sessionFactory, s -> { - Document d = (Document) s.createCriteria( Document.class ).uniqueResult(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Document.class ); + criteria.from( Document.class ); + Document d = s.createQuery( criteria ).uniqueResult(); +// Document d = (Document) s.createCriteria( Document.class ).uniqueResult(); assertFalse( isPropertyInitialized( d, "text" ) ); assertFalse( isPropertyInitialized( d, "summary" ) ); assertEquals( "Hibernate is....", d.text ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/converter/ConverterTest.java b/hibernate-core/src/test/java/org/hibernate/test/converter/ConverterTest.java index 6340d98082..6adda87e0c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/converter/ConverterTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/converter/ConverterTest.java @@ -13,6 +13,7 @@ import javax.persistence.Id; import org.hibernate.SessionFactory; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.metamodel.model.domain.AllowableParameterType; import org.hibernate.metamodel.spi.MetamodelImplementor; import org.hibernate.query.Query; import org.hibernate.type.Type; @@ -80,7 +81,7 @@ public class ConverterTest extends BaseEntityManagerFunctionalTestCase { "from Photo p " + "where upper(caption) = upper(:caption) ", Photo.class ) .unwrap( Query.class ) - .setParameter( "caption", new Caption("Nicolae Grigorescu"), captionType) + .setParameter( "caption", new Caption("Nicolae Grigorescu"), (AllowableParameterType) captionType ) .getSingleResult(); //end::basic-attribute-converter-query-parameter-converter-object-example[] diff --git a/hibernate-core/src/test/java/org/hibernate/test/ecid/CompositeIdAssociationsWithEmbeddedCompositeIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/ecid/CompositeIdAssociationsWithEmbeddedCompositeIdTest.java index a9151193e3..9850c51dc0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/ecid/CompositeIdAssociationsWithEmbeddedCompositeIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/ecid/CompositeIdAssociationsWithEmbeddedCompositeIdTest.java @@ -65,7 +65,7 @@ public class CompositeIdAssociationsWithEmbeddedCompositeIdTest extends BaseCore doInHibernate( this::sessionFactory, session -> { - Iterator iterator = session.createQuery( "from Person p", Person.class ).iterate(); + Iterator iterator = session.createQuery( "from Person p", Person.class ).list().iterator(); assertTrue( iterator.hasNext() ); Person p = iterator.next(); checkResult( p ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/event/collection/AbstractCollectionEventTest.java b/hibernate-core/src/test/java/org/hibernate/test/event/collection/AbstractCollectionEventTest.java index 1088fdba4d..ba56aa559a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/event/collection/AbstractCollectionEventTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/event/collection/AbstractCollectionEventTest.java @@ -10,6 +10,9 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.collection.internal.PersistentSet; @@ -35,20 +38,28 @@ public abstract class AbstractCollectionEventTest extends BaseCoreFunctionalTest ParentWithCollection dummyParent = createParent( "dummyParent" ); dummyParent.newChildren( createCollection() ); Child dummyChild = dummyParent.addChild( "dummyChild" ); - Session s = openSession(); - Transaction tx = s.beginTransaction(); - List children = s.createCriteria( dummyChild.getClass() ).list(); - List parents = s.createCriteria( dummyParent.getClass() ).list(); - for ( Iterator it = parents.iterator(); it.hasNext(); ) { - ParentWithCollection parent = ( ParentWithCollection ) it.next(); - parent.clearChildren(); - s.delete( parent ); - } - for ( Iterator it = children.iterator(); it.hasNext(); ) { - s.delete( it.next() ); - } - tx.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery childrenCriteria = criteriaBuilder.createQuery( dummyChild.getClass() ); + childrenCriteria.from( dummyChild.getClass() ); + List children = s.createQuery( childrenCriteria ).list(); +// List children = s.createCriteria( dummyChild.getClass() ).list(); + + CriteriaQuery parentsCriteria = criteriaBuilder.createQuery( dummyParent.getClass() ); + childrenCriteria.from( dummyParent.getClass() ); + List parents = s.createQuery( parentsCriteria ).list(); +// List parents = s.createCriteria( dummyParent.getClass() ).list(); + for ( Iterator it = parents.iterator(); it.hasNext(); ) { + ParentWithCollection parent = ( ParentWithCollection ) it.next(); + parent.clearChildren(); + s.delete( parent ); + } + for ( Iterator it = children.iterator(); it.hasNext(); ) { + s.delete( it.next() ); + } + } + ); } public abstract ParentWithCollection createParent(String name); diff --git a/hibernate-core/src/test/java/org/hibernate/test/event/collection/BrokenCollectionEventTest.java b/hibernate-core/src/test/java/org/hibernate/test/event/collection/BrokenCollectionEventTest.java index 305e4c203f..49547799d3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/event/collection/BrokenCollectionEventTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/event/collection/BrokenCollectionEventTest.java @@ -10,6 +10,9 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.junit.Test; import org.hibernate.Session; @@ -46,20 +49,30 @@ public class BrokenCollectionEventTest extends BaseCoreFunctionalTestCase { ParentWithCollection dummyParent = createParent( "dummyParent" ); dummyParent.setChildren( createCollection() ); Child dummyChild = dummyParent.addChild( "dummyChild" ); - Session s = openSession(); - Transaction tx = s.beginTransaction(); - List children = s.createCriteria( dummyChild.getClass() ).list(); - List parents = s.createCriteria( dummyParent.getClass() ).list(); - for ( Iterator it = parents.iterator(); it.hasNext(); ) { - ParentWithCollection parent = ( ParentWithCollection ) it.next(); - parent.clearChildren(); - s.delete( parent ); - } - for ( Iterator it = children.iterator(); it.hasNext(); ) { - s.delete( it.next() ); - } - tx.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery childrenCriteria = criteriaBuilder.createQuery( dummyChild.getClass() ); + childrenCriteria.from( dummyChild.getClass() ); + List children = s.createQuery( childrenCriteria ).list(); +// List children = s.createCriteria( dummyChild.getClass() ).list(); + + CriteriaQuery parentsCriteria = criteriaBuilder.createQuery( dummyParent.getClass() ); + childrenCriteria.from( dummyParent.getClass() ); + List parents = s.createQuery( parentsCriteria ).list(); + +// List children = s.createCriteria( dummyChild.getClass() ).list(); +// List parents = s.createCriteria( dummyParent.getClass() ).list(); + for ( Iterator it = parents.iterator(); it.hasNext(); ) { + ParentWithCollection parent = ( ParentWithCollection ) it.next(); + parent.clearChildren(); + s.delete( parent ); + } + for ( Iterator it = children.iterator(); it.hasNext(); ) { + s.delete( it.next() ); + } + } + ); } public ParentWithCollection createParent(String name) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/idbag/IdBagTest.java b/hibernate-core/src/test/java/org/hibernate/test/idbag/IdBagTest.java index 55cd3449aa..29ea279db4 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/idbag/IdBagTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/idbag/IdBagTest.java @@ -5,15 +5,20 @@ * See the lgpl.txt file in the root directory or . */ package org.hibernate.test.idbag; + import java.sql.SQLException; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.junit.Test; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; @@ -29,72 +34,75 @@ public class IdBagTest extends BaseCoreFunctionalTestCase { } @Test - public void testUpdateIdBag() throws HibernateException, SQLException { - Session s = openSession(); - Transaction t = s.beginTransaction(); - User gavin = new User("gavin"); - Group admins = new Group("admins"); - Group plebs = new Group("plebs"); - Group moderators = new Group("moderators"); - Group banned = new Group("banned"); - gavin.getGroups().add(plebs); - //gavin.getGroups().add(moderators); - s.persist(gavin); - s.persist(plebs); - s.persist(admins); - s.persist(moderators); - s.persist(banned); - t.commit(); - s.close(); - - s = openSession(); - t = s.beginTransaction(); - gavin = (User) s.createCriteria(User.class).uniqueResult(); - admins = (Group) s.load(Group.class, "admins"); - plebs = (Group) s.load(Group.class, "plebs"); - banned = (Group) s.load(Group.class, "banned"); - gavin.getGroups().add(admins); - gavin.getGroups().remove(plebs); - //gavin.getGroups().add(banned); + public void testUpdateIdBag() throws HibernateException { + inTransaction( + s -> { + User gavin = new User( "gavin" ); + Group admins = new Group( "admins" ); + Group plebs = new Group( "plebs" ); + Group moderators = new Group( "moderators" ); + Group banned = new Group( "banned" ); + gavin.getGroups().add( plebs ); + //gavin.getGroups().add(moderators); + s.persist( gavin ); + s.persist( plebs ); + s.persist( admins ); + s.persist( moderators ); + s.persist( banned ); + } + ); - s.delete(plebs); - s.delete(banned); - s.delete(moderators); - s.delete(admins); - s.delete(gavin); - - t.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( User.class ); + criteria.from( User.class ); + User gavin = s.createQuery( criteria ).uniqueResult(); +// User gavin = (User) s.createCriteria( User.class ).uniqueResult(); + Group admins = s.load( Group.class, "admins" ); + Group plebs = s.load( Group.class, "plebs" ); + Group banned = s.load( Group.class, "banned" ); + gavin.getGroups().add( admins ); + gavin.getGroups().remove( plebs ); + //gavin.getGroups().add(banned); + + s.delete( plebs ); + s.delete( banned ); + s.delete( s.load( Group.class, "moderators" ) ); + s.delete( admins ); + s.delete( gavin ); + } + ); } @Test public void testJoin() throws HibernateException, SQLException { Session s = openSession(); Transaction t = s.beginTransaction(); - User gavin = new User("gavin"); - Group admins = new Group("admins"); - Group plebs = new Group("plebs"); - gavin.getGroups().add(plebs); - gavin.getGroups().add(admins); - s.persist(gavin); - s.persist(plebs); - s.persist(admins); - - List l = s.createQuery("from User u join u.groups g").list(); + User gavin = new User( "gavin" ); + Group admins = new Group( "admins" ); + Group plebs = new Group( "plebs" ); + gavin.getGroups().add( plebs ); + gavin.getGroups().add( admins ); + s.persist( gavin ); + s.persist( plebs ); + s.persist( admins ); + + List l = s.createQuery( "from User u join u.groups g" ).list(); assertEquals( l.size(), 2 ); s.clear(); - - gavin = (User) s.createQuery("from User u join fetch u.groups").uniqueResult(); + + gavin = (User) s.createQuery( "from User u join fetch u.groups" ).uniqueResult(); assertTrue( Hibernate.isInitialized( gavin.getGroups() ) ); assertEquals( gavin.getGroups().size(), 2 ); - assertEquals( ( (Group) gavin.getGroups().get(0) ).getName(), "admins" ); - - s.delete( gavin.getGroups().get(0) ); - s.delete( gavin.getGroups().get(1) ); - s.delete(gavin); - + assertEquals( ( (Group) gavin.getGroups().get( 0 ) ).getName(), "admins" ); + + s.delete( gavin.getGroups().get( 0 ) ); + s.delete( gavin.getGroups().get( 1 ) ); + s.delete( gavin ); + t.commit(); - s.close(); + s.close(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomPersister.java b/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomPersister.java index 9615248f4f..2f776dd106 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomPersister.java +++ b/hibernate-core/src/test/java/org/hibernate/test/legacy/CustomPersister.java @@ -31,6 +31,7 @@ import org.hibernate.engine.internal.MutableEntityEntryFactory; import org.hibernate.engine.internal.TwoPhaseLoad; import org.hibernate.engine.spi.CascadeStyle; import org.hibernate.engine.spi.EntityEntryFactory; +import org.hibernate.engine.spi.LoadQueryInfluencers; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.ValueInclusion; @@ -739,4 +740,19 @@ public class CustomPersister implements EntityPersister { public boolean canUseReferenceCacheEntries() { return false; //To change body of implemented methods use File | Settings | File Templates. } + + @Override + public boolean isAffectedByEntityGraph(LoadQueryInfluencers loadQueryInfluencers) { + return loadQueryInfluencers.getEffectiveEntityGraph().getGraph() != null; + } + + @Override + public boolean isAffectedByEnabledFetchProfiles(LoadQueryInfluencers loadQueryInfluencers) { + return false; + } + + @Override + public boolean isAffectedByEnabledFilters(LoadQueryInfluencers loadQueryInfluencers) { + return false; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/legacy/LimitExpressionTest.java b/hibernate-core/src/test/java/org/hibernate/test/legacy/LimitExpressionTest.java index 1e02db0ab8..8aa20d7b5a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/legacy/LimitExpressionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/legacy/LimitExpressionTest.java @@ -35,7 +35,8 @@ public class LimitExpressionTest extends BaseCoreFunctionalTestCase { TransactionUtil.doInHibernate( this::sessionFactory, s -> { Iterator iter = s.createQuery( "from Person p" ) .setMaxResults( 0 ) - .iterate(); + .list().iterator(); + int count = 0; while ( iter.hasNext() ) { iter.next(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/manytomany/ordered/OrderedManyToManyTest.java b/hibernate-core/src/test/java/org/hibernate/test/manytomany/ordered/OrderedManyToManyTest.java index c1b39795fd..70cb29bc1c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/manytomany/ordered/OrderedManyToManyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/manytomany/ordered/OrderedManyToManyTest.java @@ -7,11 +7,11 @@ package org.hibernate.test.manytomany.ordered; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.JoinType; -import org.hibernate.FetchMode; import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; @@ -38,89 +38,102 @@ public class OrderedManyToManyTest extends BaseCoreFunctionalTestCase { @Test public void testManyToManyOrdering() { - Session s = openSession(); - Transaction t = s.beginTransaction(); User gavin = new User( "gavin", "jboss" ); User steve = new User( "steve", "jboss" ); User max = new User( "max", "jboss" ); User emmanuel = new User( "emmanuel", "jboss" ); - s.persist( gavin ); - s.persist( steve ); - s.persist( max ); - s.persist( emmanuel ); Group hibernate = new Group( "hibernate", "jboss" ); - hibernate.addUser( gavin ); - hibernate.addUser( steve ); - hibernate.addUser( max ); - hibernate.addUser( emmanuel ); - s.persist( hibernate ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.persist( gavin ); + s.persist( steve ); + s.persist( max ); + s.persist( emmanuel ); + hibernate.addUser( gavin ); + hibernate.addUser( steve ); + hibernate.addUser( max ); + hibernate.addUser( emmanuel ); + s.persist( hibernate ); + } + ); // delayed collection load... - s = openSession(); - t = s.beginTransaction(); - hibernate = ( Group ) s.get( Group.class, hibernate.getId() ); - assertFalse( Hibernate.isInitialized( hibernate.getUsers() ) ); - assertEquals( 4, hibernate.getUsers().size() ); - assertOrdering( hibernate.getUsers() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Group h = s.get( Group.class, hibernate.getId() ); + assertFalse( Hibernate.isInitialized( h.getUsers() ) ); + assertEquals( 4, h.getUsers().size() ); + assertOrdering( h.getUsers() ); + } + ); // HQL (non eager) - s = openSession(); - t = s.beginTransaction(); - hibernate = ( Group ) s.createQuery( "from Group" ).uniqueResult(); - assertFalse( Hibernate.isInitialized( hibernate.getUsers() ) ); - assertEquals( 4, hibernate.getUsers().size() ); - assertOrdering( hibernate.getUsers() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Group h = (Group) s.createQuery( "from Group" ).uniqueResult(); + assertFalse( Hibernate.isInitialized( h.getUsers() ) ); + assertEquals( 4, h.getUsers().size() ); + assertOrdering( h.getUsers() ); + } + ); + // HQL (eager) - s = openSession(); - t = s.beginTransaction(); - hibernate = ( Group ) s.createQuery( "from Group g inner join fetch g.users" ).uniqueResult(); - assertTrue( Hibernate.isInitialized( hibernate.getUsers() ) ); - assertEquals( 4, hibernate.getUsers().size() ); - assertOrdering( hibernate.getUsers() ); - t.commit(); - s.close(); + inTransaction( + s -> { + Group h = ( Group ) s.createQuery( "from Group g inner join fetch g.users" ).uniqueResult(); + assertTrue( Hibernate.isInitialized( h.getUsers() ) ); + assertEquals( 4, h.getUsers().size() ); + assertOrdering( h.getUsers() ); + } + ); // criteria load (forced eager fetch) - s = openSession(); - t = s.beginTransaction(); - Criteria criteria = s.createCriteria( Group.class ); - criteria.setFetchMode( "users", FetchMode.JOIN ); - hibernate = ( Group ) criteria.uniqueResult(); - assertTrue( Hibernate.isInitialized( hibernate.getUsers() ) ); - assertEquals( 4, hibernate.getUsers().size() ); - assertOrdering( hibernate.getUsers() ); - t.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Group.class ); + criteria.from( Group.class ).fetch( "users", JoinType.LEFT ); + + Group h = s.createQuery( criteria ).uniqueResult(); +// Criteria criteria = s.createCriteria( Group.class ); +// criteria.setFetchMode( "users", FetchMode.JOIN ); +// hibernate = ( Group ) criteria.uniqueResult(); + assertTrue( Hibernate.isInitialized( h.getUsers() ) ); + assertEquals( 4, h.getUsers().size() ); + assertOrdering( h.getUsers() ); + } + ); + // criteria load (forced non eager fetch) - s = openSession(); - t = s.beginTransaction(); - criteria = s.createCriteria( Group.class ); - criteria.setFetchMode( "users", FetchMode.SELECT ); - hibernate = ( Group ) criteria.uniqueResult(); - assertFalse( Hibernate.isInitialized( hibernate.getUsers() ) ); - assertEquals( 4, hibernate.getUsers().size() ); - assertOrdering( hibernate.getUsers() ); - t.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Group.class ); + criteria.from( Group.class ).join( "users", JoinType.LEFT ); + + Group h = s.createQuery( criteria ).uniqueResult(); +// criteria = s.createCriteria( Group.class ); +// criteria.setFetchMode( "users", FetchMode.SELECT ); +// hibernate = ( Group ) criteria.uniqueResult(); + assertFalse( Hibernate.isInitialized( h.getUsers() ) ); + assertEquals( 4, h.getUsers().size() ); + assertOrdering( h.getUsers() ); + } + ); + // clean up - s = openSession(); - t = s.beginTransaction(); - s.delete( gavin ); - s.delete( steve ); - s.delete( max ); - s.delete( emmanuel ); - s.delete( hibernate ); - t.commit(); - s.close(); + inTransaction( + s -> { + s.delete( gavin ); + s.delete( steve ); + s.delete( max ); + s.delete( emmanuel ); + s.delete( hibernate ); + } + ); } private void assertOrdering(List users) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/ordered/OrderByTest.java b/hibernate-core/src/test/java/org/hibernate/test/ordered/OrderByTest.java index b0b43c9790..d464f9df9f 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/ordered/OrderByTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/ordered/OrderByTest.java @@ -7,14 +7,14 @@ package org.hibernate.test.ordered; import java.util.Iterator; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.JoinType; -import org.junit.Test; - -import org.hibernate.FetchMode; import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.Transaction; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -29,48 +29,55 @@ public class OrderByTest extends BaseCoreFunctionalTestCase { } @Test - @SuppressWarnings( {"unchecked"}) + @SuppressWarnings({ "unchecked" }) public void testOrderBy() { - Search s = new Search("Hibernate"); - s.getSearchResults().add("jboss.com"); - s.getSearchResults().add("hibernate.org"); - s.getSearchResults().add("HiA"); - - Session sess = openSession(); - Transaction tx = sess.beginTransaction(); - sess.persist(s); - sess.flush(); - - sess.clear(); - s = (Search) sess.createCriteria(Search.class).uniqueResult(); - assertFalse( Hibernate.isInitialized( s.getSearchResults() ) ); - Iterator iter = s.getSearchResults().iterator(); - assertEquals( iter.next(), "HiA" ); - assertEquals( iter.next(), "hibernate.org" ); - assertEquals( iter.next(), "jboss.com" ); - assertFalse( iter.hasNext() ); - - sess.clear(); - s = (Search) sess.createCriteria(Search.class) - .setFetchMode("searchResults", FetchMode.JOIN) - .uniqueResult(); - assertTrue( Hibernate.isInitialized( s.getSearchResults() ) ); - iter = s.getSearchResults().iterator(); - assertEquals( iter.next(), "HiA" ); - assertEquals( iter.next(), "hibernate.org" ); - assertEquals( iter.next(), "jboss.com" ); - assertFalse( iter.hasNext() ); - - sess.clear(); - s = (Search) sess.createQuery("from Search s left join fetch s.searchResults") - .uniqueResult(); - assertTrue( Hibernate.isInitialized( s.getSearchResults() ) ); - iter = s.getSearchResults().iterator(); - assertEquals( iter.next(), "HiA" ); - assertEquals( iter.next(), "hibernate.org" ); - assertEquals( iter.next(), "jboss.com" ); - assertFalse( iter.hasNext() ); - + + inTransaction( + sess -> { + Search s = new Search( "Hibernate" ); + s.getSearchResults().add( "jboss.com" ); + s.getSearchResults().add( "hibernate.org" ); + s.getSearchResults().add( "HiA" ); + sess.persist( s ); + sess.flush(); + + sess.clear(); + CriteriaBuilder criteriaBuilder = sess.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Search.class ); + criteria.from( Search.class ); + s = sess.createQuery( criteria ).uniqueResult(); +// s = (Search) sess.createCriteria( Search.class ).uniqueResult(); + assertFalse( Hibernate.isInitialized( s.getSearchResults() ) ); + Iterator iter = s.getSearchResults().iterator(); + assertEquals( iter.next(), "HiA" ); + assertEquals( iter.next(), "hibernate.org" ); + assertEquals( iter.next(), "jboss.com" ); + assertFalse( iter.hasNext() ); + + sess.clear(); + criteria = criteriaBuilder.createQuery( Search.class ); + criteria.from( Search.class ).fetch( "searchResults", JoinType.LEFT ); + s = sess.createQuery( criteria ).uniqueResult(); +// s = (Search) sess.createCriteria( Search.class ) +// .setFetchMode( "searchResults", FetchMode.JOIN ) +// .uniqueResult(); + assertTrue( Hibernate.isInitialized( s.getSearchResults() ) ); + iter = s.getSearchResults().iterator(); + assertEquals( iter.next(), "HiA" ); + assertEquals( iter.next(), "hibernate.org" ); + assertEquals( iter.next(), "jboss.com" ); + assertFalse( iter.hasNext() ); + + sess.clear(); + s = (Search) sess.createQuery( "from Search s left join fetch s.searchResults" ) + .uniqueResult(); + assertTrue( Hibernate.isInitialized( s.getSearchResults() ) ); + iter = s.getSearchResults().iterator(); + assertEquals( iter.next(), "HiA" ); + assertEquals( iter.next(), "hibernate.org" ); + assertEquals( iter.next(), "jboss.com" ); + assertFalse( iter.hasNext() ); + /*sess.clear(); s = (Search) sess.createCriteria(Search.class).uniqueResult(); assertFalse( Hibernate.isInitialized( s.getSearchResults() ) ); @@ -79,11 +86,10 @@ public class OrderByTest extends BaseCoreFunctionalTestCase { assertEquals( iter.next(), "hibernate.org" ); assertEquals( iter.next(), "jboss.com" ); assertFalse( iter.hasNext() );*/ - - sess.delete(s); - tx.commit(); - sess.close(); + + sess.delete( s ); + } + ); } } - diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java index 91cee79655..e1c7a79eb0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java @@ -6,16 +6,16 @@ */ package org.hibernate.test.orphan.manytomany; -import static org.junit.Assert.assertEquals; - import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; -import org.hibernate.Session; -import org.hibernate.Transaction; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { @Override @@ -26,56 +26,62 @@ public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { @Test @TestForIssue(jiraKey = "HHH-8749") public void testManyToManyWithCascadeDeleteOrphan() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - User bob = new User( "bob", "jboss" ); - Group seam = new Group( "seam", "jboss" ); - seam.setGroupType( 1 ); - Group hb = new Group( "hibernate", "jboss" ); - hb.setGroupType( 2 ); - bob.getGroups().put( seam.getGroupType(), seam ); - bob.getGroups().put( hb.getGroupType(), hb ); - s.persist( bob ); - s.persist( seam ); - s.persist( hb ); - t.commit(); - s.close(); + inTransaction( + s -> { + User bob = new User( "bob", "jboss" ); + Group seam = new Group( "seam", "jboss" ); + seam.setGroupType( 1 ); + Group hb = new Group( "hibernate", "jboss" ); + hb.setGroupType( 2 ); + bob.getGroups().put( seam.getGroupType(), seam ); + bob.getGroups().put( hb.getGroupType(), hb ); + s.persist( bob ); + s.persist( seam ); + s.persist( hb ); + } + ); - s = openSession(); - t = s.beginTransaction(); - bob = (User) s.get( User.class, "bob" ); - assertEquals( 2, bob.getGroups().size() ); - seam = (Group) s.get( Group.class, "seam" ); - assertEquals( (Integer) 1, seam.getGroupType() ); - hb = (Group) s.get( Group.class, "hibernate" ); - assertEquals( (Integer) 2, hb.getGroupType() ); - t.commit(); - s.close(); + inTransaction( + s -> { + User b = s.get( User.class, "bob" ); + assertEquals( 2, b.getGroups().size() ); + Group sG = s.get( Group.class, "seam" ); + assertEquals( (Integer) 1, sG.getGroupType() ); + Group hbG = s.get( Group.class, "hibernate" ); + assertEquals( (Integer) 2, hbG.getGroupType() ); + } + ); - s = openSession(); - t = s.beginTransaction(); - bob = (User) s.get( User.class, "bob" ); - assertEquals( 2, bob.getGroups().size() ); - hb = (Group) s.get( Group.class, "hibernate" ); - bob.getGroups().remove( hb.getGroupType() ); - assertEquals( 1, bob.getGroups().size() ); - t.commit(); - s.close(); + inTransaction( + s -> { + User b = s.get( User.class, "bob" ); + assertEquals( 2, b.getGroups().size() ); + Group hG = s.get( Group.class, "hibernate" ); + b.getGroups().remove( hG.getGroupType() ); + assertEquals( 1, b.getGroups().size() ); + } + ); - s = openSession(); - t = s.beginTransaction(); - bob = (User) s.get( User.class, "bob" ); - assertEquals( 1, bob.getGroups().size() ); - t.commit(); - s.close(); + inTransaction( + s -> { + User b = s.get( User.class, "bob" ); + assertEquals( 1, b.getGroups().size() ); + } + ); // Verify orphan group was deleted - s = openSession(); - t = s.beginTransaction(); - List groups = s.createCriteria( Group.class ).list(); - assertEquals( 1, groups.size() ); - assertEquals( "seam", groups.get( 0 ).getName() ); - t.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Group.class ); + criteria.from( Group.class ); + List groups = s.createQuery( criteria ).list(); + +// List groups = s.createCriteria( Group.class ).list(); + assertEquals( 1, groups.size() ); + assertEquals( "seam", groups.get( 0 ).getName() ); + } + ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/PropertyRefTest.java b/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/PropertyRefTest.java index b2da4dac64..bd44033639 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/PropertyRefTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/propertyref/basic/PropertyRefTest.java @@ -10,6 +10,9 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.hibernate.FetchMode; import org.hibernate.Hibernate; import org.hibernate.Session; @@ -208,48 +211,53 @@ public class PropertyRefTest extends BaseNonConfigCoreFunctionalTestCase { @Test public void testJoinFetchPropertyRef() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Person p = new Person(); - p.setName("Steve"); - p.setUserId("steve"); - Address a = new Address(); - a.setAddress("Texas"); - a.setCountry("USA"); - p.setAddress(a); - a.setPerson(p); - s.save(p); + inTransaction( + s -> { + Person p = new Person(); + p.setName("Steve"); + p.setUserId("steve"); + Address a = new Address(); + a.setAddress("Texas"); + a.setCountry("USA"); + p.setAddress(a); + a.setPerson(p); + s.save(p); - s.flush(); - s.clear(); + s.flush(); + s.clear(); - sessionFactory().getStatistics().clear(); + sessionFactory().getStatistics().clear(); - p = (Person) s.get( Person.class, p.getId() ); //get address reference by outer join - - assertTrue( Hibernate.isInitialized( p.getAddress() ) ); - assertNotNull( p.getAddress() ); - assertEquals( sessionFactory().getStatistics().getPrepareStatementCount(), 1 ); - assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 ); + p = s.get( Person.class, p.getId() ); //get address reference by outer join - s.clear(); + assertTrue( Hibernate.isInitialized( p.getAddress() ) ); + assertNotNull( p.getAddress() ); + assertEquals( sessionFactory().getStatistics().getPrepareStatementCount(), 1 ); + assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 ); - sessionFactory().getStatistics().clear(); + s.clear(); - p = (Person) s.createCriteria(Person.class) - .setFetchMode("address", FetchMode.SELECT) - .uniqueResult(); //get address reference by select - - assertTrue( Hibernate.isInitialized( p.getAddress() ) ); - assertNotNull( p.getAddress() ); - assertEquals( sessionFactory().getStatistics().getPrepareStatementCount(), 2 ); - assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 ); + sessionFactory().getStatistics().clear(); - s.createQuery("delete from Address").executeUpdate(); - s.createQuery("delete from Person").executeUpdate(); - - t.commit(); - s.close(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + criteria.from( Person.class ).join( "address" ); + + p = s.createQuery( criteria ).uniqueResult(); +// p = (Person) s.createCriteria(Person.class) +// .setFetchMode("address", FetchMode.SELECT) +// .uniqueResult(); //get address reference by select + + assertTrue( Hibernate.isInitialized( p.getAddress() ) ); + assertNotNull( p.getAddress() ); + assertEquals( sessionFactory().getStatistics().getPrepareStatementCount(), 2 ); + assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 ); + + s.createQuery("delete from Address").executeUpdate(); + s.createQuery("delete from Person").executeUpdate(); + } + + ); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/test/propertyref/inheritence/joined/JoinedSubclassPropertyRefTest.java b/hibernate-core/src/test/java/org/hibernate/test/propertyref/inheritence/joined/JoinedSubclassPropertyRefTest.java index 010e9bc655..3fdda8ef54 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/propertyref/inheritence/joined/JoinedSubclassPropertyRefTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/propertyref/inheritence/joined/JoinedSubclassPropertyRefTest.java @@ -6,13 +6,14 @@ */ package org.hibernate.test.propertyref.inheritence.joined; -import org.junit.Test; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.JoinType; -import org.hibernate.FetchMode; import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.Transaction; + import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -27,43 +28,45 @@ public class JoinedSubclassPropertyRefTest extends BaseCoreFunctionalTestCase { @Test public void testPropertyRefToJoinedSubclass() { - Session session = openSession(); - Transaction tx = session.beginTransaction(); - Person p = new Person(); - p.setName("Gavin King"); - BankAccount acc = new BankAccount(); - acc.setBsb("0634"); - acc.setType('B'); - acc.setAccountNumber("xxx-123-abc"); - p.setBankAccount(acc); - session.persist(p); - tx.commit(); - session.close(); + Person person = new Person(); + inTransaction( + session -> { + person.setName( "Gavin King" ); + BankAccount acc = new BankAccount(); + acc.setBsb( "0634" ); + acc.setType( 'B' ); + acc.setAccountNumber( "xxx-123-abc" ); + person.setBankAccount( acc ); + session.persist( person ); + } + ); - session = openSession(); - tx = session.beginTransaction(); - p = (Person) session.get(Person.class, p.getId()); - assertNotNull( p.getBankAccount() ); - assertTrue( Hibernate.isInitialized( p.getBankAccount() ) ); - tx.commit(); - session.close(); + inTransaction( + session -> { + Person p = session.get( Person.class, person.getId() ); + assertNotNull( p.getBankAccount() ); + assertTrue( Hibernate.isInitialized( p.getBankAccount() ) ); + } + ); - session = openSession(); - tx = session.beginTransaction(); - p = (Person) session.createCriteria(Person.class) - .setFetchMode("bankAccount", FetchMode.JOIN) - .uniqueResult(); - assertNotNull( p.getBankAccount() ); - assertTrue( Hibernate.isInitialized( p.getBankAccount() ) ); - tx.commit(); - session.close(); + inTransaction( + session -> { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + criteria.from( Person.class ).fetch( "bankAccount", JoinType.LEFT ); - session = openSession(); - tx = session.beginTransaction(); - session.delete(p); - tx.commit(); - session.close(); + Person p = session.createQuery( criteria ).uniqueResult(); +// Person p = (Person) session.createCriteria(Person.class) +// .setFetchMode("bankAccount", FetchMode.JOIN) +// .uniqueResult(); + assertNotNull( p.getBankAccount() ); + assertTrue( Hibernate.isInitialized( p.getBankAccount() ) ); + } + ); + + inTransaction( + session -> session.delete( person ) + ); } } - diff --git a/hibernate-core/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java b/hibernate-core/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java index 7b5e9de38c..36e5956ba3 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java @@ -17,6 +17,10 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + import org.hibernate.EmptyInterceptor; import org.hibernate.Hibernate; import org.hibernate.query.NativeQuery; @@ -594,11 +598,18 @@ public class QueryCacheTest extends BaseNonConfigCoreFunctionalTestCase { key.setSubstation( "foo4" ); entity.setPk( key ); s.persist( entity ); - Criteria c = s.createCriteria( - EntityWithStringCompositeKey.class ).add( Restrictions.eq( - "pk", key ) ); - c.setCacheable( true ); - assertEquals( 1, c.list().size() ); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( EntityWithStringCompositeKey.class ); + Root root = criteria.from( EntityWithStringCompositeKey.class ); + criteria.where( criteriaBuilder.equal( root.get( "pk" ), key ) ); + s.createQuery( criteria ).setCacheable( true ); + + assertEquals( 1, s.createQuery( criteria ).list().size() ); +// Criteria c = s.createCriteria( +// EntityWithStringCompositeKey.class ).add( Restrictions.eq( +// "pk", key ) ); +// c.setCacheable( true ); +// assertEquals( 1, c.list().size() ); s.getTransaction().rollback(); s.close(); } @@ -720,15 +731,22 @@ public class QueryCacheTest extends BaseNonConfigCoreFunctionalTestCase { protected Item findByDescription(SessionBuilder sessionBuilder, final String description) { Session s = sessionBuilder.openSession(); try { - return (Item) s.createCriteria(Item.class) - .setCacheable(true) - .setReadOnly(true) - .add(Restrictions.eq("description", description)) - .uniqueResult(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Item.class ); + Root root = criteria.from( Item.class ); + criteria.where( criteriaBuilder.equal( root.get( "description" ), description ) ); - } finally { - s.close(); - } + return s.createQuery( criteria ).setCacheable( true ).setReadOnly( true ).uniqueResult(); +// return (Item) s.createCriteria(Item.class) +// .setCacheable(true) +// .setReadOnly(true) +// .add(Restrictions.eq("description", description)) +// .uniqueResult(); + + } + finally { + s.close(); + } } public class DelayLoadOperations extends EmptyInterceptor { @@ -771,4 +789,3 @@ public class QueryCacheTest extends BaseNonConfigCoreFunctionalTestCase { } } } - diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java index a1a07128ab..1f996663f7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlySessionTest.java @@ -7,17 +7,16 @@ package org.hibernate.test.readonly; import java.math.BigDecimal; -import java.util.Iterator; import java.util.List; import org.hibernate.CacheMode; import org.hibernate.Hibernate; -import org.hibernate.query.Query; import org.hibernate.ScrollMode; import org.hibernate.ScrollableResults; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.proxy.HibernateProxy; +import org.hibernate.query.Query; import org.junit.Test; @@ -384,314 +383,12 @@ public class ReadOnlySessionTest extends AbstractReadOnlyTest { s.close(); } - @Test - public void testReadOnlySessionDefaultQueryIterate() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - t = s.beginTransaction(); - s.setDefaultReadOnly( true ); - int i = 0; - Iterator it = s.createQuery("from DataPoint dp order by dp.x asc") - .iterate(); - s.setDefaultReadOnly( false ); - while ( it.hasNext() ) { - DataPoint dp = (DataPoint) it.next(); - if (++i==50) { - s.setReadOnly(dp, false); - } - dp.setDescription("done!"); - } - t.commit(); - s.clear(); - t = s.beginTransaction(); - List single = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( 1, single.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); - t.commit(); - s.close(); - } - @Test - public void testReadOnlySessionModifiableQueryIterate() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - t = s.beginTransaction(); - s.setDefaultReadOnly( true ); - int i = 0; - Iterator it = s.createQuery("from DataPoint dp order by dp.x asc") - .setReadOnly( false ) - .iterate(); - while ( it.hasNext() ) { - DataPoint dp = (DataPoint) it.next(); - if (++i==50) { - s.setReadOnly(dp, true); - } - dp.setDescription("done!"); - } - t.commit(); - s.clear(); - t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( 99, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); - t.commit(); - s.close(); - } - @Test - public void testModifiableSessionReadOnlyQueryIterate() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - t = s.beginTransaction(); - assertFalse( s.isDefaultReadOnly() ); - int i = 0; - Iterator it = s.createQuery("from DataPoint dp order by dp.x asc") - .setReadOnly( true ) - .iterate(); - while ( it.hasNext() ) { - DataPoint dp = (DataPoint) it.next(); - if (++i==50) { - s.setReadOnly(dp, false); - } - dp.setDescription("done!"); - } - t.commit(); - s.clear(); - t = s.beginTransaction(); - List single = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( 1, single.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); - t.commit(); - s.close(); - } - @Test - public void testModifiableSessionDefaultQueryReadOnlySessionIterate() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - Transaction t = s.beginTransaction(); - for ( int i=0; i<100; i++ ) { - DataPoint dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - t = s.beginTransaction(); - s.setDefaultReadOnly( false ); - int i = 0; - Query query = s.createQuery("from DataPoint dp order by dp.x asc"); - s.setDefaultReadOnly( true ); - Iterator it = query.iterate(); - s.setDefaultReadOnly( false ); - while ( it.hasNext() ) { - DataPoint dp = (DataPoint) it.next(); - if (++i==50) { - s.setReadOnly(dp, false); - } - dp.setDescription("done!"); - } - t.commit(); - s.clear(); - t = s.beginTransaction(); - List single = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( 1, single.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); - t.commit(); - s.close(); - } - - @Test - public void testQueryReadOnlyIterate() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - Transaction t = s.beginTransaction(); - DataPoint dp = null; - for ( int i=0; i<100; i++ ) { - dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - t = s.beginTransaction(); - s.setDefaultReadOnly( false ); - int i = 0; - Query query = s.createQuery("from DataPoint dp order by dp.x asc"); - assertFalse( query.isReadOnly() ); - s.setDefaultReadOnly( true ); - assertTrue( query.isReadOnly() ); - s.setDefaultReadOnly( false ); - assertFalse( query.isReadOnly() ); - query.setReadOnly( true ); - assertTrue( query.isReadOnly() ); - s.setDefaultReadOnly( true ); - assertTrue( query.isReadOnly() ); - s.setDefaultReadOnly( false ); - assertTrue( query.isReadOnly() ); - query.setReadOnly( false ); - assertFalse( query.isReadOnly() ); - s.setDefaultReadOnly( true ); - assertFalse( query.isReadOnly() ); - query.setReadOnly( true ); - assertTrue( query.isReadOnly() ); - s.setDefaultReadOnly( false ); - assertFalse( s.isDefaultReadOnly() ); - Iterator it = query.iterate(); - assertTrue( query.isReadOnly() ); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - assertFalse( s.isReadOnly( dpLast ) ); - query.setReadOnly( false ); - assertFalse( query.isReadOnly() ); - int nExpectedChanges = 0; - assertFalse( s.isDefaultReadOnly() ); - while ( it.hasNext() ) { - assertFalse( s.isDefaultReadOnly() ); - dp = (DataPoint) it.next(); - assertFalse( s.isDefaultReadOnly() ); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertFalse( s.isReadOnly( dp ) ); - } - else { - assertTrue( s.isReadOnly( dp ) ); - } - if (++i==50) { - s.setReadOnly(dp, false); - nExpectedChanges = ( dp == dpLast ? 1 : 2 ); - } - dp.setDescription("done!"); - } - assertFalse( s.isDefaultReadOnly() ); - t.commit(); - s.clear(); - t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( nExpectedChanges, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); - t.commit(); - s.close(); - } - - @Test - public void testQueryModifiableIterate() { - Session s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - Transaction t = s.beginTransaction(); - DataPoint dp = null; - for ( int i=0; i<100; i++ ) { - dp = new DataPoint(); - dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); - dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); - s.save(dp); - } - t.commit(); - s.close(); - - s = openSession(); - s.setCacheMode(CacheMode.IGNORE); - t = s.beginTransaction(); - s.setDefaultReadOnly( true ); - int i = 0; - Query query = s.createQuery("from DataPoint dp order by dp.x asc"); - assertTrue( query.isReadOnly() ); - s.setDefaultReadOnly( false ); - assertFalse( query.isReadOnly() ); - s.setDefaultReadOnly( true ); - assertTrue( query.isReadOnly() ); - query.setReadOnly( false ); - assertFalse( query.isReadOnly() ); - s.setDefaultReadOnly( false ); - assertFalse( query.isReadOnly() ); - s.setDefaultReadOnly( true ); - assertFalse( query.isReadOnly() ); - query.setReadOnly( true ); - assertTrue( query.isReadOnly() ); - s.setDefaultReadOnly( false ); - assertTrue( query.isReadOnly() ); - query.setReadOnly( false ); - assertFalse( query.isReadOnly() ); - s.setDefaultReadOnly( true ); - assertTrue( s.isDefaultReadOnly() ); - Iterator it = query.iterate(); - assertFalse( query.isReadOnly() ); - DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); - assertTrue( s.isReadOnly( dpLast ) ); - query.setReadOnly( true ); - assertTrue( query.isReadOnly() ); - int nExpectedChanges = 0; - assertTrue( s.isDefaultReadOnly() ); - while ( it.hasNext() ) { - assertTrue( s.isDefaultReadOnly() ); - dp = (DataPoint) it.next(); - assertTrue( s.isDefaultReadOnly() ); - if ( dp.getId() == dpLast.getId() ) { - //dpLast existed in the session before executing the read-only query - assertTrue( s.isReadOnly( dp ) ); - } - else { - assertFalse( s.isReadOnly( dp ) ); - } - if (++i==50) { - s.setReadOnly(dp, true); - nExpectedChanges = ( dp == dpLast ? 99 : 98 ); - } - dp.setDescription("done!"); - } - assertTrue( s.isDefaultReadOnly() ); - t.commit(); - s.clear(); - t = s.beginTransaction(); - List list = s.createQuery("from DataPoint where description='done!'").list(); - assertEquals( nExpectedChanges, list.size() ); - s.createQuery("delete from DataPoint").executeUpdate(); - t.commit(); - s.close(); - } @Test public void testReadOnlyRefresh() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyTest.java b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyTest.java index 9a4c41bfba..4f65a37588 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/readonly/ReadOnlyTest.java @@ -107,7 +107,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest { .setReadOnly(true) .scroll(ScrollMode.FORWARD_ONLY); while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(0); + DataPoint dp = (DataPoint) sr.get(); if (++i==50) { s.setReadOnly(dp, false); } @@ -157,7 +157,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest { clearCounts(); while ( sr.next() ) { - DataPoint dp = (DataPoint) sr.get(0); + DataPoint dp = (DataPoint) sr.get(); assertFalse( s.isReadOnly( dp ) ); s.delete( dp ); } @@ -419,7 +419,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest { .scroll(ScrollMode.FORWARD_ONLY); int nExpectedChanges = 0; while ( sr.next() ) { - dp = (DataPoint) sr.get(0); + dp = (DataPoint) sr.get(); if ( dp.getId() == dpLast.getId() ) { //dpLast existed in the session before executing the read-only query assertFalse( s.isReadOnly( dp ) ); @@ -486,7 +486,7 @@ public class ReadOnlyTest extends AbstractReadOnlyTest { .scroll(ScrollMode.FORWARD_ONLY); int nExpectedChanges = 0; while ( sr.next() ) { - dp = (DataPoint) sr.get(0); + dp = (DataPoint) sr.get(); if ( dp.getId() == dpLast.getId() ) { //dpLast existed in the session before executing the read-only query assertTrue( s.isReadOnly( dp ) ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/rowid/RowIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/rowid/RowIdTest.java index b2b8cd647f..b7949d82bb 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/rowid/RowIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/rowid/RowIdTest.java @@ -7,19 +7,18 @@ package org.hibernate.test.rowid; import java.math.BigDecimal; -import java.sql.Connection; -import java.sql.SQLException; import java.sql.Statement; - -import org.junit.Test; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.dialect.Oracle9iDialect; import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.jdbc.Work; + import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; /** * @author Gavin King @@ -42,18 +41,15 @@ public class RowIdTest extends BaseCoreFunctionalTestCase { super.afterSessionFactoryBuilt(); final Session session = sessionFactory().openSession(); session.doWork( - new Work() { - @Override - public void execute(Connection connection) throws SQLException { - Statement st = ((SessionImplementor)session).getJdbcCoordinator().getStatementPreparer().createStatement(); - try { - ((SessionImplementor)session).getJdbcCoordinator().getResultSetReturn().execute( st, "drop table Point"); - } - catch (Exception ignored) { - } - ((SessionImplementor)session).getJdbcCoordinator().getResultSetReturn().execute( st, "create table Point (\"x\" number(19,2) not null, \"y\" number(19,2) not null, description varchar2(255) )"); - ((SessionImplementor)session).getJdbcCoordinator().getResourceRegistry().release( st ); + connection -> { + Statement st = ((SessionImplementor)session).getJdbcCoordinator().getStatementPreparer().createStatement(); + try { + ((SessionImplementor)session).getJdbcCoordinator().getResultSetReturn().execute( st, "drop table Point"); } + catch (Exception ignored) { + } + ((SessionImplementor)session).getJdbcCoordinator().getResultSetReturn().execute( st, "create table Point (\"x\" number(19,2) not null, \"y\" number(19,2) not null, description varchar2(255) )"); + ((SessionImplementor)session).getJdbcCoordinator().getResourceRegistry().release( st ); } ); session.close(); @@ -61,31 +57,43 @@ public class RowIdTest extends BaseCoreFunctionalTestCase { @Test public void testRowId() { - Session s = openSession(); - Transaction t = s.beginTransaction(); - Point p = new Point( new BigDecimal(1.0), new BigDecimal(1.0) ); - s.persist(p); - t.commit(); - s.clear(); - - t = s.beginTransaction(); - p = (Point) s.createCriteria(Point.class).uniqueResult(); - p.setDescription("new desc"); - t.commit(); - s.clear(); - - t = s.beginTransaction(); - p = (Point) s.createQuery("from Point").uniqueResult(); - p.setDescription("new new desc"); - t.commit(); - s.clear(); - - t = s.beginTransaction(); - p = (Point) s.get(Point.class, p); - p.setDescription("new new new desc"); - t.commit(); - s.close(); + inSession( + s -> { + try { + Transaction t = s.beginTransaction(); + Point p = new Point( new BigDecimal( 1.0 ), new BigDecimal( 1.0 ) ); + s.persist( p ); + t.commit(); + s.clear(); + + t = s.beginTransaction(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Point.class ); + criteria.from( Point.class ); + p = s.createQuery( criteria ).uniqueResult(); +// p = (Point) s.createCriteria( Point.class ).uniqueResult(); + p.setDescription( "new desc" ); + t.commit(); + s.clear(); + + t = s.beginTransaction(); + p = (Point) s.createQuery( "from Point" ).uniqueResult(); + p.setDescription( "new new desc" ); + t.commit(); + s.clear(); + + t = s.beginTransaction(); + p = s.get( Point.class, p ); + p.setDescription( "new new new desc" ); + t.commit(); + } + finally { + if ( s.getTransaction().isActive() ) { + s.getTransaction().rollback(); + } + } + } + ); } } - diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java index d36a30cdbe..12327d8e64 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java @@ -73,6 +73,7 @@ public class StatelessSessionTest extends BaseCoreFunctionalTestCase { assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); + doc2 = (Document) ss.createCriteria(Document.class).uniqueResult(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); diff --git a/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java b/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java index 845ae90be1..48dc9d3e40 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/stats/StatsTest.java @@ -167,7 +167,7 @@ public class StatsTest extends BaseUnitTestCase { assertEquals( maxTime, sf.getStatistics().getQueryExecutionMaxTime() ); // assertEquals( continents, stats.getQueryExecutionMaxTimeQueryString() ); - Iterator itr = s.createQuery( continents ).iterate(); + Iterator itr = s.createQuery( continents ).list().iterator(); // iterate() should increment the execution count assertEquals( "unexpected execution count", 2, continentStats.getExecutionCount() ); // but should not effect the cumulative row count diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTest.java b/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTest.java index 0b8b689098..9b818c5411 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTest.java @@ -8,7 +8,9 @@ package org.hibernate.test.subselectfetch; import java.util.List; -import org.hibernate.Session; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.hibernate.dialect.SQLServerDialect; import org.hibernate.dialect.SybaseDialect; import org.hibernate.mapping.Collection; @@ -83,12 +85,12 @@ public class SubselectFetchWithFormulaTest extends BaseNonConfigCoreFunctionalTe @After public void after() { - Session session = openSession(); - session.getTransaction().begin(); - session.createQuery( "delete Value" ).executeUpdate(); - session.createQuery( "delete Name" ).executeUpdate(); - session.getTransaction().commit(); - session.close(); + inTransaction( + session -> { + session.createQuery( "delete Value" ).executeUpdate(); + session.createQuery( "delete Name" ).executeUpdate(); + } + ); } @@ -99,15 +101,19 @@ public class SubselectFetchWithFormulaTest extends BaseNonConfigCoreFunctionalTe assertThat( collectionBinding.isSubselectLoadable(), is( true ) ); // Now force the subselect fetch and make sure we do not get SQL errors - Session session = openSession(); - session.getTransaction().begin(); - List results = session.createCriteria(Name.class).list(); - for (Object result : results) { - Name name = (Name) result; - name.getValues().size(); - } - session.getTransaction().commit(); - session.close(); + inTransaction( + session -> { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Name.class ); + criteria.from( Name.class ); + List results = session.createQuery( criteria ).list(); +// List results = session.createCriteria(Name.class).list(); + for (Name name : results) { + name.getValues().size(); + } + } + ); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTransactSqlTest.java b/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTransactSqlTest.java index 4962b7666c..3deaac8494 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTransactSqlTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/subselectfetch/SubselectFetchWithFormulaTransactSqlTest.java @@ -7,17 +7,18 @@ package org.hibernate.test.subselectfetch; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.hibernate.Session; import org.hibernate.dialect.SQLServerDialect; import org.hibernate.dialect.SybaseDialect; import org.hibernate.mapping.Collection; + import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; @@ -39,56 +40,53 @@ public class SubselectFetchWithFormulaTransactSqlTest extends BaseNonConfigCoreF @Before public void before() { - Session session = openSession(); - session.getTransaction().begin(); + inTransaction( + session -> { + Name chris = new Name(); + chris.setId( 1 ); + chris.setName( "chris" ); + Value cat = new Value(); + cat.setId(1); + cat.setName( chris ); + cat.setValue( "cat" ); + Value canary = new Value(); + canary.setId( 2 ); + canary.setName( chris ); + canary.setValue( "canary" ); - Name chris = new Name(); - chris.setId( 1 ); - chris.setName( "chris" ); - Value cat = new Value(); - cat.setId(1); - cat.setName( chris ); - cat.setValue( "cat" ); - Value canary = new Value(); - canary.setId( 2 ); - canary.setName( chris ); - canary.setValue( "canary" ); + session.persist( chris ); + session.persist( cat ); + session.persist( canary ); - session.persist( chris ); - session.persist( cat ); - session.persist( canary ); + Name sam = new Name(); + sam.setId(2); + sam.setName( "sam" ); + Value seal = new Value(); + seal.setId( 3 ); + seal.setName( sam ); + seal.setValue( "seal" ); + Value snake = new Value(); + snake.setId( 4 ); + snake.setName( sam ); + snake.setValue( "snake" ); - Name sam = new Name(); - sam.setId(2); - sam.setName( "sam" ); - Value seal = new Value(); - seal.setId( 3 ); - seal.setName( sam ); - seal.setValue( "seal" ); - Value snake = new Value(); - snake.setId( 4 ); - snake.setName( sam ); - snake.setValue( "snake" ); - - session.persist( sam ); - session.persist(seal); - session.persist( snake ); - - session.getTransaction().commit(); - session.close(); + session.persist( sam ); + session.persist(seal); + session.persist( snake ); + } + ); } @After public void after() { - Session session = openSession(); - session.getTransaction().begin(); - session.createQuery( "delete Value" ).executeUpdate(); - session.createQuery( "delete Name" ).executeUpdate(); - session.getTransaction().commit(); - session.close(); + inTransaction( + session -> { + session.createQuery( "delete Value" ).executeUpdate(); + session.createQuery( "delete Name" ).executeUpdate(); + } + ); } - @Test public void checkSubselectWithFormula() throws Exception { // as a pre-condition make sure that subselect fetching is enabled for the collection... @@ -96,15 +94,18 @@ public class SubselectFetchWithFormulaTransactSqlTest extends BaseNonConfigCoreF assertThat( collectionBinding.isSubselectLoadable(), is( true ) ); // Now force the subselect fetch and make sure we do not get SQL errors - Session session = openSession(); - session.getTransaction().begin(); - List results = session.createCriteria(Name.class).list(); - for (Object result : results) { - Name name = (Name) result; - name.getValues().size(); - } - session.getTransaction().commit(); - session.close(); + inTransaction( + session -> { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Name.class ); + criteria.from( Name.class ); + List results = session.createQuery( criteria ).list(); +// List results = session.createCriteria(Name.class).list(); + for (Name name : results) { + name.getValues().size(); + } + } + ); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/version/VersionTest.java b/hibernate-core/src/test/java/org/hibernate/test/version/VersionTest.java index 75c5656cb0..7838fb0a8e 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/version/VersionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/version/VersionTest.java @@ -6,6 +6,9 @@ */ package org.hibernate.test.version; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.junit.Test; import org.hibernate.Hibernate; @@ -37,7 +40,7 @@ public class VersionTest extends BaseCoreFunctionalTestCase { s.persist(gavin); t.commit(); s.close(); - + s = openSession(); t = s.beginTransaction(); Thing passp = (Thing) s.get(Thing.class, "Passport"); @@ -47,7 +50,7 @@ public class VersionTest extends BaseCoreFunctionalTestCase { s.createQuery("from Person").list(); t.commit(); s.close(); - + assertEquals( passp.getVersion(), 1 ); s = openSession(); @@ -80,7 +83,7 @@ public class VersionTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - gavin = (Person) s.createCriteria(Person.class).uniqueResult(); + gavin = getPerson( s ); new Thing("Laptop", gavin); t.commit(); s.close(); @@ -90,7 +93,7 @@ public class VersionTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - gavin = (Person) s.createCriteria(Person.class).uniqueResult(); + gavin = getPerson( s ); gavin.getThings().clear(); t.commit(); s.close(); @@ -119,7 +122,7 @@ public class VersionTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - gavin = (Person) s.createCriteria(Person.class).uniqueResult(); + gavin = getPerson( s ); new Task("Document", gavin); t.commit(); s.close(); @@ -129,7 +132,7 @@ public class VersionTest extends BaseCoreFunctionalTestCase { s = openSession(); t = s.beginTransaction(); - gavin = (Person) s.createCriteria(Person.class).uniqueResult(); + gavin = getPerson( s ); gavin.getTasks().clear(); t.commit(); s.close(); @@ -144,5 +147,10 @@ public class VersionTest extends BaseCoreFunctionalTestCase { s.close(); } + private Person getPerson(Session s) { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Person.class ); + criteria.from( Person.class ); + return s.createQuery( criteria ).uniqueResult(); + } } - diff --git a/hibernate-core/src/test/java/org/hibernate/test/queryplan/GetHqlQueryPlanTest.java b/hibernate-core/src/test_legacy/org/hibernate/test/queryplan/GetHqlQueryPlanTest.java similarity index 100% rename from hibernate-core/src/test/java/org/hibernate/test/queryplan/GetHqlQueryPlanTest.java rename to hibernate-core/src/test_legacy/org/hibernate/test/queryplan/GetHqlQueryPlanTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/queryplan/NativeSQLQueryPlanEqualsTest.java b/hibernate-core/src/test_legacy/org/hibernate/test/queryplan/NativeSQLQueryPlanEqualsTest.java similarity index 100% rename from hibernate-core/src/test/java/org/hibernate/test/queryplan/NativeSQLQueryPlanEqualsTest.java rename to hibernate-core/src/test_legacy/org/hibernate/test/queryplan/NativeSQLQueryPlanEqualsTest.java diff --git a/hibernate-core/src/test_legacy/org/hibernate/test/readonly/ReadOnlySessionQueryIteratorTest.java b/hibernate-core/src/test_legacy/org/hibernate/test/readonly/ReadOnlySessionQueryIteratorTest.java new file mode 100644 index 0000000000..a414cdf813 --- /dev/null +++ b/hibernate-core/src/test_legacy/org/hibernate/test/readonly/ReadOnlySessionQueryIteratorTest.java @@ -0,0 +1,344 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.test.readonly; + +import java.math.BigDecimal; +import java.util.Iterator; +import java.util.List; + +import org.hibernate.CacheMode; +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.query.Query; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Andrea Boriero + */ +public class ReadOnlySessionQueryIteratorTest extends AbstractReadOnlyTest { + @Override + public String[] getMappings() { + return new String[] { "readonly/DataPoint.hbm.xml", "readonly/TextHolder.hbm.xml" }; + } + + @Test + public void testReadOnlySessionDefaultQueryIterate() { + Session s = openSession(); + s.setCacheMode( CacheMode.IGNORE ); + Transaction t = s.beginTransaction(); + for ( int i = 0; i < 100; i++ ) { + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) ); + s.save( dp ); + } + t.commit(); + s.close(); + + s = openSession(); + s.setCacheMode( CacheMode.IGNORE ); + t = s.beginTransaction(); + s.setDefaultReadOnly( true ); + int i = 0; + Iterator it = s.createQuery( "from DataPoint dp order by dp.x asc" ) + .iterate(); + s.setDefaultReadOnly( false ); + while ( it.hasNext() ) { + DataPoint dp = (DataPoint) it.next(); + if ( ++i == 50 ) { + s.setReadOnly( dp, false ); + } + dp.setDescription( "done!" ); + } + t.commit(); + s.clear(); + t = s.beginTransaction(); + List single = s.createQuery( "from DataPoint where description='done!'" ).list(); + assertEquals( 1, single.size() ); + s.createQuery( "delete from DataPoint" ).executeUpdate(); + t.commit(); + s.close(); + } + + @Test + public void testReadOnlySessionModifiableQueryIterate() { + Session s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + Transaction t = s.beginTransaction(); + for ( int i=0; i<100; i++ ) { + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); + s.save(dp); + } + t.commit(); + s.close(); + + s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + t = s.beginTransaction(); + s.setDefaultReadOnly( true ); + int i = 0; + Iterator it = s.createQuery("from DataPoint dp order by dp.x asc") + .setReadOnly( false ) + .iterate(); + while ( it.hasNext() ) { + DataPoint dp = (DataPoint) it.next(); + if (++i==50) { + s.setReadOnly(dp, true); + } + dp.setDescription("done!"); + } + t.commit(); + s.clear(); + t = s.beginTransaction(); + List list = s.createQuery("from DataPoint where description='done!'").list(); + assertEquals( 99, list.size() ); + s.createQuery("delete from DataPoint").executeUpdate(); + t.commit(); + s.close(); + } + + @Test + public void testModifiableSessionReadOnlyQueryIterate() { + Session s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + Transaction t = s.beginTransaction(); + for ( int i=0; i<100; i++ ) { + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); + s.save(dp); + } + t.commit(); + s.close(); + + s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + t = s.beginTransaction(); + assertFalse( s.isDefaultReadOnly() ); + int i = 0; + Iterator it = s.createQuery("from DataPoint dp order by dp.x asc") + .setReadOnly( true ) + .iterate(); + while ( it.hasNext() ) { + DataPoint dp = (DataPoint) it.next(); + if (++i==50) { + s.setReadOnly(dp, false); + } + dp.setDescription("done!"); + } + t.commit(); + s.clear(); + t = s.beginTransaction(); + List single = s.createQuery("from DataPoint where description='done!'").list(); + assertEquals( 1, single.size() ); + s.createQuery("delete from DataPoint").executeUpdate(); + t.commit(); + s.close(); + } + + @Test + public void testModifiableSessionDefaultQueryReadOnlySessionIterate() { + Session s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + Transaction t = s.beginTransaction(); + for ( int i=0; i<100; i++ ) { + DataPoint dp = new DataPoint(); + dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); + s.save(dp); + } + t.commit(); + s.close(); + + s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + t = s.beginTransaction(); + s.setDefaultReadOnly( false ); + int i = 0; + Query query = s.createQuery( "from DataPoint dp order by dp.x asc"); + s.setDefaultReadOnly( true ); + Iterator it = query.iterate(); + s.setDefaultReadOnly( false ); + while ( it.hasNext() ) { + DataPoint dp = (DataPoint) it.next(); + if (++i==50) { + s.setReadOnly(dp, false); + } + dp.setDescription("done!"); + } + t.commit(); + s.clear(); + t = s.beginTransaction(); + List single = s.createQuery("from DataPoint where description='done!'").list(); + assertEquals( 1, single.size() ); + s.createQuery("delete from DataPoint").executeUpdate(); + t.commit(); + s.close(); + } + + @Test + public void testQueryReadOnlyIterate() { + Session s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + Transaction t = s.beginTransaction(); + DataPoint dp = null; + for ( int i=0; i<100; i++ ) { + dp = new DataPoint(); + dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); + s.save(dp); + } + t.commit(); + s.close(); + + s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + t = s.beginTransaction(); + s.setDefaultReadOnly( false ); + int i = 0; + Query query = s.createQuery("from DataPoint dp order by dp.x asc"); + assertFalse( query.isReadOnly() ); + s.setDefaultReadOnly( true ); + assertTrue( query.isReadOnly() ); + s.setDefaultReadOnly( false ); + assertFalse( query.isReadOnly() ); + query.setReadOnly( true ); + assertTrue( query.isReadOnly() ); + s.setDefaultReadOnly( true ); + assertTrue( query.isReadOnly() ); + s.setDefaultReadOnly( false ); + assertTrue( query.isReadOnly() ); + query.setReadOnly( false ); + assertFalse( query.isReadOnly() ); + s.setDefaultReadOnly( true ); + assertFalse( query.isReadOnly() ); + query.setReadOnly( true ); + assertTrue( query.isReadOnly() ); + s.setDefaultReadOnly( false ); + assertFalse( s.isDefaultReadOnly() ); + Iterator it = query.iterate(); + assertTrue( query.isReadOnly() ); + DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + assertFalse( s.isReadOnly( dpLast ) ); + query.setReadOnly( false ); + assertFalse( query.isReadOnly() ); + int nExpectedChanges = 0; + assertFalse( s.isDefaultReadOnly() ); + while ( it.hasNext() ) { + assertFalse( s.isDefaultReadOnly() ); + dp = (DataPoint) it.next(); + assertFalse( s.isDefaultReadOnly() ); + if ( dp.getId() == dpLast.getId() ) { + //dpLast existed in the session before executing the read-only query + assertFalse( s.isReadOnly( dp ) ); + } + else { + assertTrue( s.isReadOnly( dp ) ); + } + if (++i==50) { + s.setReadOnly(dp, false); + nExpectedChanges = ( dp == dpLast ? 1 : 2 ); + } + dp.setDescription("done!"); + } + assertFalse( s.isDefaultReadOnly() ); + t.commit(); + s.clear(); + t = s.beginTransaction(); + List list = s.createQuery("from DataPoint where description='done!'").list(); + assertEquals( nExpectedChanges, list.size() ); + s.createQuery("delete from DataPoint").executeUpdate(); + t.commit(); + s.close(); + } + + @Test + public void testQueryModifiableIterate() { + Session s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + Transaction t = s.beginTransaction(); + DataPoint dp = null; + for ( int i=0; i<100; i++ ) { + dp = new DataPoint(); + dp.setX( new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN) ); + dp.setY( new BigDecimal( Math.cos( dp.getX().doubleValue() ) ).setScale(19, BigDecimal.ROUND_DOWN) ); + s.save(dp); + } + t.commit(); + s.close(); + + s = openSession(); + s.setCacheMode(CacheMode.IGNORE); + t = s.beginTransaction(); + s.setDefaultReadOnly( true ); + int i = 0; + Query query = s.createQuery("from DataPoint dp order by dp.x asc"); + assertTrue( query.isReadOnly() ); + s.setDefaultReadOnly( false ); + assertFalse( query.isReadOnly() ); + s.setDefaultReadOnly( true ); + assertTrue( query.isReadOnly() ); + query.setReadOnly( false ); + assertFalse( query.isReadOnly() ); + s.setDefaultReadOnly( false ); + assertFalse( query.isReadOnly() ); + s.setDefaultReadOnly( true ); + assertFalse( query.isReadOnly() ); + query.setReadOnly( true ); + assertTrue( query.isReadOnly() ); + s.setDefaultReadOnly( false ); + assertTrue( query.isReadOnly() ); + query.setReadOnly( false ); + assertFalse( query.isReadOnly() ); + s.setDefaultReadOnly( true ); + assertTrue( s.isDefaultReadOnly() ); + Iterator it = query.iterate(); + assertFalse( query.isReadOnly() ); + DataPoint dpLast = ( DataPoint ) s.get( DataPoint.class, dp.getId() ); + assertTrue( s.isReadOnly( dpLast ) ); + query.setReadOnly( true ); + assertTrue( query.isReadOnly() ); + int nExpectedChanges = 0; + assertTrue( s.isDefaultReadOnly() ); + while ( it.hasNext() ) { + assertTrue( s.isDefaultReadOnly() ); + dp = (DataPoint) it.next(); + assertTrue( s.isDefaultReadOnly() ); + if ( dp.getId() == dpLast.getId() ) { + //dpLast existed in the session before executing the read-only query + assertTrue( s.isReadOnly( dp ) ); + } + else { + assertFalse( s.isReadOnly( dp ) ); + } + if (++i==50) { + s.setReadOnly(dp, true); + nExpectedChanges = ( dp == dpLast ? 99 : 98 ); + } + dp.setDescription("done!"); + } + assertTrue( s.isDefaultReadOnly() ); + t.commit(); + s.clear(); + t = s.beginTransaction(); + List list = s.createQuery("from DataPoint where description='done!'").list(); + assertEquals( nExpectedChanges, list.size() ); + s.createQuery("delete from DataPoint").executeUpdate(); + t.commit(); + s.close(); + } + + + +}