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,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<IrrelevantEntity> criteria = criteriaBuilder.createQuery( IrrelevantEntity.class );
|
||||
criteria.from( IrrelevantEntity.class );
|
||||
List<IrrelevantEntity> 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
|
||||
|
|
|
@ -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<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();
|
||||
|
||||
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) {
|
||||
|
|
|
@ -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<Event> criteria = criteriaBuilder.createQuery( Event.class );
|
||||
criteria.from( Event.class );
|
||||
|
||||
session.getTransaction().commit();
|
||||
List<Event> 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) {
|
||||
|
|
Loading…
Reference in New Issue