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 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,46 +28,51 @@ 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.beginTransaction();
|
session -> {
|
||||||
|
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)
|
||||||
IrrelevantEntity irrelevantEntity = new IrrelevantEntity();
|
IrrelevantEntity irrelevantEntity = new IrrelevantEntity();
|
||||||
irrelevantEntity.setName( "valid 1" );
|
irrelevantEntity.setName( "valid 1" );
|
||||||
session.save( irrelevantEntity );
|
session.save( irrelevantEntity );
|
||||||
//name is required
|
//name is required
|
||||||
irrelevantEntity = new IrrelevantEntity();
|
irrelevantEntity = new IrrelevantEntity();
|
||||||
session.save( irrelevantEntity );
|
session.save( irrelevantEntity );
|
||||||
try {
|
try {
|
||||||
session.flush();
|
session.flush();
|
||||||
Assert.fail( "Validation exception did not occur" );
|
Assert.fail( "Validation exception did not occur" );
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
//this is expected roll the transaction back
|
//this is expected roll the transaction back
|
||||||
session.getTransaction().rollback();
|
session.getTransaction().rollback();
|
||||||
}
|
}
|
||||||
session.close();
|
}
|
||||||
|
);
|
||||||
|
|
||||||
session = openSession();
|
inTransaction(
|
||||||
session.beginTransaction();
|
session -> {
|
||||||
|
//save a new entity and commit it
|
||||||
//save a new entity and commit it
|
IrrelevantEntity irrelevantEntity = new 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();
|
||||||
|
|
||||||
Assert.assertEquals( 1, results.size() );
|
// Criteria criteria = session.createCriteria( IrrelevantEntity.class );
|
||||||
|
// List results = criteria.list();
|
||||||
|
Assert.assertEquals( 1, results.size() );
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -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,17 +107,24 @@ public class EventManager {
|
||||||
|
|
||||||
Session session = sessionFactory.getCurrentSession();
|
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);
|
session.getTransaction().commit();
|
||||||
query.setEntity("organizer", organizer);
|
|
||||||
List result = query.list();
|
|
||||||
|
|
||||||
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() {
|
public List listEventsWithCriteria() {
|
||||||
Session session = sessionFactory.getCurrentSession();
|
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)
|
session.getTransaction().commit();
|
||||||
.setCacheable(true)
|
return result;
|
||||||
.list();
|
}
|
||||||
|
catch (Exception e) {
|
||||||
session.getTransaction().commit();
|
if ( session.getTransaction().isActive() ) {
|
||||||
|
session.getTransaction().rollback();
|
||||||
return result;
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPersonToEvent(Long personId, Long eventId) {
|
public void addPersonToEvent(Long personId, Long eventId) {
|
||||||
|
|
|
@ -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,17 +107,25 @@ public class EventManager {
|
||||||
|
|
||||||
Session session = sessionFactory.getCurrentSession();
|
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.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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -123,15 +134,28 @@ public class EventManager {
|
||||||
public List listEventsWithCriteria() {
|
public List listEventsWithCriteria() {
|
||||||
Session session = sessionFactory.getCurrentSession();
|
Session session = sessionFactory.getCurrentSession();
|
||||||
|
|
||||||
session.beginTransaction();
|
try {
|
||||||
|
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 );
|
||||||
|
|
||||||
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) {
|
public void addPersonToEvent(Long personId, Long eventId) {
|
||||||
|
|
Loading…
Reference in New Issue