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

View File

@ -812,21 +812,24 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona
assertUpdateCount( isContractVersioned ? 1 : 0 ); assertUpdateCount( isContractVersioned ? 1 : 0 );
clearCounts(); clearCounts();
try { inSession(
inTransaction(
s -> { s -> {
pOrig.removeContract( cOrig );
try {
s.merge( pOrig ); s.merge( pOrig );
assertFalse( isContractVersioned ); assertFalse( isContractVersioned );
} }
);
}
catch (PersistenceException ex) { catch (PersistenceException ex) {
assertTyping(StaleObjectStateException.class, ex.getCause()); assertTyping(StaleObjectStateException.class, ex.getCause());
assertTrue( isContractVersioned); assertTrue( isContractVersioned);
} }
finally {
s.getTransaction().rollback();
}
}
);
pOrig.removeContract( cOrig );
inTransaction( inTransaction(
s -> { s -> {
@ -867,18 +870,14 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona
assertUpdateCount( isContractVersioned ? 1 : 0 ); assertUpdateCount( isContractVersioned ? 1 : 0 );
clearCounts(); clearCounts();
try { inSession(
inTransaction(
s -> { s -> {
pOrig.removeContract( cOrig ); try {
s.update( pOrig ); s.getTransaction().commit();
assertFalse( isContractVersioned ); assertFalse( isContractVersioned );
}
);
} }
catch (PersistenceException ex) { catch (PersistenceException ex) {
s.getTransaction().rollback();
assertTrue( isContractVersioned ); assertTrue( isContractVersioned );
if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) { if ( !sessionFactory().getSessionFactoryOptions().isJdbcBatchVersionedData() ) {
assertTyping( StaleObjectStateException.class, ex.getCause() ); assertTyping( StaleObjectStateException.class, ex.getCause() );
@ -887,6 +886,8 @@ public abstract class AbstractEntityWithManyToManyTest extends BaseCoreFunctiona
assertTyping( StaleStateException.class, ex.getCause() ); assertTyping( StaleStateException.class, ex.getCause() );
} }
} }
}
);
inTransaction( inTransaction(
s -> { 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>. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/ */
package org.hibernate.test.joinfetch; package org.hibernate.test.joinfetch;
import java.util.List; 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.junit.Test;
import org.hibernate.FetchMode;
import org.hibernate.Hibernate; import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -40,23 +42,32 @@ public class JoinFetchTest extends BaseCoreFunctionalTestCase {
@Test @Test
public void testProjection() { public void testProjection() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
s.createCriteria(Item.class).setProjection( Projections.rowCount() ).uniqueResult(); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
s.createCriteria(Item.class).uniqueResult(); CriteriaQuery<Long> criteria = criteriaBuilder.createQuery( Long.class );
t.commit(); criteria.select( criteriaBuilder.count( criteria.from( Item.class ) ) );
s.close(); 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 @Test
public void testJoinFetch() { public void testJoinFetch() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
s.createQuery( "delete from Bid" ).executeUpdate(); s.createQuery( "delete from Bid" ).executeUpdate();
s.createQuery( "delete from Comment" ).executeUpdate(); s.createQuery( "delete from Comment" ).executeUpdate();
s.createQuery( "delete from Item" ).executeUpdate(); s.createQuery( "delete from Item" ).executeUpdate();
t.commit(); }
s.close(); );
Category cat = new Category( "Photography" ); Category cat = new Category( "Photography" );
Item i = new Item( cat, "Camera" ); Item i = new Item( cat, "Camera" );
@ -65,96 +76,109 @@ public class JoinFetchTest extends BaseCoreFunctionalTestCase {
new Comment( i, "This looks like a really good deal" ); new Comment( i, "This looks like a really good deal" );
new Comment( i, "Is it the latest version?" ); new Comment( i, "Is it the latest version?" );
new Comment( i, "<comment deleted>" ); new Comment( i, "<comment deleted>" );
System.out.println( b.getTimestamp() );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
s.persist( cat ); s.persist( cat );
s.persist( i ); s.persist( i );
t.commit(); }
s.close(); );
sessionFactory().getCache().evictEntityRegion( Item.class ); sessionFactory().getCache().evictEntityRegion( Item.class );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
i = s.get( Item.class, i.getId() ); Item i1 = s.get( Item.class, i.getId() );
assertTrue( Hibernate.isInitialized( i.getBids() ) ); assertTrue( Hibernate.isInitialized( i1.getBids() ) );
assertEquals( i.getBids().size(), 2 ); assertEquals( i1.getBids().size(), 2 );
assertTrue( Hibernate.isInitialized( i.getComments() ) ); assertTrue( Hibernate.isInitialized( i1.getComments() ) );
assertEquals( i.getComments().size(), 3 ); assertEquals( i1.getComments().size(), 3 );
t.commit(); }
s.close(); );
sessionFactory().getCache().evictEntityRegion( Bid.class ); sessionFactory().getCache().evictEntityRegion( Bid.class );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
b = s.get( Bid.class, b.getId() ); Bid b1 = s.get( Bid.class, b.getId() );
assertTrue( Hibernate.isInitialized( b.getItem() ) ); assertTrue( Hibernate.isInitialized( b1.getItem() ) );
assertTrue( Hibernate.isInitialized( b.getItem().getComments() ) ); assertTrue( Hibernate.isInitialized( b1.getItem().getComments() ) );
assertEquals( b.getItem().getComments().size(), 3 ); assertEquals( b1.getItem().getComments().size(), 3 );
System.out.println( b.getTimestamp() ); }
t.commit(); );
s.close();
sessionFactory().getCache().evictCollectionRegion( Item.class.getName() + ".bids" ); sessionFactory().getCache().evictCollectionRegion( Item.class.getName() + ".bids" );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
i = (Item) s.createCriteria( Item.class ) CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
.setFetchMode("bids", FetchMode.SELECT) CriteriaQuery<Item> criteria = criteriaBuilder.createQuery( Item.class );
.setFetchMode("comments", FetchMode.SELECT) 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(); .uniqueResult();
assertFalse( Hibernate.isInitialized( i.getBids() ) ); assertTrue( Hibernate.isInitialized( i1.getBids() ) );
assertFalse( Hibernate.isInitialized( i.getComments() ) ); assertTrue( Hibernate.isInitialized( i1.getComments() ) );
b = (Bid) i.getBids().iterator().next(); assertEquals( i1.getComments().size(), 3 );
assertTrue( Hibernate.isInitialized( b.getItem() ) ); assertEquals( i1.getBids().size(), 2 );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
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();
s = openSession();
t = s.beginTransaction();
Object[] row = (Object[]) s.getNamedQuery( Item.class.getName() + ".all" ).list().get( 0 ); Object[] row = (Object[]) s.getNamedQuery( Item.class.getName() + ".all" ).list().get( 0 );
i = (Item) row[0]; Item i1 = (Item) row[0];
assertTrue( Hibernate.isInitialized( i.getBids() ) ); assertTrue( Hibernate.isInitialized( i1.getBids() ) );
assertTrue( Hibernate.isInitialized( i.getComments() ) ); assertTrue( Hibernate.isInitialized( i1.getComments() ) );
assertEquals( i.getComments().size(), 3 ); assertEquals( i1.getComments().size(), 3 );
assertEquals( i.getBids().size(), 2 ); assertEquals( i1.getBids().size(), 2 );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
i = (Item) s.createCriteria(Item.class).uniqueResult(); // Item i1 = (Item) s.createCriteria( Item.class ).uniqueResult();
assertTrue( Hibernate.isInitialized( i.getBids() ) ); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
assertTrue( Hibernate.isInitialized( i.getComments() ) ); CriteriaQuery<Item> criteria = criteriaBuilder.createQuery( Item.class );
assertEquals( i.getComments().size(), 3 ); criteria.from( Item.class );
assertEquals( i.getBids().size(), 2 ); Item i1 = s.createQuery( criteria ).uniqueResult();
t.commit(); assertTrue( Hibernate.isInitialized( i1.getBids() ) );
s.close(); assertTrue( Hibernate.isInitialized( i1.getComments() ) );
assertEquals( i1.getComments().size(), 3 );
assertEquals( i1.getBids().size(), 2 );
}
);
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
List bids = s.createQuery("from Bid b left join fetch b.item i left join fetch i.category").list(); 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 ); Bid bid = (Bid) bids.get( 0 );
assertTrue( Hibernate.isInitialized( bid.getItem() ) ); assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
List pairs = s.createQuery( "from Item i left join i.bids b left join fetch i.category" ).list(); 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]; Item item = (Item) ( (Object[]) pairs.get( 0 ) )[0];
assertFalse( Hibernate.isInitialized( item.getBids() ) ); assertFalse( Hibernate.isInitialized( item.getBids() ) );
@ -166,7 +190,7 @@ public class JoinFetchTest extends BaseCoreFunctionalTestCase {
assertTrue( Hibernate.isInitialized( item.getCategory() ) ); assertTrue( Hibernate.isInitialized( item.getCategory() ) );
s.clear(); s.clear();
pairs = s.createQuery( "from Bid b left join b.item i left join fetch i.category" ).list(); pairs = s.createQuery( "from Bid b left join b.item i left join fetch i.category" ).list();
bid = (Bid) ( (Object[]) pairs.get(0) )[0]; Bid bid = (Bid) ( (Object[]) pairs.get( 0 ) )[0];
assertTrue( Hibernate.isInitialized( bid.getItem() ) ); assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
s.clear(); s.clear();
@ -174,24 +198,23 @@ public class JoinFetchTest extends BaseCoreFunctionalTestCase {
bid = (Bid) ( (Object[]) pairs.get( 0 ) )[0]; bid = (Bid) ( (Object[]) pairs.get( 0 ) )[0];
assertTrue( Hibernate.isInitialized( bid.getItem() ) ); assertTrue( Hibernate.isInitialized( bid.getItem() ) );
assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) ); assertTrue( Hibernate.isInitialized( bid.getItem().getCategory() ) );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
s.createQuery( "delete from Bid" ).executeUpdate(); s.createQuery( "delete from Bid" ).executeUpdate();
s.createQuery( "delete from Comment" ).executeUpdate(); s.createQuery( "delete from Comment" ).executeUpdate();
s.createQuery( "delete from Item" ).executeUpdate(); s.createQuery( "delete from Item" ).executeUpdate();
s.createQuery( "delete from Category" ).executeUpdate(); s.createQuery( "delete from Category" ).executeUpdate();
t.commit(); }
s.close(); );
} }
@Test @Test
public void testCollectionFilter() { public void testCollectionFilter() {
Session s = openSession(); inTransaction(
Transaction t = s.beginTransaction(); s -> {
Group hb = new Group( "hibernate" ); Group hb = new Group( "hibernate" );
User gavin = new User( "gavin" ); User gavin = new User( "gavin" );
User max = new User( "max" ); User max = new User( "max" );
@ -200,71 +223,82 @@ public class JoinFetchTest extends BaseCoreFunctionalTestCase {
gavin.getGroups().put( "hibernate", hb ); gavin.getGroups().put( "hibernate", hb );
max.getGroups().put( "hibernate", hb ); max.getGroups().put( "hibernate", hb );
s.persist( hb ); s.persist( hb );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
hb = (Group) s.createCriteria(Group.class) CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
.setFetchMode("users", FetchMode.SELECT) CriteriaQuery<Group> criteria = criteriaBuilder.createQuery( Group.class );
.add( Restrictions.idEq("hibernate") ) Root<Group> from = criteria.from( Group.class );
.uniqueResult(); 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() ) ); assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
//gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult(); //gavin = (User) s.createFilter( hb.getUsers(), "where index(this) = 'gavin'" ).uniqueResult();
Long size = (Long) s.createFilter( hb.getUsers(), "select count(*)" ).uniqueResult(); // Long size = (Long) s.createFilter( hb.getUsers(), "select count(*)" ).uniqueResult();
assertEquals( new Long(2), size ); // assertEquals( new Long( 2 ), size );
assertFalse( Hibernate.isInitialized( hb.getUsers() ) ); // assertFalse( Hibernate.isInitialized( hb.getUsers() ) );
s.delete( hb ); s.delete( hb );
t.commit(); }
s.close(); );
} }
@Test @Test
public void testJoinFetchManyToMany() { public void testJoinFetchManyToMany() {
Session s = openSession(); Group group = new Group( "hibernate" );
Transaction t = s.beginTransaction(); inTransaction(
Group hb = new Group("hibernate"); s -> {
User gavin = new User( "gavin" ); User gavin = new User( "gavin" );
User max = new User( "max" ); User max = new User( "max" );
hb.getUsers().put("gavin", gavin); group.getUsers().put( "gavin", gavin );
hb.getUsers().put("max", max); group.getUsers().put( "max", max );
gavin.getGroups().put("hibernate", hb); gavin.getGroups().put( "hibernate", group );
max.getGroups().put("hibernate", hb); max.getGroups().put( "hibernate", group );
s.persist(hb); s.persist( group );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
hb = s.get(Group.class, "hibernate"); Group hb = s.get( Group.class, "hibernate" );
assertTrue( Hibernate.isInitialized( hb.getUsers() ) ); assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
gavin = (User) hb.getUsers().get("gavin"); User gavin = (User) hb.getUsers().get( "gavin" );
assertFalse( Hibernate.isInitialized( gavin.getGroups() ) ); assertFalse( Hibernate.isInitialized( gavin.getGroups() ) );
max = s.get(User.class, "max"); User max = s.get( User.class, "max" );
assertFalse( Hibernate.isInitialized( max.getGroups() ) ); assertFalse( Hibernate.isInitialized( max.getGroups() ) );
t.commit(); }
s.close(); );
s = openSession(); inTransaction(
t = s.beginTransaction(); s -> {
hb = (Group) s.createCriteria(Group.class) CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
.setFetchMode("users", FetchMode.JOIN) CriteriaQuery<Group> criteria = criteriaBuilder.createQuery( Group.class );
.setFetchMode("users.groups", FetchMode.JOIN) Root<Group> from = criteria.from( Group.class );
.uniqueResult(); 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() ) ); assertTrue( Hibernate.isInitialized( hb.getUsers() ) );
gavin = (User) hb.getUsers().get("gavin"); User gavin = (User) hb.getUsers().get( "gavin" );
assertTrue( Hibernate.isInitialized( gavin.getGroups() ) ); assertTrue( Hibernate.isInitialized( gavin.getGroups() ) );
max = s.get(User.class, "max"); User max = s.get( User.class, "max" );
assertTrue( Hibernate.isInitialized( max.getGroups() ) ); assertTrue( Hibernate.isInitialized( max.getGroups() ) );
t.commit(); }
s.close(); );
s = openSession();
t = s.beginTransaction(); inTransaction(
s.delete(hb); s -> {
t.commit(); s.delete( group );
s.close(); }
);
} }
} }

View File

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