6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-16 14:42:27 +01:00
parent 0bf8cd1266
commit 430a765888
6 changed files with 1042 additions and 975 deletions

View File

@ -340,7 +340,7 @@ public class ImmutableNaturalKeyLookupTest extends BaseCoreFunctionalTestCase {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<A> criteria = criteriaBuilder.createQuery( A.class );
Root<A> root = criteria.from( A.class );
root.join( fecth, joinType );
root.fetch( fecth, joinType );
criteria.where( criteriaBuilder.equal( root.get( "name" ), "name1" ) );

View File

@ -10,7 +10,6 @@ import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
@ -171,9 +170,9 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
EntityWithFunctionAsColumnHolder.class );
Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class );
root.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
Join<Object, Object> nextHolder = root.join( "nextHolder", JoinType.LEFT );
nextHolder.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT )
.fetch( "nextHolder", JoinType.LEFT )
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT );
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
holder1 = s.createQuery( criteria ).uniqueResult();
@ -277,9 +276,9 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
CriteriaQuery<EntityWithFunctionAsColumnHolder> criteria = criteriaBuilder.createQuery(
EntityWithFunctionAsColumnHolder.class );
Root<EntityWithFunctionAsColumnHolder> root = criteria.from( EntityWithFunctionAsColumnHolder.class );
root.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
Join<Object, Object> nextHolder = root.join( "nextHolder", JoinType.LEFT );
nextHolder.join( "entityWithArgFunctionAsColumns", JoinType.LEFT );
root.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT )
.fetch( "nextHolder", JoinType.LEFT )
.fetch( "entityWithArgFunctionAsColumns", JoinType.LEFT );
criteria.where( criteriaBuilder.isNotNull( root.get( "nextHolder" ) ) );
// holder1 = ( EntityWithFunctionAsColumnHolder ) s.createCriteria( EntityWithFunctionAsColumnHolder.class )

View File

