From ec086cd29995431d26efd236489d700b71942ac6 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Mon, 22 Jul 2019 10:58:13 +0100 Subject: [PATCH] 6 - SQM based on JPA type system --- .../test/c3p0/StatementCacheTest.java | 78 ++++++++++--------- .../ehcache/test/domain/EventManager.java | 54 +++++++++---- .../jcache/test/domain/EventManager.java | 52 +++++++++---- 3 files changed, 118 insertions(+), 66 deletions(-) diff --git a/hibernate-c3p0/src/test/java/org/hibernate/test/c3p0/StatementCacheTest.java b/hibernate-c3p0/src/test/java/org/hibernate/test/c3p0/StatementCacheTest.java index 938bf97937..5c9169d3e2 100644 --- a/hibernate-c3p0/src/test/java/org/hibernate/test/c3p0/StatementCacheTest.java +++ b/hibernate-c3p0/src/test/java/org/hibernate/test/c3p0/StatementCacheTest.java @@ -8,6 +8,9 @@ package org.hibernate.test.c3p0; import java.util.List; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + import org.hibernate.Session; import org.junit.Assert; @@ -25,46 +28,51 @@ public class StatementCacheTest extends BaseCoreFunctionalTestCase { @Test @TestForIssue( jiraKey = "HHH-7193" ) public void testStatementCaching() { - Session session = openSession(); - session.beginTransaction(); + inSession( + session -> { + session.beginTransaction(); - //save 2 new entities, one valid, one invalid (neither should be persisted) - IrrelevantEntity irrelevantEntity = new IrrelevantEntity(); - irrelevantEntity.setName( "valid 1" ); - session.save( irrelevantEntity ); - //name is required - irrelevantEntity = new IrrelevantEntity(); - session.save( irrelevantEntity ); - try { - session.flush(); - Assert.fail( "Validation exception did not occur" ); - } - catch (Exception e) { - //this is expected roll the transaction back - session.getTransaction().rollback(); - } - session.close(); + //save 2 new entities, one valid, one invalid (neither should be persisted) + IrrelevantEntity irrelevantEntity = new IrrelevantEntity(); + irrelevantEntity.setName( "valid 1" ); + session.save( irrelevantEntity ); + //name is required + irrelevantEntity = new IrrelevantEntity(); + session.save( irrelevantEntity ); + try { + session.flush(); + Assert.fail( "Validation exception did not occur" ); + } + catch (Exception e) { + //this is expected roll the transaction back + session.getTransaction().rollback(); + } + } + ); - session = openSession(); - session.beginTransaction(); - - //save a new entity and commit it - irrelevantEntity = new IrrelevantEntity(); - irrelevantEntity.setName( "valid 2" ); - session.save( irrelevantEntity ); - session.flush(); - session.getTransaction().commit(); - session.close(); + inTransaction( + session -> { + //save a new entity and commit it + IrrelevantEntity irrelevantEntity = new IrrelevantEntity(); + irrelevantEntity.setName( "valid 2" ); + session.save( irrelevantEntity ); + session.flush(); + } + ); //only one entity should have been inserted to the database (if the statement in the cache wasn't cleared then it would have inserted both entities) - session = openSession(); - session.beginTransaction(); - Criteria criteria = session.createCriteria( IrrelevantEntity.class ); - List results = criteria.list(); - session.getTransaction().commit(); - session.close(); + inTransaction( + session -> { + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( IrrelevantEntity.class ); + criteria.from( IrrelevantEntity.class ); + List results = session.createQuery( criteria ).list(); - Assert.assertEquals( 1, results.size() ); +// Criteria criteria = session.createCriteria( IrrelevantEntity.class ); +// List results = criteria.list(); + Assert.assertEquals( 1, results.size() ); + } + ); } @Override diff --git a/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/domain/EventManager.java b/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/domain/EventManager.java index e69607fa3c..1f204119a7 100644 --- a/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/domain/EventManager.java +++ b/hibernate-ehcache/src/test/java/org/hibernate/cache/ehcache/test/domain/EventManager.java @@ -12,7 +12,10 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import org.hibernate.Query; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + +import org.hibernate.query.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -104,17 +107,24 @@ public class EventManager { Session session = sessionFactory.getCurrentSession(); - session.beginTransaction(); + try { + session.beginTransaction(); + Query query = session.createQuery( "from Event ev where ev.organizer = :organizer" ); - Query query = session.createQuery("from Event ev where ev.organizer = :organizer"); + query.setCacheable( true ); + query.setParameter( "organizer", organizer ); + List result = query.list(); - query.setCacheable(true); - query.setEntity("organizer", organizer); - List result = query.list(); + session.getTransaction().commit(); - session.getTransaction().commit(); - - return result; + return result; + } + catch (Exception e) { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + throw e; + } } /** @@ -123,15 +133,25 @@ public class EventManager { public List listEventsWithCriteria() { Session session = sessionFactory.getCurrentSession(); - session.beginTransaction(); + try { + session.beginTransaction(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Event.class ); + criteria.from( Event.class ); + List result = session.createQuery( criteria ).setCacheable( true ).list(); +// List result = session.createCriteria(Event.class) +// .setCacheable(true) +// .list(); - List result = session.createCriteria(Event.class) - .setCacheable(true) - .list(); - - session.getTransaction().commit(); - - return result; + session.getTransaction().commit(); + return result; + } + catch (Exception e) { + if ( session.getTransaction().isActive() ) { + session.getTransaction().rollback(); + } + throw e; + } } public void addPersonToEvent(Long personId, Long eventId) { diff --git a/hibernate-jcache/src/test/java/org/hibernate/jcache/test/domain/EventManager.java b/hibernate-jcache/src/test/java/org/hibernate/jcache/test/domain/EventManager.java index 4109533969..05b3aab39d 100644 --- a/hibernate-jcache/src/test/java/org/hibernate/jcache/test/domain/EventManager.java +++ b/hibernate-jcache/src/test/java/org/hibernate/jcache/test/domain/EventManager.java @@ -12,7 +12,10 @@ import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import org.hibernate.Query; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; + +import org.hibernate.query.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; @@ -104,17 +107,25 @@ public class EventManager { Session session = sessionFactory.getCurrentSession(); - session.beginTransaction(); + try { + session.beginTransaction(); - Query query = session.createQuery("from Event ev where ev.organizer = :organizer"); + Query query = session.createQuery( "from Event ev where ev.organizer = :organizer" ); - query.setCacheable(true); - query.setEntity("organizer", organizer); - List result = query.list(); + query.setCacheable( true ); + query.setParameter( "organizer", organizer ); + List result = query.list(); - session.getTransaction().commit(); + session.getTransaction().commit(); - return result; + return result; + } + catch (Exception e){ + if(session.getTransaction().isActive()){ + session.getTransaction().rollback(); + } + throw e; + } } /** @@ -123,15 +134,28 @@ public class EventManager { public List listEventsWithCriteria() { Session session = sessionFactory.getCurrentSession(); - session.beginTransaction(); + try { + session.beginTransaction(); - List result = session.createCriteria(Event.class) - .setCacheable(true) - .list(); + CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder(); + CriteriaQuery criteria = criteriaBuilder.createQuery( Event.class ); + criteria.from( Event.class ); - session.getTransaction().commit(); + List result = session.createQuery( criteria ).setCacheable( true ).list(); +// List result = session.createCriteria( Event.class ) +// .setCacheable( true ) +// .list(); - return result; + session.getTransaction().commit(); + + return result; + } + catch (Exception e){ + if(session.getTransaction().isActive()){ + session.getTransaction().rollback(); + } + throw e; + } } public void addPersonToEvent(Long personId, Long eventId) {