6 - SQM based on JPA type system
This commit is contained in:
parent
b196374da9
commit
ec086cd299
|
@ -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,7 +28,8 @@ public class StatementCacheTest extends BaseCoreFunctionalTestCase {
|
|||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-7193" )
|
||||
public void testStatementCaching() {
|
||||
Session session = openSession();
|
||||
inSession(
|
||||
session -> {
|
||||
session.beginTransaction();
|
||||
|
||||
//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
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
session -> {
|
||||
//save a new entity and commit it
|
||||
irrelevantEntity = new IrrelevantEntity();
|
||||
IrrelevantEntity irrelevantEntity = new IrrelevantEntity();
|
||||
irrelevantEntity.setName( "valid 2" );
|
||||
session.save( irrelevantEntity );
|
||||
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)
|
||||
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<IrrelevantEntity> criteria = criteriaBuilder.createQuery( IrrelevantEntity.class );
|
||||
criteria.from( IrrelevantEntity.class );
|
||||
List<IrrelevantEntity> results = session.createQuery( criteria ).list();
|
||||
|
||||
// Criteria criteria = session.createCriteria( IrrelevantEntity.class );
|
||||
// List results = criteria.list();
|
||||
Assert.assertEquals( 1, results.size() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
|
|
|
@ -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,18 +107,25 @@ public class EventManager {
|
|||
|
||||
Session session = sessionFactory.getCurrentSession();
|
||||
|
||||
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);
|
||||
query.setCacheable( true );
|
||||
query.setParameter( "organizer", organizer );
|
||||
List result = query.list();
|
||||
|
||||
session.getTransaction().commit();
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( session.getTransaction().isActive() ) {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a Criteria query - see FORGE-247
|
||||
|
@ -123,16 +133,26 @@ public class EventManager {
|
|||
public List listEventsWithCriteria() {
|
||||
Session session = sessionFactory.getCurrentSession();
|
||||
|
||||
try {
|
||||
session.beginTransaction();
|
||||
|
||||
List result = session.createCriteria(Event.class)
|
||||
.setCacheable(true)
|
||||
.list();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Event> criteria = criteriaBuilder.createQuery( Event.class );
|
||||
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();
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e) {
|
||||
if ( session.getTransaction().isActive() ) {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void addPersonToEvent(Long personId, Long eventId) {
|
||||
|
||||
|
|
|
@ -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,18 +107,26 @@ public class EventManager {
|
|||
|
||||
Session session = sessionFactory.getCurrentSession();
|
||||
|
||||
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);
|
||||
query.setCacheable( true );
|
||||
query.setParameter( "organizer", organizer );
|
||||
List result = query.list();
|
||||
|
||||
session.getTransaction().commit();
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e){
|
||||
if(session.getTransaction().isActive()){
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a Criteria query - see FORGE-247
|
||||
|
@ -123,16 +134,29 @@ public class EventManager {
|
|||
public List listEventsWithCriteria() {
|
||||
Session session = sessionFactory.getCurrentSession();
|
||||
|
||||
try {
|
||||
session.beginTransaction();
|
||||
|
||||
List result = session.createCriteria(Event.class)
|
||||
.setCacheable(true)
|
||||
.list();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Event> criteria = criteriaBuilder.createQuery( Event.class );
|
||||
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();
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception e){
|
||||
if(session.getTransaction().isActive()){
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void addPersonToEvent(Long personId, Long eventId) {
|
||||
|
||||
|
|
Loading…
Reference in New Issue