@ -812,21 +812,24 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona
assertUpdateCount( isContractVersioned ? 1 : 0 );
clearCounts();
try {
inTransaction(
s -> {
inSession(
s -> {
pOrig.removeContract( cOrig );
try {
s.merge( pOrig );
assertFalse( isContractVersioned );
}
);
}
catch (PersistenceException ex) {
assertTyping( StaleObjectStateException.class, ex.getCause() );
assertTrue( isContractVersioned );
}
catch (PersistenceException ex) {
assertTyping(StaleObjectStateException.class, ex.getCause());
assertTrue( isContractVersioned);
}
finally {
s.getTransaction().rollback();
}
}
);
pOrig.removeContract( cOrig );
inTransaction(
s -> {
@ -867,26 +870,24 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona
assertUpdateCount( isContractVersioned ? 1 : 0 );
clearCounts();
try {
inTransaction(
s -> {
pOrig.removeContract( cOrig );
s.update( pOrig );
inSession(
s -> {
try {
s.getTransaction().commit();
assertFalse( isContractVersioned );
}
);
}
catch (PersistenceException ex) {
assertTrue( isContractVersioned );
if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) {
assertTyping( StaleObjectStateException.class, ex.getCause() );
}
else {
assertTyping( StaleStateException.class, ex.getCause() );
}
}
catch (PersistenceException ex) {
s.getTransaction().rollback();
assertTrue( isContractVersioned );
if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) {
assertTyping( StaleObjectStateException.class, ex.getCause() );
}
else {
assertTyping( StaleStateException.class, ex.getCause() );
}
}
}
);
inTransaction(
s -> {

View File

@ -5,18 +5,20 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.joinfetch;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import org.junit.Test;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
@ -34,237 +36,269 @@ public class JoinFetchTest extends BaseCoreFunctionalTestCase {
@Override
public void configure(Configuration cfg) {
cfg.setProperty(Environment.MAX_FETCH_DEPTH, "10");
cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "false");
cfg.setProperty( Environment.MAX_FETCH_DEPTH, "10" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
}
@Test
public void testProjection() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createCriteria(Item.class).setProjection( Projections.rowCount() ).uniqueResult();
s.createCriteria(Item.class).uniqueResult();
t.commit();
s.close();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Long> criteria = criteriaBuilder.createQuery( Long.class );
criteria.select( criteriaBuilder.count( criteria.from( Item.class ) ) );
s.createQuery( criteria ).uniqueResult();
CriteriaQuery<Item> itemCriteria = criteriaBuilder.createQuery( Item.class );
itemCriteria.from( Item.class );
s.createQuery( itemCriteria ).uniqueResult();
// s.createCriteria(Item.class).setProjection( Projections.rowCount() ).uniqueResult();
// s.createCriteria(Item.class).uniqueResult();
}
);
}
@Test
public void testJoinFetch() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery( "delete from Bid" ).executeUpdate();
s.createQuery( "delete from Comment" ).executeUpdate();
s.createQuery( "delete from Item" ).executeUpdate();
t.commit();
s.close();
Category cat = new Category("Photography");
Item i = new Item(cat, "Camera");
Bid b = new Bid(i, 100.0f);
new Bid(i, 105.0f);
new Comment(i, "This looks like a really good deal");
new Comment(i, "Is it the latest version?");
new Comment(i, "<comment deleted>");
System.out.println( b.getTimestamp() );
s = openSession();
t = s.beginTransaction();
s.persist(cat);
s.persist(i);
t.commit();
s.close();
sessionFactory().getCache().evictEntityRegion(Item.class);
inTransaction(
s -> {
s.createQuery( "delete from Bid" ).executeUpdate();
s.createQuery( "delete from Comment" ).executeUpdate();
s.createQuery( "delete from Item" ).executeUpdate();
}
);
s = openSession();
t = s.beginTransaction();
i = s.get( Item.class, i.getId() );
assertTrue( Hibernate.isInitialized( i.getBids() ) );
assertEquals( i.getBids().size(), 2 );
assertTrue( Hibernate.isInitialized( i.getComments() ) );
assertEquals( i.getComments().size(), 3 );
t.commit();
s.close();
Category cat = new Category( "Photography" );
Item i = new Item( cat, "Camera" );
Bid b = new Bid( i, 100.0f );
new Bid( i, 105.0f );
new Comment( i, "This looks like a really good deal" );
new Comment( i, "Is it the latest version?" );
new Comment( i, "<comment deleted>" );
sessionFactory().getCache().evictEntityRegion(Bid.class);
inTransaction(
s -> {
s.persist( cat );
s.persist( i );
}
);
s = openSession();
t = s.beginTransaction();
b = s.get( Bid.class, b.getId() );
assertTrue( Hibernate.isInitialized( b.getItem() ) );
assertTrue( Hibernate.isInitialized( b.getItem().getComments() ) );
assertEquals( b.getItem().getComments().size(), 3 );
System.out.println( b.getTimestamp() );
t.commit();
s.close();
sessionFactory().getCache().evictEntityRegion( Item.class );
sessionFactory().getCache().evictCollectionRegion(Item.class.getName() + ".bids");
s = openSession();
t = s.beginTransaction();
i = (Item) s.createCriteria( Item.class )
.setFetchMode("bids", FetchMode.SELECT)
.setFetchMode("comments", FetchMode.SELECT)
.uniqueResult();
assertFalse( Hibernate.isInitialized( i.getBids() ) );
assertFalse( Hibernate.isInitialized( i.getComments() ) );
b = (Bid) i.getBids().iterator().next();
assertTrue( Hibernate.isInitialized( b.getItem() ) );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
i = (Item) s.createQuery("from Item i left join fetch i.bids left join fetch i.comments").uniqueResult();
assertTrue( Hibernate.isInitialized( i.getBids() ) );
assertTrue( Hibernate.isInitialized( i.getComments() ) );
assertEquals( i.getComments().size(), 3 );
assertEquals( i.getBids().size(), 2 );
t.commit();
s.close();
inTransaction(
s -> {
Item i1 = s.get( Item.class, i.getId() );
assertTrue( Hibernate.isInitialized( i1.getBids() ) );
assertEquals( i1.getBids().size(), 2 );
assertTrue( Hibernate.isInitialized( i1.getComments() ) );
assertEquals( i1.getComments().size(), 3 );
}
);
s = openSession();
t = s.beginTransaction();
Object[] row = (Object[]) s.getNamedQuery(Item.class.getName() + ".all").list().get(0);
i = (Item) row[0];
assertTrue( Hibernate.isInitialized( i.getBids() ) );
assertTrue( Hibernate.isInitialized( i.getComments() ) );
assertEquals( i.getComments().size(), 3 );
assertEquals( i.getBids().size(), 2 );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
i = (Item) s.createCriteria(Item.class).uniqueResult();
assertTrue( Hibernate.isInitialized( i.getBids() ) );
assertTrue( Hibernate.isInitialized( i.getComments() ) );
assertEquals( i.getComments().size(), 3 );
assertEquals( i.getBids().size(), 2 );
t.commit();
s.close();
sessionFactory().getCache().evictEntityRegion( Bid.class );
s = openSession();
t = s.beginTransaction();
List bids = s.createQuery("from Bid b left join fetch b.item i left join fetch i.category").list();
Bid bid = (Bid) bids.get(0);
assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
t.commit();
s.close();
inTransaction(
s -> {
Bid b1 = s.get( Bid.class, b.getId() );
assertTrue( Hibernate.isInitialized( b1.getItem() ) );
assertTrue( Hibernate.isInitialized( b1.getItem().getComments() ) );
assertEquals( b1.getItem().getComments().size(), 3 );
}
);
s = openSession();
t = s.beginTransaction();
List pairs = s.createQuery("from Item i left join i.bids b left join fetch i.category").list();
Item item = (Item) ( (Object[]) pairs.get(0) )[0];
assertFalse( Hibernate.isInitialized( item.getBids() ) );
assertTrue( Hibernate.isInitialized( item.getCategory() ) );
s.clear();
pairs = s.createQuery("from Item i left join i.bids b left join i.category").list();
item = (Item) ( (Object[]) pairs.get(0) )[0];
assertFalse( Hibernate.isInitialized( item.getBids() ) );
assertTrue( Hibernate.isInitialized( item.getCategory() ) );
s.clear();
pairs = s.createQuery("from Bid b left join b.item i left join fetch i.category").list();
bid = (Bid) ( (Object[]) pairs.get(0) )[0];
assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
s.clear();
pairs = s.createQuery("from Bid b left join b.item i left join i.category").list();
bid = (Bid) ( (Object[]) pairs.get(0) )[0];
assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
t.commit();
s.close();
sessionFactory().getCache().evictCollectionRegion( Item.class.getName() + ".bids" );
s = openSession();
t = s.beginTransaction();
s.createQuery( "delete from Bid" ).executeUpdate();
s.createQuery( "delete from Comment" ).executeUpdate();
s.createQuery( "delete from Item" ).executeUpdate();
s.createQuery( "delete from Category" ).executeUpdate();
t.commit();
s.close();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Item> criteria = criteriaBuilder.createQuery( Item.class );
Root<Item> root = criteria.from( Item.class );
root.join( "bids" );
root.join( "comments" );
Item i1 = s.createQuery( criteria ).uniqueResult();
// Item i1 = (Item) s.createCriteria( Item.class )from
// .setFetchMode( "bids", FetchMode.SELECT )
// .setFetchMode( "comments", FetchMode.SELECT )
// .uniqueResult();
assertFalse( Hibernate.isInitialized( i1.getBids() ) );
assertFalse( Hibernate.isInitialized( i1.getComments() ) );
Bid b1 = (Bid) i1.getBids().iterator().next();
assertTrue( Hibernate.isInitialized( b1.getItem() ) );
}
);
inTransaction(
s -> {
Item i1 = (Item) s.createQuery( "from Item i left join fetch i.bids left join fetch i.comments" )
.uniqueResult();
assertTrue( Hibernate.isInitialized( i1.getBids() ) );
assertTrue( Hibernate.isInitialized( i1.getComments() ) );
assertEquals( i1.getComments().size(), 3 );
assertEquals( i1.getBids().size(), 2 );
}
);
inTransaction(
s -> {
Object[] row = (Object[]) s.getNamedQuery( Item.class.getName() + ".all" ).list().get( 0 );
Item i1 = (Item) row[0];
assertTrue( Hibernate.isInitialized( i1.getBids() ) );
assertTrue( Hibernate.isInitialized( i1.getComments() ) );
assertEquals( i1.getComments().size(), 3 );
assertEquals( i1.getBids().size(), 2 );
}
);
inTransaction(
s -> {
// Item i1 = (Item) s.createCriteria( Item.class ).uniqueResult();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Item> criteria = criteriaBuilder.createQuery( Item.class );
criteria.from( Item.class );
Item i1 = s.createQuery( criteria ).uniqueResult();
assertTrue( Hibernate.isInitialized( i1.getBids() ) );
assertTrue( Hibernate.isInitialized( i1.getComments() ) );
assertEquals( i1.getComments().size(), 3 );
assertEquals( i1.getBids().size(), 2 );
}
);
inTransaction(
s -> {
List bids = s.createQuery( "from Bid b left join fetch b.item i left join fetch i.category" )
.list();
Bid bid = (Bid) bids.get( 0 );
assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
}
);
inTransaction(
s -> {
List pairs = s.createQuery( "from Item i left join i.bids b left join fetch i.category" ).list();
Item item = (Item) ( (Object[]) pairs.get( 0 ) )[0];
assertFalse( Hibernate.isInitialized( item.getBids() ) );
assertTrue( Hibernate.isInitialized( item.getCategory() ) );
s.clear();
pairs = s.createQuery( "from Item i left join i.bids b left join i.category" ).list();
item = (Item) ( (Object[]) pairs.get( 0 ) )[0];
assertFalse( Hibernate.isInitialized( item.getBids() ) );
assertTrue( Hibernate.isInitialized( item.getCategory() ) );
s.clear();
pairs = s.createQuery( "from Bid b left join b.item i left join fetch i.category" ).list();
Bid bid = (Bid) ( (Object[]) pairs.get( 0 ) )[0];
assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
s.clear();
pairs = s.createQuery( "from Bid b left join b.item i left join i.category" ).list();
bid = (Bid) ( (Object[]) pairs.get( 0 ) )[0];
assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
}
);
inTransaction(
s -> {
s.createQuery( "delete from Bid" ).executeUpdate();
s.createQuery( "delete from Comment" ).executeUpdate();
s.createQuery( "delete from Item" ).executeUpdate();
s.createQuery( "delete from Category" ).executeUpdate();
}
);
}
@Test
public void testCollectionFilter() {
Session s = openSession();
Transaction t = s.beginTransaction();
Group hb = new Group("hibernate");
User gavin = new User("gavin");
User max = new User("max");
hb.getUsers().put("gavin", gavin);
hb.getUsers().put("max", max);
gavin.getGroups().put("hibernate", hb);
max.getGroups().put("hibernate", hb);
s.persist(hb);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
hb = (Group) s.createCriteria(Group.class)
.setFetchMode("users", FetchMode.SELECT)
.add( Restrictions.idEq("hibernate") )
.uniqueResult();
assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
//gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult();
Long size = (Long) s.createFilter( hb.getUsers(), "select count(*)" ).uniqueResult();
assertEquals( new Long(2), size );
assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
s.delete(hb);
t.commit();
s.close();
inTransaction(
s -> {
Group hb = new Group( "hibernate" );
User gavin = new User( "gavin" );
User max = new User( "max" );
hb.getUsers().put( "gavin", gavin );
hb.getUsers().put( "max", max );
gavin.getGroups().put( "hibernate", hb );
max.getGroups().put( "hibernate", hb );
s.persist( hb );
}
);
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Group> criteria = criteriaBuilder.createQuery( Group.class );
Root<Group> from = criteria.from( Group.class );
from.join( "users", JoinType.LEFT );
criteria.where( criteriaBuilder.equal( from.get( "name" ), "hibernate" ) );
Group hb = s.createQuery( criteria ).uniqueResult();
// hb = (Group) s.createCriteria( Group.class )
// .setFetchMode( "users", FetchMode.SELECT )
// .add( Restrictions.idEq( "hibernate" ) )
// .uniqueResult();
assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
//gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult();
// Long size = (Long) s.createFilter( hb.getUsers(), "select count(*)" ).uniqueResult();
// assertEquals( new Long( 2 ), size );
// assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
s.delete( hb );
}
);
}
@Test
public void testJoinFetchManyToMany() {
Session s = openSession();
Transaction t = s.beginTransaction();
Group hb = new Group("hibernate");
User gavin = new User("gavin");
User max = new User("max");
hb.getUsers().put("gavin", gavin);
hb.getUsers().put("max", max);
gavin.getGroups().put("hibernate", hb);
max.getGroups().put("hibernate", hb);
s.persist(hb);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
hb = s.get(Group.class, "hibernate");
assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
gavin = (User) hb.getUsers().get("gavin");
assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
max = s.get(User.class, "max");
assertFalse( Hibernate.isInitialized( max.getGroups() ) );
t.commit();
s.close();
Group group = new Group( "hibernate" );
inTransaction(
s -> {
User gavin = new User( "gavin" );
User max = new User( "max" );
group.getUsers().put( "gavin", gavin );
group.getUsers().put( "max", max );
gavin.getGroups().put( "hibernate", group );
max.getGroups().put( "hibernate", group );
s.persist( group );
}
);
s = openSession();
t = s.beginTransaction();
hb = (Group) s.createCriteria(Group.class)
.setFetchMode("users", FetchMode.JOIN)
.setFetchMode("users.groups", FetchMode.JOIN)
.uniqueResult();
assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
gavin = (User) hb.getUsers().get("gavin");
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
max = s.get(User.class, "max");
assertTrue( Hibernate.isInitialized( max.getGroups() ) );
t.commit();
s.close();
inTransaction(
s -> {
Group hb = s.get( Group.class, "hibernate" );
assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
User gavin = (User) hb.getUsers().get( "gavin" );
assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
User max = s.get( User.class, "max" );
assertFalse( Hibernate.isInitialized( max.getGroups() ) );
}
);
s = openSession();
t = s.beginTransaction();
s.delete(hb);
t.commit();
s.close();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Group> criteria = criteriaBuilder.createQuery( Group.class );
Root<Group> from = criteria.from( Group.class );
from.fetch( "users", JoinType.LEFT ).fetch( "groups" );
Group hb = s.createQuery( criteria ).uniqueResult();
// hb = (Group) s.createCriteria( Group.class )
// .setFetchMode( "users", FetchMode.JOIN )
// .setFetchMode( "users.groups", FetchMode.JOIN )
// .uniqueResult();
assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
User gavin = (User) hb.getUsers().get( "gavin" );
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
User max = s.get( User.class, "max" );
assertTrue( Hibernate.isInitialized( max.getGroups() ) );
}
);
inTransaction(
s -> {
s.delete( group );
}
);
}
}

