From dd5d399f27df700d50fc50b8e9cf5ef2c7dfc4fc Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 17 Jul 2019 16:07:55 +0100 Subject: [PATCH] 6 - SQM based on JPA type system --- .../test/annotations/cid/CompositeIdTest.java | 42 ++-- .../test/annotations/fetch/FetchingTest.java | 114 +++++---- .../annotations/inheritance/SubclassTest.java | 216 +++++++++--------- .../joined/JoinedSubclassTest.java | 102 +++++---- .../inheritance/mixed/SubclassTest.java | 41 ++-- .../inheritance/union/SubclassTest.java | 45 ++-- 6 files changed, 313 insertions(+), 247 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/cid/CompositeIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/cid/CompositeIdTest.java index 1b775ef3db..470461bf2d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/cid/CompositeIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/cid/CompositeIdTest.java @@ -10,6 +10,10 @@ import java.util.ArrayList; import java.util.Date; 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.query.Query; @@ -417,24 +421,30 @@ public class CompositeIdTest extends BaseCoreFunctionalTestCase { @Test public void testQueryInAndComposite() { - Session s = openSession( ); - Transaction transaction = s.beginTransaction(); - createData( s ); - s.flush(); - List ids = new ArrayList(2); - ids.add( new SomeEntityId(1,12) ); - ids.add( new SomeEntityId(10,23) ); + inTransaction( + s -> { + createData( s ); + s.flush(); + List ids = new ArrayList(2); + ids.add( new SomeEntityId(1,12) ); + ids.add( new SomeEntityId(10,23) ); - Criteria criteria = s.createCriteria( SomeEntity.class ); - Disjunction disjunction = Restrictions.disjunction(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( SomeEntity.class ); + Root root = criteria.from( SomeEntity.class ); + criteria.where( criteriaBuilder.or( criteriaBuilder.in( root.get( "id" )).value( ids) ) ); + List list = s.createQuery( criteria ).list(); - disjunction.add( Restrictions.in( "id", ids ) ); - criteria.add( disjunction ); - - List list = criteria.list(); - assertEquals( 2, list.size() ); - transaction.rollback(); - s.close(); +// Criteria criteria = s.createCriteria( SomeEntity.class ); +// Disjunction disjunction = Restrictions.disjunction(); +// +// disjunction.add( Restrictions.in( "id", ids ) ); +// criteria.add( disjunction ); +// +// List list = criteria.list(); + assertEquals( 2, list.size() ); + } + ); } @Test diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/fetch/FetchingTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/fetch/FetchingTest.java index ed49ce4236..57ab5b81fe 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/fetch/FetchingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/fetch/FetchingTest.java @@ -8,6 +8,9 @@ package org.hibernate.test.annotations.fetch; import java.util.Date; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.junit.Test; import org.hibernate.Hibernate; @@ -24,7 +27,7 @@ import static org.junit.Assert.assertTrue; */ public class FetchingTest extends BaseCoreFunctionalTestCase { @Test - public void testLazy() throws Exception { + public void testLazy() { Session s; Transaction tx; s = openSession(); @@ -45,7 +48,7 @@ public class FetchingTest extends BaseCoreFunctionalTestCase { } @Test - public void testExtraLazy() throws Exception { + public void testExtraLazy() { Session s; Transaction tx; s = openSession(); @@ -70,59 +73,76 @@ public class FetchingTest extends BaseCoreFunctionalTestCase { } @Test - public void testHibernateFetchingLazy() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Person p = new Person( "Gavin", "King", "JBoss Inc" ); - Stay stay = new Stay( null, new Date(), new Date(), "A380", "Blah", "Blah" ); - Stay stay2 = new Stay( null, new Date(), new Date(), "A320", "Blah", "Blah" ); - Stay stay3 = new Stay( null, new Date(), new Date(), "A340", "Blah", "Blah" ); - stay.setOldPerson( p ); - stay2.setVeryOldPerson( p ); - stay3.setVeryOldPerson( p ); - p.addOldStay( stay ); - p.addVeryOldStay( stay2 ); - p.addVeryOldStay( stay3 ); - s.persist( p ); - tx.commit(); - s.clear(); - tx = s.beginTransaction(); - p = (Person) s.createQuery( "from Person p where p.firstName = :name" ) - .setParameter( "name", "Gavin" ).uniqueResult(); - assertFalse( Hibernate.isInitialized( p.getOldStays() ) ); - assertEquals( 1, p.getOldStays().size() ); - assertFalse( "lazy extra is failing", Hibernate.isInitialized( p.getOldStays() ) ); - s.clear(); - stay = (Stay) s.get( Stay.class, stay.getId() ); - assertTrue( ! Hibernate.isInitialized( stay.getOldPerson() ) ); - s.clear(); - stay3 = (Stay) s.get( Stay.class, stay3.getId() ); - assertTrue( "FetchMode.JOIN should overrides lazy options", Hibernate.isInitialized( stay3.getVeryOldPerson() ) ); - s.delete( stay3.getVeryOldPerson() ); - tx.commit(); - s.close(); + public void testHibernateFetchingLazy() { + try(Session s = openSession()) { + Transaction tx = s.beginTransaction(); + try { + Person p = new Person( "Gavin", "King", "JBoss Inc" ); + Stay stay = new Stay( null, new Date(), new Date(), "A380", "Blah", "Blah" ); + Stay stay2 = new Stay( null, new Date(), new Date(), "A320", "Blah", "Blah" ); + Stay stay3 = new Stay( null, new Date(), new Date(), "A340", "Blah", "Blah" ); + stay.setOldPerson( p ); + stay2.setVeryOldPerson( p ); + stay3.setVeryOldPerson( p ); + p.addOldStay( stay ); + p.addVeryOldStay( stay2 ); + p.addVeryOldStay( stay3 ); + s.persist( p ); + tx.commit(); + s.clear(); + tx = s.beginTransaction(); + p = (Person) s.createQuery( "from Person p where p.firstName = :name" ) + .setParameter( "name", "Gavin" ).uniqueResult(); + assertFalse( Hibernate.isInitialized( p.getOldStays() ) ); + assertEquals( 1, p.getOldStays().size() ); + assertFalse( "lazy extra is failing", Hibernate.isInitialized( p.getOldStays() ) ); + s.clear(); + stay = (Stay) s.get( Stay.class, stay.getId() ); + assertTrue( !Hibernate.isInitialized( stay.getOldPerson() ) ); + s.clear(); + stay3 = (Stay) s.get( Stay.class, stay3.getId() ); + assertTrue( + "FetchMode.JOIN should overrides lazy options", + Hibernate.isInitialized( stay3.getVeryOldPerson() ) + ); + s.delete( stay3.getVeryOldPerson() ); + tx.commit(); + }finally { + if ( tx.isActive() ) { + tx.rollback(); + } + } + } } @Test - public void testOneToManyFetchEager() throws Exception { + public void testOneToManyFetchEager() { Branch b = new Branch(); Session s = openSession( ); - s.getTransaction().begin(); - s.persist( b ); - s.flush(); - Leaf l = new Leaf(); - l.setBranch( b ); - s.persist( l ); - s.flush(); + try { + s.getTransaction().begin(); + s.persist( b ); + s.flush(); + Leaf l = new Leaf(); + l.setBranch( b ); + s.persist( l ); + s.flush(); - s.clear(); + s.clear(); - s.createCriteria( Branch.class ).list(); + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Branch.class ); + criteria.from( Branch.class ); + s.createQuery( criteria ).list(); +// s.createCriteria( Branch.class ).list(); - s.getTransaction().rollback(); - s.close(); + } + finally { + if ( s.getTransaction().isActive() ) { + s.getTransaction().rollback(); + } + s.close(); + } } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/SubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/SubclassTest.java index cd8efcee1b..d739ecc000 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/SubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/SubclassTest.java @@ -8,11 +8,14 @@ package org.hibernate.test.annotations.inheritance; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.junit.Test; +import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.query.Query; -import org.hibernate.Session; -import org.hibernate.Transaction; + import org.hibernate.test.annotations.A320; import org.hibernate.test.annotations.A320b; import org.hibernate.test.annotations.Plane; @@ -23,7 +26,7 @@ import org.hibernate.test.annotations.inheritance.singletable.Rock; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -36,125 +39,132 @@ public class SubclassTest extends BaseCoreFunctionalTestCase { protected boolean isCleanupTestDataRequired() { return true; } - @Test - public void testPolymorphism() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - Plane p = new Plane(); - p.setNbrOfSeats( 10 ); - A320 a = new A320(); - a.setJavaEmbeddedVersion( "5.0" ); - a.setNbrOfSeats( 300 ); - s.persist( a ); - s.persist( p ); - tx.commit(); - s.close(); - s = openSession(); - tx = s.beginTransaction(); - Query q = s.createQuery( "from " + A320.class.getName() ); - List a320s = q.list(); - assertNotNull( a320s ); - assertEquals( 1, a320s.size() ); - assertTrue( a320s.get( 0 ) instanceof A320 ); - assertEquals( "5.0", ( (A320) a320s.get( 0 ) ).getJavaEmbeddedVersion() ); - q = s.createQuery( "from " + Plane.class.getName() ); - List planes = q.list(); - assertNotNull( planes ); - assertEquals( 2, planes.size() ); - tx.commit(); - s.close(); + @Test + public void testPolymorphism() { + inTransaction( + s -> { + Plane p = new Plane(); + p.setNbrOfSeats( 10 ); + A320 a = new A320(); + a.setJavaEmbeddedVersion( "5.0" ); + a.setNbrOfSeats( 300 ); + s.persist( a ); + s.persist( p ); + } + ); + + inTransaction( + s -> { + Query q = s.createQuery( "from " + A320.class.getName() ); + List a320s = q.list(); + assertNotNull( a320s ); + assertEquals( 1, a320s.size() ); + assertTrue( a320s.get( 0 ) instanceof A320 ); + assertEquals( "5.0", ( (A320) a320s.get( 0 ) ).getJavaEmbeddedVersion() ); + q = s.createQuery( "from " + Plane.class.getName() ); + List planes = q.list(); + assertNotNull( planes ); + assertEquals( 2, planes.size() ); + } + ); } @Test - public void test2ndLevelSubClass() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - A320b a = new A320b(); - a.setJavaEmbeddedVersion( "Elephant" ); - a.setNbrOfSeats( 300 ); - s.persist( a ); - tx.commit(); - s.close(); + public void test2ndLevelSubClass() { + inTransaction( + s -> { + A320b a = new A320b(); + a.setJavaEmbeddedVersion( "Elephant" ); + a.setNbrOfSeats( 300 ); + s.persist( a ); + } + ); - s = openSession(); - tx = s.beginTransaction(); - Query q = s.createQuery( "from " + A320.class.getName() + " as a where a.javaEmbeddedVersion = :version" ); - q.setParameter( "version", "Elephant" ); - List a320s = q.list(); - assertNotNull( a320s ); - assertEquals( 1, a320s.size() ); - tx.commit(); - s.close(); + inTransaction( + s -> { + Query q = s.createQuery( "from " + A320.class.getName() + " as a where a.javaEmbeddedVersion = :version" ); + q.setParameter( "version", "Elephant" ); + List a320s = q.list(); + assertNotNull( a320s ); + assertEquals( 1, a320s.size() ); + + } + ); } @Test - public void testEmbeddedSuperclass() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - Plane p = new Plane(); - p.setAlive( true ); //sic - p.setAltitude( 10000 ); - p.setMetricAltitude( 3000 ); - p.setNbrOfSeats( 150 ); - p.setSerial( "0123456789" ); - s.persist( p ); - tx.commit(); - s.close(); + public void testEmbeddedSuperclass() { + Plane plane = new Plane(); + inTransaction( + s -> { + plane.setAlive( true ); //sic + plane.setAltitude( 10000 ); + plane.setMetricAltitude( 3000 ); + plane.setNbrOfSeats( 150 ); + plane.setSerial( "0123456789" ); + s.persist( plane ); + } + ); - s = openSession(); - tx = s.beginTransaction(); - p = (Plane) s.get( Plane.class, p.getId() ); - assertNotNull( p ); - assertEquals( true, p.isAlive() ); - assertEquals( 150, p.getNbrOfSeats() ); - assertEquals( 10000, p.getAltitude() ); - assertEquals( "0123456789", p.getSerial() ); - assertFalse( 3000 == p.getMetricAltitude() ); - s.delete( p ); - tx.commit(); - s.close(); + inTransaction( + s -> { + Plane p = s.get( Plane.class, plane.getId() ); + assertNotNull( p ); + assertTrue( p.isAlive() ); + assertEquals( 150, p.getNbrOfSeats() ); + assertEquals( 10000, p.getAltitude() ); + assertEquals( "0123456789", p.getSerial() ); + assertNotEquals( 3000, p.getMetricAltitude() ); + s.delete( p ); + + } + ); } @Test - public void testFormula() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - Rock guns = new Rock(); - guns.setAvgBeat( 90 ); - guns.setType( 2 ); - Noise white = new Noise(); - white.setAvgBeat( 0 ); - white.setType( null ); + public void testFormula() { + inTransaction( + s -> { + Rock guns = new Rock(); + guns.setAvgBeat( 90 ); + guns.setType( 2 ); + Noise white = new Noise(); + white.setAvgBeat( 0 ); + white.setType( null ); - s.persist( guns ); - s.persist( white ); - tx.commit(); - s.close(); + s.persist( guns ); + s.persist( white ); + } + ); - s = openSession(); - tx = s.beginTransaction(); - List result = s.createCriteria( Noise.class ).list(); - assertNotNull( result ); - assertEquals( 1, result.size() ); - white = (Noise) result.get( 0 ); - assertNull( white.getType() ); - s.delete( white ); - result = s.createCriteria( Rock.class ).list(); - assertEquals( 1, result.size() ); - s.delete( result.get( 0 ) ); - result = s.createCriteria( Funk.class ).list(); - assertEquals( 0, result.size() ); + inTransaction( + s -> { + List result = createQueryForClass( s, Noise.class ).list(); + assertNotNull( result ); + assertEquals( 1, result.size() ); + Noise w = (Noise) result.get( 0 ); + assertNull( w.getType() ); + s.delete( w ); + result = createQueryForClass( s, Rock.class ).list(); + assertEquals( 1, result.size() ); + s.delete( result.get( 0 ) ); + result = createQueryForClass( s, Funk.class ).list(); + assertEquals( 0, result.size() ); + } + ); + } - tx.commit(); - s.close(); + private Query createQueryForClass(SessionImplementor session, Class clazz) { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( clazz ); + criteria.from( clazz ); + return session.createQuery( criteria ); } @Override protected Class[] getAnnotatedClasses() { - return new Class[]{ + return new Class[] { A320b.class, //subclasses should be properly reordered Plane.class, A320.class, diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/joined/JoinedSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/joined/JoinedSubclassTest.java index 91def4331d..bdfc0be748 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/joined/JoinedSubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/joined/JoinedSubclassTest.java @@ -10,6 +10,9 @@ import java.util.Iterator; import java.util.List; import java.util.Set; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.hibernate.Session; import org.hibernate.Transaction; @@ -28,31 +31,32 @@ import static org.junit.Assert.fail; */ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { @Test - public void testDefault() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); + public void testDefault() { File doc = new Document( "Enron Stuff To Shred", 1000 ); Folder folder = new Folder( "Enron" ); - s.persist( doc ); - s.persist( folder ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - List result = s.createCriteria( File.class ).list(); - assertNotNull( result ); - assertEquals( 2, result.size() ); - File f2 = (File) result.get( 0 ); - checkClassType( f2, doc, folder ); - f2 = (File) result.get( 1 ); - checkClassType( f2, doc, folder ); - s.delete( result.get( 0 ) ); - s.delete( result.get( 1 ) ); - tx.commit(); - s.close(); + inTransaction( + s -> { + s.persist( doc ); + s.persist( folder ); + } + ); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( File.class ); + criteria.from( File.class ); + List result = s.createQuery( criteria ).list(); +// List result = s.createCriteria( File.class ).list(); + assertNotNull( result ); + assertEquals( 2, result.size() ); + File f2 = result.get( 0 ); + checkClassType( f2, doc, folder ); + f2 = result.get( 1 ); + checkClassType( f2, doc, folder ); + s.delete( result.get( 0 ) ); + s.delete( result.get( 1 ) ); + } + ); } @Test @@ -62,22 +66,30 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { ProgramExecution remove = new ProgramExecution(); remove.setAction( "remove" ); remove.setAppliesOn( f ); - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); - s.persist( f ); - s.persist( remove ); - tx.commit(); - s.clear(); - tx = s.beginTransaction(); - remove = (ProgramExecution) s.get( ProgramExecution.class, remove.getId() ); - assertNotNull( remove ); - assertNotNull( remove.getAppliesOn().getName() ); - s.delete( remove ); - s.delete( remove.getAppliesOn() ); - tx.commit(); - s.close(); + + try(Session s = openSession()) { + Transaction tx = s.beginTransaction(); + try { + s.persist( f ); + s.persist( remove ); + tx.commit(); + s.clear(); + tx = s.beginTransaction(); + remove = s.get( ProgramExecution.class, remove.getId() ); + assertNotNull( remove ); + assertNotNull( remove.getAppliesOn().getName() ); + s.delete( remove ); + s.delete( remove.getAppliesOn() ); + + tx.commit(); + } + catch (Exception e) { + if ( s.getTransaction().isActive() ) { + s.getTransaction().rollback(); + } + throw e; + } + } } private void checkClassType(File fruitToTest, File f, Folder a) { @@ -93,7 +105,7 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { } @Test - public void testJoinedAbstractClass() throws Exception { + public void testJoinedAbstractClass() { Session s; s = openSession(); s.getTransaction().begin(); @@ -107,18 +119,18 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { s = openSession(); s.getTransaction().begin(); - sw = (Sweater) s.get( Sweater.class, sw.getId() ); + sw = s.get( Sweater.class, sw.getId() ); s.delete( sw ); s.getTransaction().commit(); s.close(); } @Test - public void testInheritance() throws Exception { + public void testInheritance() { Session session = openSession(); Transaction transaction = session.beginTransaction(); String eventPK = "event1"; - EventInformation event = (EventInformation) session.get( EventInformation.class, eventPK ); + EventInformation event = session.get( EventInformation.class, eventPK ); if ( event == null ) { event = new EventInformation(); event.setNotificationId( eventPK ); @@ -162,13 +174,13 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase { s.flush(); s.clear(); - c1 = (Client) s.load(Client.class, c1.getId()); + c1 = s.load(Client.class, c1.getId()); assertEquals( 5000.0, c1.getAccount().getBalance(), 0.01 ); s.flush(); s.clear(); - a1 = (Account) s.load(Account.class,a1.getId()); + a1 = s.load(Account.class,a1.getId()); Set clients = a1.getClients(); assertEquals(1, clients.size()); Iterator it = clients.iterator(); diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/mixed/SubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/mixed/SubclassTest.java index 3f34271d96..5ef5139700 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/mixed/SubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/mixed/SubclassTest.java @@ -8,6 +8,9 @@ package org.hibernate.test.annotations.inheritance.mixed; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.junit.Test; import org.hibernate.Session; @@ -26,16 +29,17 @@ import static org.junit.Assert.fail; */ public class SubclassTest extends BaseCoreFunctionalTestCase { @Test - public void testDefault() throws Exception { + public void testDefault() { Session s; Transaction tx; s = openSession(); tx = s.beginTransaction(); File doc = new Document( "Enron Stuff To Shred", 1000 ); Folder folder = new Folder( "Enron" ); - s.persist( doc ); - s.persist( folder ); try { + s.persist( doc ); + s.persist( folder ); + tx.commit(); } catch (SQLGrammarException e) { @@ -43,19 +47,24 @@ public class SubclassTest extends BaseCoreFunctionalTestCase { } s.close(); - s = openSession(); - tx = s.beginTransaction(); - List result = s.createCriteria( File.class ).list(); - assertNotNull( result ); - assertEquals( 2, result.size() ); - File f2 = (File) result.get( 0 ); - checkClassType( f2, doc, folder ); - f2 = (File) result.get( 1 ); - checkClassType( f2, doc, folder ); - s.delete( result.get( 0 ) ); - s.delete( result.get( 1 ) ); - tx.commit(); - s.close(); + inTransaction( + session -> { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( File.class ); + criteria.from( File.class ); + List result = session.createQuery( criteria ).list(); + +// List result = session.createCriteria( File.class ).list(); + assertNotNull( result ); + assertEquals( 2, result.size() ); + File f2 = result.get( 0 ); + checkClassType( f2, doc, folder ); + f2 = result.get( 1 ); + checkClassType( f2, doc, folder ); + session.delete( result.get( 0 ) ); + session.delete( result.get( 1 ) ); + } + ); } private void checkClassType(File fruitToTest, File f, Folder a) { diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/union/SubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/union/SubclassTest.java index 15e600f3f8..6b7f8b692e 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/union/SubclassTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/inheritance/union/SubclassTest.java @@ -8,6 +8,9 @@ package org.hibernate.test.annotations.inheritance.union; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.junit.Test; import org.hibernate.Session; @@ -26,29 +29,31 @@ import static org.junit.Assert.fail; */ public class SubclassTest extends BaseCoreFunctionalTestCase { @Test - public void testDefault() throws Exception { - Session s; - Transaction tx; - s = openSession(); - tx = s.beginTransaction(); + public void testDefault() { File doc = new Document( "Enron Stuff To Shred", 1000 ); Folder folder = new Folder( "Enron" ); - s.persist( doc ); - s.persist( folder ); - tx.commit(); - s.close(); + inTransaction( + s -> { + s.persist( doc ); + s.persist( folder ); + } + ); - s = openSession(); - tx = s.beginTransaction(); - List result = s.createCriteria( File.class ).list(); - assertNotNull( result ); - assertEquals( 2, result.size() ); - File f2 = (File) result.get( 0 ); - checkClassType( f2, doc, folder ); - f2 = (File) result.get( 1 ); - checkClassType( f2, doc, folder ); - tx.commit(); - s.close(); + inTransaction( + s -> { + CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( File.class ); + criteria.from( File.class ); + List result = s.createQuery( criteria ).list(); +// List result = s.createCriteria( File.class ).list(); + assertNotNull( result ); + assertEquals( 2, result.size() ); + File f2 = result.get( 0 ); + checkClassType( f2, doc, folder ); + f2 = result.get( 1 ); + checkClassType( f2, doc, folder ); + } + ); } private void checkClassType(File fruitToTest, File f, Folder a) {