6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-22 10:58:13 +01:00
parent b196374da9
commit ec086cd299
3 changed files with 118 additions and 66 deletions

View File

@ -8,6 +8,9 @@ package org.hibernate.test.c3p0;
import java.util.List; import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.Session; import org.hibernate.Session;
import org.junit.Assert; import org.junit.Assert;
@ -25,7 +28,8 @@ public class StatementCacheTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-7193" ) @TestForIssue( jiraKey = "HHH-7193" )
public void testStatementCaching() { public void testStatementCaching() {
Session session = openSession(); inSession(
session -> {
session.beginTransaction(); session.beginTransaction();
//save 2 new entities, one valid, one invalid (neither should be persisted) //save 2 new entities, one valid, one invalid (neither should be persisted)
@ -43,29 +47,33 @@ public class StatementCacheTest extends BaseCoreFunctionalTestCase {
//this is expected roll the transaction back //this is expected roll the transaction back
session.getTransaction().rollback(); session.getTransaction().rollback();
} }
session.close(); }
);
session = openSession();
session.beginTransaction();
inTransaction(
session -> {
//save a new entity and commit it //save a new entity and commit it
irrelevantEntity = new IrrelevantEntity(); IrrelevantEntity irrelevantEntity = new IrrelevantEntity();
irrelevantEntity.setName( "valid 2" ); irrelevantEntity.setName( "valid 2" );
session.save( irrelevantEntity ); session.save( irrelevantEntity );
session.flush(); session.flush();
session.getTransaction().commit(); }
session.close(); );
//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) //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(); inTransaction(
session.beginTransaction(); session -> {
Criteria criteria = session.createCriteria( IrrelevantEntity.class ); CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
List results = criteria.list(); CriteriaQuery<IrrelevantEntity> criteria = criteriaBuilder.createQuery( IrrelevantEntity.class );
session.getTransaction().commit(); criteria.from( IrrelevantEntity.class );
session.close(); List<IrrelevantEntity> results = session.createQuery( criteria ).list();
// Criteria criteria = session.createCriteria( IrrelevantEntity.class );
// List results = criteria.list();
Assert.assertEquals( 1, results.size() ); Assert.assertEquals( 1, results.size() );
} }
);
}
@Override @Override
protected Class<?>[] getAnnotatedClasses() { protected Class<?>[] getAnnotatedClasses() {

View File

@ -12,7 +12,10 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; 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.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@ -104,18 +107,25 @@ public class EventManager {
Session session = sessionFactory.getCurrentSession(); Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction(); 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.setCacheable( true );
query.setEntity("organizer", organizer); query.setParameter( "organizer", organizer );
List result = query.list(); 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;
}
}
/** /**
* Use a Criteria query - see FORGE-247 * Use a Criteria query - see FORGE-247
@ -123,16 +133,26 @@ public class EventManager {
public List listEventsWithCriteria() { public List listEventsWithCriteria() {
Session session = sessionFactory.getCurrentSession(); Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction(); session.beginTransaction();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
List result = session.createCriteria(Event.class) CriteriaQuery<Event> criteria = criteriaBuilder.createQuery( Event.class );
.setCacheable(true) criteria.from( Event.class );
.list(); List<Event> result = session.createQuery( criteria ).setCacheable( true ).list();
// List result = session.createCriteria(Event.class)
// .setCacheable(true)
// .list();
session.getTransaction().commit(); session.getTransaction().commit();
return result; return result;
} }
catch (Exception e) {
if ( session.getTransaction().isActive() ) {
session.getTransaction().rollback();
}
throw e;
}
}
public void addPersonToEvent(Long personId, Long eventId) { public void addPersonToEvent(Long personId, Long eventId) {

View File

@ -12,7 +12,10 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.ListIterator; 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.Session;
import org.hibernate.SessionFactory; import org.hibernate.SessionFactory;
@ -104,18 +107,26 @@ public class EventManager {
Session session = sessionFactory.getCurrentSession(); Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction(); 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.setCacheable( true );
query.setEntity("organizer", organizer); query.setParameter( "organizer", organizer );
List result = query.list(); 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;
}
}
/** /**
* Use a Criteria query - see FORGE-247 * Use a Criteria query - see FORGE-247
@ -123,16 +134,29 @@ public class EventManager {
public List listEventsWithCriteria() { public List listEventsWithCriteria() {
Session session = sessionFactory.getCurrentSession(); Session session = sessionFactory.getCurrentSession();
try {
session.beginTransaction(); session.beginTransaction();
List result = session.createCriteria(Event.class) CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
.setCacheable(true) CriteriaQuery<Event> criteria = criteriaBuilder.createQuery( Event.class );
.list(); criteria.from( Event.class );
List<Event> result = session.createQuery( criteria ).setCacheable( true ).list();
// List result = session.createCriteria( Event.class )
// .setCacheable( true )
// .list();
session.getTransaction().commit(); session.getTransaction().commit();
return result; return result;
} }
catch (Exception e){
if(session.getTransaction().isActive()){
session.getTransaction().rollback();
}
throw e;
}
}
public void addPersonToEvent(Long personId, Long eventId) { public void addPersonToEvent(Long personId, Long eventId) {