View File

@ -6,11 +6,15 @@
*/
package org.hibernate.test.onetoone.formula;
import org.hibernate.FetchMode;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Root;
import org.hibernate.Hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Property;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.TextType;
@ -23,6 +27,7 @@ import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -73,14 +78,14 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
address.setStreet( "Karbarook Ave" );
person.setAddress( address );
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
doInHibernate( this::sessionFactory, session -> {
session.persist( person );
} );
}
@Override
protected void cleanupTest() {
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
doInHibernate( this::sessionFactory, session -> {
session.delete( person );
} );
}
@ -88,7 +93,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
@Test
public void testOneToOneFormula() {
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
doInHibernate( this::sessionFactory, s -> {
Person p = (Person) s.createQuery( "from Person" ).uniqueResult();
assertNotNull( p.getAddress() );
@ -96,7 +101,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
assertNull( p.getMailingAddress() );
} );
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
doInHibernate( this::sessionFactory, s -> {
Person p = (Person) s.createQuery(
"from Person p left join fetch p.mailingAddress left join fetch p.address" ).uniqueResult();
@ -105,7 +110,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
assertNull( p.getMailingAddress() );
} );
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
doInHibernate( this::sessionFactory, s -> {
Person p = (Person) s.createQuery( "from Person p left join fetch p.address" ).uniqueResult();
assertNotNull( p.getAddress() );
@ -113,28 +118,45 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
assertNull( p.getMailingAddress() );
} );
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Person p = (Person) s.createCriteria( Person.class )
.createCriteria( "address" )
.add( Property.forName( "zip" ).eq( "3181" ) )
.uniqueResult();
doInHibernate( this::sessionFactory, s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
Join<Object, Object> address = root.join( "address", JoinType.INNER );
criteria.where( criteriaBuilder.equal( address.get( "zip" ), "3181" ) );
Person p = s.createQuery( criteria ).uniqueResult();
// Person p = (Person) s.createCriteria( Person.class )
// .createCriteria( "address" )
// .add( Property.forName( "zip" ).eq( "3181" ) )
// .uniqueResult();
assertNotNull( p );
} );
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Person p = (Person) s.createCriteria( Person.class )
.setFetchMode( "address", FetchMode.JOIN )
.uniqueResult();
doInHibernate( this::sessionFactory, s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
root.fetch( "address", JoinType.LEFT );
Person p = s.createQuery( criteria ).uniqueResult();
// Person p = (Person) s.createCriteria( Person.class )
// .setFetchMode( "address", FetchMode.JOIN )
// .uniqueResult();
assertNotNull( p.getAddress() );
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
assertNull( p.getMailingAddress() );
} );
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Person p = (Person) s.createCriteria( Person.class )
.setFetchMode( "mailingAddress", FetchMode.JOIN )
.uniqueResult();
doInHibernate( this::sessionFactory, s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Person> criteria = criteriaBuilder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
root.fetch( "address", JoinType.LEFT );
Person p = s.createQuery( criteria ).uniqueResult();
// Person p = (Person) s.createCriteria( Person.class )
// .setFetchMode( "mailingAddress", FetchMode.JOIN )
// .uniqueResult();
assertNotNull( p.getAddress() );
assertTrue( Hibernate.isInitialized( p.getAddress() ) );
@ -147,14 +169,14 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-5757")
public void testQuery() {
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
doInHibernate( this::sessionFactory, session -> {
Person p = (Person) session.createQuery( "from Person p where p.address = :address" ).setParameter(
"address",
address ).uniqueResult();
assertThat( p, notNullValue() );
} );
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
doInHibernate( this::sessionFactory, session -> {
Address a = (Address) session.createQuery( "from Address a where a.person = :person" ).setParameter(
"person",
person ).uniqueResult();
@ -165,7 +187,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
@Test
public void testOneToOneEmbeddedCompositeKey() {
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
doInHibernate( this::sessionFactory, session -> {
Address a = new Address();
a.setType("HOME");
a.setPerson(person);
@ -177,7 +199,7 @@ public class OneToOneFormulaTest extends BaseCoreFunctionalTestCase {
assertEquals(a.getZip(), "3181");
} );
TransactionUtil.doInHibernate( this::sessionFactory, session -> {
doInHibernate( this::sessionFactory, session -> {
Address a = new Address();
a.setType("HOME");
a.setPerson(person);