6 - SQM based on JPA type system
This commit is contained in:
parent
d99a3f1938
commit
f436d18bf2
|
@ -37,6 +37,8 @@ import javax.persistence.OneToMany;
|
|||
import javax.persistence.OrderColumn;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.HibernateException;
|
||||
|
@ -70,24 +72,27 @@ public abstract class UserCollectionTypeTest extends BaseCoreFunctionalTestCase
|
|||
|
||||
@Test
|
||||
public void testBasicOperation() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
User u = new User("max");
|
||||
u.getEmailAddresses().add( new Email("max@hibernate.org") );
|
||||
u.getEmailAddresses().add( new Email("max.andersen@jboss.com") );
|
||||
s.persist(u);
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
User u2 = (User) s.createCriteria(User.class).uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
assertNotNull( u2.getEmailAddresses().head());
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = new User("max");
|
||||
u.getEmailAddresses().add( new Email("max@hibernate.org") );
|
||||
u.getEmailAddresses().add( new Email("max.andersen@jboss.com") );
|
||||
s.persist(u);
|
||||
}
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
|
||||
criteria.from( User.class );
|
||||
User u2 = s.createQuery( criteria ).uniqueResult();
|
||||
// User u2 = (User) s.createCriteria(User.class).uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
assertNotNull( u2.getEmailAddresses().head());
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -44,6 +44,8 @@ import javax.persistence.OneToMany;
|
|||
import javax.persistence.OrderColumn;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Transient;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.HibernateException;
|
||||
|
@ -89,7 +91,11 @@ public class UserCollectionTypeTest extends BaseCoreFunctionalTestCase {
|
|||
} );
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
User u2 = (User) session.createCriteria( User.class ).uniqueResult();
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
|
||||
criteria.from( User.class );
|
||||
User u2 = session.createQuery( criteria ).uniqueResult();
|
||||
// User u2 = (User) session.createCriteria( User.class ).uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
assertNotNull( u2.getEmailAddresses().head() );
|
||||
|
|
|
@ -7,12 +7,13 @@
|
|||
package org.hibernate.test.collection.original;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.dialect.AbstractHANADialect;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
|
@ -38,132 +39,135 @@ public class CollectionTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testExtraLazy() throws HibernateException, SQLException {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
User u = new User( "gavin" );
|
||||
u.getPermissions().add( new Permission( "obnoxiousness" ) );
|
||||
u.getPermissions().add( new Permission( "pigheadedness" ) );
|
||||
u.getSessionData().put( "foo", "foo value" );
|
||||
s.persist( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
u = ( User ) s.get( User.class, "gavin" );
|
||||
public void testExtraLazy() throws HibernateException {
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = new User( "gavin" );
|
||||
u.getPermissions().add( new Permission( "obnoxiousness" ) );
|
||||
u.getPermissions().add( new Permission( "pigheadedness" ) );
|
||||
u.getSessionData().put( "foo", "foo value" );
|
||||
s.persist( u );
|
||||
}
|
||||
);
|
||||
|
||||
assertFalse( Hibernate.isInitialized( u.getPermissions() ) );
|
||||
assertEquals( u.getPermissions().size(), 2 );
|
||||
assertTrue( u.getPermissions().contains( new Permission( "obnoxiousness" ) ) );
|
||||
assertFalse( u.getPermissions().contains( new Permission( "silliness" ) ) );
|
||||
assertNotNull( u.getPermissions().get( 1 ) );
|
||||
assertNull( u.getPermissions().get( 3 ) );
|
||||
assertFalse( Hibernate.isInitialized( u.getPermissions() ) );
|
||||
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
assertEquals( u.getSessionData().size(), 1 );
|
||||
assertTrue( u.getSessionData().containsKey( "foo" ) );
|
||||
assertFalse( u.getSessionData().containsKey( "bar" ) );
|
||||
assertTrue( u.getSessionData().containsValue( "foo value" ) );
|
||||
assertFalse( u.getSessionData().containsValue( "bar" ) );
|
||||
assertEquals( "foo value", u.getSessionData().get( "foo" ) );
|
||||
assertNull( u.getSessionData().get( "bar" ) );
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = s.get( User.class, "gavin" );
|
||||
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
u.getSessionData().put( "bar", "bar value" );
|
||||
u.getSessionAttributeNames().add( "bar" );
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionAttributeNames() ) );
|
||||
assertTrue( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
assertFalse( Hibernate.isInitialized( u.getPermissions() ) );
|
||||
assertEquals( u.getPermissions().size(), 2 );
|
||||
assertTrue( u.getPermissions().contains( new Permission( "obnoxiousness" ) ) );
|
||||
assertFalse( u.getPermissions().contains( new Permission( "silliness" ) ) );
|
||||
assertNotNull( u.getPermissions().get( 1 ) );
|
||||
assertNull( u.getPermissions().get( 3 ) );
|
||||
assertFalse( Hibernate.isInitialized( u.getPermissions() ) );
|
||||
|
||||
s.delete( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
assertEquals( u.getSessionData().size(), 1 );
|
||||
assertTrue( u.getSessionData().containsKey( "foo" ) );
|
||||
assertFalse( u.getSessionData().containsKey( "bar" ) );
|
||||
assertTrue( u.getSessionData().containsValue( "foo value" ) );
|
||||
assertFalse( u.getSessionData().containsValue( "bar" ) );
|
||||
assertEquals( "foo value", u.getSessionData().get( "foo" ) );
|
||||
assertNull( u.getSessionData().get( "bar" ) );
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
u.getSessionData().put( "bar", "bar value" );
|
||||
u.getSessionAttributeNames().add( "bar" );
|
||||
assertFalse( Hibernate.isInitialized( u.getSessionAttributeNames() ) );
|
||||
assertTrue( Hibernate.isInitialized( u.getSessionData() ) );
|
||||
|
||||
s.delete( u );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMerge() throws HibernateException, SQLException {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
User u = new User( "gavin" );
|
||||
u.getPermissions().add( new Permission( "obnoxiousness" ) );
|
||||
u.getPermissions().add( new Permission( "pigheadedness" ) );
|
||||
s.persist( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
u.getPermissions().add( new Permission( "obnoxiousness" ) );
|
||||
u.getPermissions().add( new Permission( "pigheadedness" ) );
|
||||
s.persist( u );
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
User u2 = ( User ) s.createCriteria( User.class ).uniqueResult();
|
||||
u2.setPermissions( null ); //forces one shot delete
|
||||
s.merge( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u2 = findUser( s );
|
||||
u2.setPermissions( null ); //forces one shot delete
|
||||
s.merge( u );
|
||||
}
|
||||
);
|
||||
|
||||
u.getPermissions().add( new Permission( "silliness" ) );
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.merge( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> s.merge( u )
|
||||
);
|
||||
;
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
u2 = ( User ) s.createCriteria( User.class ).uniqueResult();
|
||||
assertEquals( u2.getPermissions().size(), 3 );
|
||||
assertEquals( ( ( Permission ) u2.getPermissions().get( 0 ) ).getType(), "obnoxiousness" );
|
||||
assertEquals( ( ( Permission ) u2.getPermissions().get( 2 ) ).getType(), "silliness" );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u2 = findUser( s );
|
||||
assertEquals( u2.getPermissions().size(), 3 );
|
||||
assertEquals( ( (Permission) u2.getPermissions().get( 0 ) ).getType(), "obnoxiousness" );
|
||||
assertEquals( ( (Permission) u2.getPermissions().get( 2 ) ).getType(), "silliness" );
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.delete( u2 );
|
||||
s.flush();
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u2 = findUser( s );
|
||||
s.delete( u2 );
|
||||
s.flush();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFetch() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
User u = new User( "gavin" );
|
||||
u.getPermissions().add( new Permission( "obnoxiousness" ) );
|
||||
u.getPermissions().add( new Permission( "pigheadedness" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@hibernate.org" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin.king@jboss.com" ) );
|
||||
s.persist( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = new User( "gavin" );
|
||||
u.getPermissions().add( new Permission( "obnoxiousness" ) );
|
||||
u.getPermissions().add( new Permission( "pigheadedness" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@hibernate.org" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin.king@jboss.com" ) );
|
||||
s.persist( u );
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
User u2 = ( User ) s.createCriteria( User.class ).uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
|
||||
assertFalse( Hibernate.isInitialized( u2.getPermissions() ) );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
s.delete( u2 );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u2 = findUser( s );
|
||||
assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
|
||||
assertFalse( Hibernate.isInitialized( u2.getPermissions() ) );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
s.delete( u2 );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateOrder() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
User u = new User( "gavin" );
|
||||
u.getSessionData().put( "foo", "foo value" );
|
||||
u.getSessionData().put( "bar", "bar value" );
|
||||
u.getEmailAddresses().add( new Email( "gavin.king@jboss.com" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@hibernate.org" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@illflow.com" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@nospam.com" ) );
|
||||
s.persist( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
u.getSessionData().put( "foo", "foo value" );
|
||||
u.getSessionData().put( "bar", "bar value" );
|
||||
u.getEmailAddresses().add( new Email( "gavin.king@jboss.com" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@hibernate.org" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@illflow.com" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin@nospam.com" ) );
|
||||
s.persist( u );
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
u.getSessionData().clear();
|
||||
u.getSessionData().put( "baz", "baz value" );
|
||||
|
@ -171,94 +175,95 @@ public class CollectionTest extends BaseCoreFunctionalTestCase {
|
|||
u.getEmailAddresses().remove( 0 );
|
||||
u.getEmailAddresses().remove( 2 );
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.update( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> s.update( u )
|
||||
);
|
||||
|
||||
u.getSessionData().clear();
|
||||
u.getEmailAddresses().add( 0, new Email( "gavin@nospam.com" ) );
|
||||
u.getEmailAddresses().add( new Email( "gavin.king@jboss.com" ) );
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.update( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.delete( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> s.update( u )
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
s -> s.delete( u )
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValueMap() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
User u = new User( "gavin" );
|
||||
u.getSessionData().put( "foo", "foo value" );
|
||||
u.getSessionData().put( "bar", null );
|
||||
u.getEmailAddresses().add( null );
|
||||
u.getEmailAddresses().add( new Email( "gavin.king@jboss.com" ) );
|
||||
u.getEmailAddresses().add( null );
|
||||
u.getEmailAddresses().add( null );
|
||||
s.persist( u );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = new User( "gavin" );
|
||||
u.getSessionData().put( "foo", "foo value" );
|
||||
u.getSessionData().put( "bar", null );
|
||||
u.getEmailAddresses().add( null );
|
||||
u.getEmailAddresses().add( new Email( "gavin.king@jboss.com" ) );
|
||||
u.getEmailAddresses().add( null );
|
||||
u.getEmailAddresses().add( null );
|
||||
s.persist( u );
|
||||
}
|
||||
);
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u2 = findUser( s );
|
||||
// User u2 = (User) s.createCriteria( User.class ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( u2.getSessionData() ) );
|
||||
assertEquals( u2.getSessionData().size(), 1 );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
u2.getSessionData().put( "foo", "new foo value" );
|
||||
u2.getEmailAddresses().set( 1, new Email( "gavin@hibernate.org" ) );
|
||||
//u2.getEmailAddresses().remove(3);
|
||||
//u2.getEmailAddresses().remove(2);
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
User u2 = ( User ) s.createCriteria( User.class ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( u2.getSessionData() ) );
|
||||
assertEquals( u2.getSessionData().size(), 1 );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
u2.getSessionData().put( "foo", "new foo value" );
|
||||
u2.getEmailAddresses().set( 1, new Email( "gavin@hibernate.org" ) );
|
||||
//u2.getEmailAddresses().remove(3);
|
||||
//u2.getEmailAddresses().remove(2);
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u2 = findUser( s );
|
||||
// User u2 = (User) s.createCriteria( User.class ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( u2.getSessionData() ) );
|
||||
assertEquals( u2.getSessionData().size(), 1 );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
assertEquals( u2.getSessionData().get( "foo" ), "new foo value" );
|
||||
assertEquals( ( (Email) u2.getEmailAddresses().get( 1 ) ).getAddress(), "gavin@hibernate.org" );
|
||||
s.delete( u2 );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
u2 = ( User ) s.createCriteria( User.class ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( u2.getSessionData() ) );
|
||||
assertEquals( u2.getSessionData().size(), 1 );
|
||||
assertEquals( u2.getEmailAddresses().size(), 2 );
|
||||
assertEquals( u2.getSessionData().get( "foo" ), "new foo value" );
|
||||
assertEquals( ( ( Email ) u2.getEmailAddresses().get( 1 ) ).getAddress(), "gavin@hibernate.org" );
|
||||
s.delete( u2 );
|
||||
t.commit();
|
||||
s.close();
|
||||
private User findUser(SessionImplementor s) {
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<User> criteria = criteriaBuilder.createQuery( User.class );
|
||||
criteria.from( User.class );
|
||||
return s.createQuery( criteria ).uniqueResult();
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-3636" )
|
||||
@TestForIssue(jiraKey = "HHH-3636")
|
||||
@SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column")
|
||||
public void testCollectionInheritance() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Zoo zoo = new Zoo();
|
||||
Mammal m = new Mammal();
|
||||
m.setMammalName( "name1" );
|
||||
m.setMammalName2( "name2" );
|
||||
m.setMammalName3( "name3" );
|
||||
m.setZoo( zoo );
|
||||
zoo.getAnimals().add( m );
|
||||
Long id = ( Long ) s.save( zoo );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Mammal m = new Mammal();
|
||||
m.setMammalName( "name1" );
|
||||
m.setMammalName2( "name2" );
|
||||
m.setMammalName3( "name3" );
|
||||
m.setZoo( zoo );
|
||||
zoo.getAnimals().add( m );
|
||||
s.save( zoo );
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
Zoo found = ( Zoo ) s.get( Zoo.class, id );
|
||||
found.getAnimals().size();
|
||||
s.delete( found );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Zoo found = (Zoo) s.get( Zoo.class, zoo.getId() );
|
||||
found.getAnimals().size();
|
||||
s.delete( found );
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,9 +5,10 @@
|
|||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.fetchprofiles.join;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import java.util.List;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.Session;
|
||||
|
@ -15,7 +16,9 @@ import org.hibernate.UnknownProfileException;
|
|||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
@ -40,36 +43,40 @@ public class JoinFetchProfileTest extends BaseCoreFunctionalTestCase {
|
|||
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
|
||||
}
|
||||
|
||||
@SuppressWarnings( {"UnusedDeclaration"})
|
||||
private static interface TestData {
|
||||
public Long getStudentId();
|
||||
public Long getDepartmentId();
|
||||
public Long getCourseId();
|
||||
public Long getSectionId();
|
||||
public Long getEnrollmentId();
|
||||
@SuppressWarnings({ "UnusedDeclaration" })
|
||||
private interface TestData {
|
||||
Long getStudentId();
|
||||
|
||||
Long getDepartmentId();
|
||||
|
||||
Long getCourseId();
|
||||
|
||||
Long getSectionId();
|
||||
|
||||
Long getEnrollmentId();
|
||||
}
|
||||
|
||||
private interface TestCode {
|
||||
public void perform(TestData data);
|
||||
void perform(TestData data);
|
||||
}
|
||||
|
||||
@SuppressWarnings( {"unchecked"})
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
private void performWithStandardData(TestCode testCode) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
final Department literatureDepartment = new Department( "lit", "Literature" );
|
||||
session.save( literatureDepartment );
|
||||
final Course lit101 = new Course( new Course.Code( literatureDepartment, 101 ), "Introduction to Literature" );
|
||||
session.save( lit101 );
|
||||
final CourseOffering section = new CourseOffering( lit101, 1, 2008 );
|
||||
session.save( section );
|
||||
final Student me = new Student( "Steve" );
|
||||
session.save( me );
|
||||
final Course lit101 = new Course( new Course.Code( literatureDepartment, 101 ), "Introduction to Literature" );
|
||||
final CourseOffering section = new CourseOffering( lit101, 1, 2008 );
|
||||
final Enrollment enrollment = new Enrollment( section, me );
|
||||
section.getEnrollments().add( enrollment );
|
||||
session.save( enrollment );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.save( literatureDepartment );
|
||||
session.save( lit101 );
|
||||
session.save( section );
|
||||
session.save( me );
|
||||
section.getEnrollments().add( enrollment );
|
||||
session.save( enrollment );
|
||||
}
|
||||
);
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
|
@ -97,68 +104,80 @@ public class JoinFetchProfileTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
);
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
session.delete( enrollment );
|
||||
session.delete( me );
|
||||
session.delete( enrollment.getOffering() );
|
||||
session.delete( enrollment.getOffering().getCourse() );
|
||||
session.delete( enrollment.getOffering().getCourse().getCode().getDepartment() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.delete( enrollment );
|
||||
session.delete( me );
|
||||
session.delete( enrollment.getOffering() );
|
||||
session.delete( enrollment.getOffering().getCourse() );
|
||||
session.delete( enrollment.getOffering().getCourse().getCode().getDepartment() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalLoading() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityLoadCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse().getCode().getDepartment() ) );
|
||||
assertTrue( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
CourseOffering section = session.get( CourseOffering.class, data.getSectionId() );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityLoadCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse()
|
||||
.getCode()
|
||||
.getDepartment() ) );
|
||||
assertTrue( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
}
|
||||
)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalCriteria() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
CourseOffering section = ( CourseOffering ) session.createCriteria( CourseOffering.class ).uniqueResult();
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityLoadCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse().getCode().getDepartment() ) );
|
||||
assertTrue( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<CourseOffering> criteria = criteriaBuilder.createQuery( CourseOffering.class );
|
||||
criteria.from( CourseOffering.class );
|
||||
CourseOffering section = session.createQuery( criteria ).uniqueResult();
|
||||
// CourseOffering section = ( CourseOffering ) session.createCriteria( CourseOffering.class ).uniqueResult();
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityLoadCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
assertFalse( Hibernate.isInitialized( section.getCourse()
|
||||
.getCode()
|
||||
.getDepartment() ) );
|
||||
assertTrue( Hibernate.isInitialized( section.getCourse() ) );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicFetchProfileOperation() {
|
||||
assertTrue( "fetch profile not parsed properly", sessionFactory().containsFetchProfileDefinition( "enrollment.details" ) );
|
||||
assertTrue( "fetch profile not parsed properly", sessionFactory().containsFetchProfileDefinition( "offering.details" ) );
|
||||
assertTrue( "fetch profile not parsed properly", sessionFactory().containsFetchProfileDefinition( "course.details" ) );
|
||||
assertTrue(
|
||||
"fetch profile not parsed properly",
|
||||
sessionFactory().containsFetchProfileDefinition( "enrollment.details" )
|
||||
);
|
||||
assertTrue(
|
||||
"fetch profile not parsed properly",
|
||||
sessionFactory().containsFetchProfileDefinition( "offering.details" )
|
||||
);
|
||||
assertTrue(
|
||||
"fetch profile not parsed properly",
|
||||
sessionFactory().containsFetchProfileDefinition( "course.details" )
|
||||
);
|
||||
Session s = openSession();
|
||||
SessionImplementor si = ( SessionImplementor ) s;
|
||||
SessionImplementor si = (SessionImplementor) s;
|
||||
s.enableFetchProfile( "enrollment.details" );
|
||||
assertTrue( si.getLoadQueryInfluencers().hasEnabledFetchProfiles() );
|
||||
s.disableFetchProfile( "enrollment.details" );
|
||||
|
@ -167,7 +186,7 @@ public class JoinFetchProfileTest extends BaseCoreFunctionalTestCase {
|
|||
s.enableFetchProfile( "never-gonna-get-it" );
|
||||
fail( "expecting failure on undefined fetch-profile" );
|
||||
}
|
||||
catch ( UnknownProfileException expected ) {
|
||||
catch (UnknownProfileException expected) {
|
||||
}
|
||||
s.close();
|
||||
}
|
||||
|
@ -175,124 +194,140 @@ public class JoinFetchProfileTest extends BaseCoreFunctionalTestCase {
|
|||
@Test
|
||||
public void testLoadManyToOneFetchProfile() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
Enrollment enrollment = ( Enrollment ) session.get( Enrollment.class, data.getEnrollmentId() );
|
||||
assertEquals( 3, sessionFactory().getStatistics().getEntityLoadCount() ); // enrollment + (section + student)
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
Enrollment enrollment = session.get(
|
||||
Enrollment.class,
|
||||
data.getEnrollmentId()
|
||||
);
|
||||
assertEquals(
|
||||
3,
|
||||
sessionFactory().getStatistics().getEntityLoadCount()
|
||||
); // enrollment + (section + student)
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCriteriaManyToOneFetchProfile() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
Enrollment enrollment = ( Enrollment ) session.createCriteria( Enrollment.class ).uniqueResult();
|
||||
assertEquals( 3, sessionFactory().getStatistics().getEntityLoadCount() ); // enrollment + (section + student)
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Enrollment> criteria = criteriaBuilder.createQuery( Enrollment.class );
|
||||
criteria.from( Enrollment.class );
|
||||
Enrollment enrollment = session.createQuery( criteria ).uniqueResult();
|
||||
// Enrollment enrollment = ( Enrollment ) session.createCriteria( Enrollment.class ).uniqueResult();
|
||||
assertEquals(
|
||||
3,
|
||||
sessionFactory().getStatistics().getEntityLoadCount()
|
||||
); // enrollment + (section + student)
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getOffering() ) );
|
||||
assertTrue( Hibernate.isInitialized( enrollment.getStudent() ) );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadOneToManyFetchProfile() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
session.enableFetchProfile( "offering.details" );
|
||||
CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() );
|
||||
assertEquals( 3, sessionFactory().getStatistics().getEntityLoadCount() ); // section + (enrollments + course)
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.enableFetchProfile( "offering.details" );
|
||||
CourseOffering section = session.get(
|
||||
CourseOffering.class,
|
||||
data.getSectionId()
|
||||
);
|
||||
assertEquals(
|
||||
3,
|
||||
sessionFactory().getStatistics().getEntityLoadCount()
|
||||
); // section + (enrollments + course)
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
}
|
||||
)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadDeepFetchProfile() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
// enable both enrollment and offering detail profiles;
|
||||
// then loading the section/offering should fetch the enrollment
|
||||
// which in turn should fetch student (+ offering).
|
||||
session.enableFetchProfile( "offering.details" );
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
CourseOffering section = ( CourseOffering ) session.get( CourseOffering.class, data.getSectionId() );
|
||||
assertEquals( 4, sessionFactory().getStatistics().getEntityLoadCount() ); // section + (course + enrollments + (student))
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
// enable both enrollment and offering detail profiles;
|
||||
// then loading the section/offering should fetch the enrollment
|
||||
// which in turn should fetch student (+ offering).
|
||||
session.enableFetchProfile( "offering.details" );
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
CourseOffering section = session.get(
|
||||
CourseOffering.class,
|
||||
data.getSectionId()
|
||||
);
|
||||
assertEquals(
|
||||
4,
|
||||
sessionFactory().getStatistics().getEntityLoadCount()
|
||||
); // section + (course + enrollments + (student))
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( section.getEnrollments() ) );
|
||||
}
|
||||
)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadComponentDerefFetchProfile() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
session.enableFetchProfile( "course.details" );
|
||||
Course course = ( Course ) session.get( Course.class, data.getCourseId() );
|
||||
assertEquals( 2, sessionFactory().getStatistics().getEntityLoadCount() ); // course + department
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( course.getCode().getDepartment() ) );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.enableFetchProfile( "course.details" );
|
||||
Course course = session.get( Course.class, data.getCourseId() );
|
||||
assertEquals(
|
||||
2,
|
||||
sessionFactory().getStatistics().getEntityLoadCount()
|
||||
); // course + department
|
||||
assertEquals( 0, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
assertTrue( Hibernate.isInitialized( course.getCode().getDepartment() ) );
|
||||
}
|
||||
)
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHQL() {
|
||||
performWithStandardData(
|
||||
new TestCode() {
|
||||
public void perform(TestData data) {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
session.enableFetchProfile( "offering.details" );
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
List sections = session.createQuery( "from CourseOffering" ).list();
|
||||
int sectionCount = sections.size();
|
||||
assertEquals( "unexpected CourseOffering count", 1, sectionCount );
|
||||
assertEquals( 4, sessionFactory().getStatistics().getEntityLoadCount() );
|
||||
assertEquals( 2, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
data ->
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.enableFetchProfile( "offering.details" );
|
||||
session.enableFetchProfile( "enrollment.details" );
|
||||
List sections = session.createQuery( "from CourseOffering" ).list();
|
||||
int sectionCount = sections.size();
|
||||
assertEquals( "unexpected CourseOffering count", 1, sectionCount );
|
||||
assertEquals( 4, sessionFactory().getStatistics().getEntityLoadCount() );
|
||||
assertEquals( 2, sessionFactory().getStatistics().getEntityFetchCount() );
|
||||
}
|
||||
)
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -174,161 +174,5 @@ public class ImmutableNaturalIdTest extends AbstractJPATest {
|
|||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdCache() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
User u = new User( "steve", "superSecret" );
|
||||
s.persist( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCachePutCount() );//1: no stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
User v = new User( "gavin", "supsup" );
|
||||
s.persist( v );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//0: no stats since hbm.xml can't enable NaturalId caching
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//0: no stats since hbm.xml can't enable NaturalId caching
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( "delete User" ).executeUpdate();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdDeleteUsingCache() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
User u = new User( "steve", "superSecret" );
|
||||
s.persist( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCachePutCount() );//0: no stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//1: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
s.delete( u );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNull( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdRecreateUsingCache() {
|
||||
testNaturalIdDeleteUsingCache();
|
||||
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
User u = new User( "steve", "superSecret" );
|
||||
s.persist( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCachePutCount() );//1: no stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//1: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
s.delete( u );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -367,7 +367,7 @@ public class ProxyTest extends BaseCoreFunctionalTestCase {
|
|||
assertFalse( Hibernate.isInitialized( proxy ) );
|
||||
// load the rest back into the PC
|
||||
List all = s.createQuery( "from Container as c inner join fetch c.owner inner join fetch c.dataPoints where c.id <> :last" )
|
||||
.setLong( "last", lastContainerId.longValue() )
|
||||
.setParameter( "last", lastContainerId.longValue() )
|
||||
.list();
|
||||
Container container = ( Container ) all.get( 0 );
|
||||
s.delete( container );
|
||||
|
|
|
@ -0,0 +1,193 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.test.jpa.naturalid;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
|
||||
import org.hibernate.test.jpa.AbstractJPATest;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* @author Andrea Boriero
|
||||
*/
|
||||
public class ImmutableNaturalIdUsingCacheTest extends AbstractJPATest {
|
||||
public String[] getMappings() {
|
||||
return new String[] { "jpa/naturalid/User.hbm.xml" };
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(Configuration cfg) {
|
||||
super.configure( cfg );
|
||||
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
|
||||
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
|
||||
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdCache() {
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = new User( "steve", "superSecret" );
|
||||
s.persist( u );
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
}
|
||||
);
|
||||
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCachePutCount() );//1: no stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
User v = new User( "gavin", "supsup" );
|
||||
s.persist( v );
|
||||
}
|
||||
);
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
User u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//0: no stats since hbm.xml can't enable NaturalId caching
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//0: no stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
s -> s.createQuery( "delete User" ).executeUpdate()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdDeleteUsingCache() {
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
User u = new User( "steve", "superSecret" );
|
||||
s.persist( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCachePutCount() );//0: no stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//1: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
s.delete( u );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNull( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNaturalIdRecreateUsingCache() {
|
||||
testNaturalIdDeleteUsingCache();
|
||||
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
User u = new User( "steve", "superSecret" );
|
||||
s.persist( u );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCachePutCount() );//1: no stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
sessionFactory().getStatistics().clear();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
u = ( User ) s.createCriteria( User.class )
|
||||
.add( Restrictions.naturalId().set( "userName", "steve" ) )
|
||||
.setCacheable( true )
|
||||
.uniqueResult();
|
||||
assertNotNull( u );
|
||||
assertEquals( 1, sessionFactory().getStatistics().getNaturalIdQueryExecutionCount() );//0: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
assertEquals( 0, sessionFactory().getStatistics().getNaturalIdCacheHitCount() );//1: incorrect stats since hbm.xml can't enable NaturalId caching
|
||||
|
||||
s.delete( u );
|
||||
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue