6 - SQM based on JPA type system
This commit is contained in:
parent
3dbcf28b41
commit
d99a3f1938
|
@ -40,6 +40,7 @@ import org.junit.Test;
|
|||
import org.jboss.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
@ -321,149 +322,154 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
|
|||
s.close();
|
||||
}
|
||||
|
||||
public void testSQLFunctions() throws Exception {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Simple simple = new Simple( Long.valueOf(10) );
|
||||
simple.setName("Simple 1");
|
||||
s.save(simple );
|
||||
public void testSQLFunctions() {
|
||||
try(Session s = openSession()) {
|
||||
Transaction t = s.beginTransaction();
|
||||
Simple simple = new Simple( Long.valueOf( 10 ) );
|
||||
simple.setName( "Simple 1" );
|
||||
s.save( simple );
|
||||
|
||||
s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'" ).list();
|
||||
s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'" ).list();
|
||||
s.createQuery( "from Simple s where repeat( lower(s.name), (3 + (1-1)) / 2) = 'foofoofoo'" ).list();
|
||||
s.createQuery( "from Simple s where repeat('foo', 3) = 'foofoofoo'" ).list();
|
||||
s.createQuery( "from Simple s where repeat(s.name, 3) = 'foofoofoo'" ).list();
|
||||
s.createQuery( "from Simple s where repeat( lower(s.name), (3 + (1-1)) / 2) = 'foofoofoo'" ).list();
|
||||
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where upper( s.name ) ='SIMPLE 1'" ).list().size()==1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"from Simple s where not( upper( s.name ) ='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar' )"
|
||||
).list()
|
||||
.size()==1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where upper( s.name ) ='SIMPLE 1'" ).list().size() == 1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"from Simple s where not( upper( s.name ) ='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar' )"
|
||||
).list()
|
||||
.size() == 1
|
||||
);
|
||||
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where lower( s.name || ' foo' ) ='simple 1 foo'" ).list().size()==1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where lower( concat(s.name, ' foo') ) ='simple 1 foo'" ).list().size()==1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where lower( s.name || ' foo' ) ='simple 1 foo'" ).list().size() == 1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where lower( concat(s.name, ' foo') ) ='simple 1 foo'" )
|
||||
.list()
|
||||
.size() == 1
|
||||
);
|
||||
|
||||
Simple other = new Simple( Long.valueOf(20) );
|
||||
other.setName( "Simple 2" );
|
||||
other.setCount( 12 );
|
||||
simple.setOther( other );
|
||||
s.save( other );
|
||||
//s.find("from Simple s where s.name ## 'cat|rat|bag'");
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where upper( s.other.name ) ='SIMPLE 2'" ).list().size()==1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where not ( upper( s.other.name ) ='SIMPLE 2' )" ).list().size()==0
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"select distinct s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2"
|
||||
).list()
|
||||
.size()==1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"select s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2 order by s.other.count"
|
||||
).list()
|
||||
.size()==1
|
||||
);
|
||||
Simple min = new Simple( Long.valueOf(30) );
|
||||
min.setCount( -1 );
|
||||
s.save(min );
|
||||
Simple other = new Simple( Long.valueOf( 20 ) );
|
||||
other.setName( "Simple 2" );
|
||||
other.setCount( 12 );
|
||||
simple.setOther( other );
|
||||
s.save( other );
|
||||
//s.find("from Simple s where s.name ## 'cat|rat|bag'");
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where upper( s.other.name ) ='SIMPLE 2'" ).list().size() == 1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where not ( upper( s.other.name ) ='SIMPLE 2' )" ).list().size() == 0
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"select distinct s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2"
|
||||
).list()
|
||||
.size() == 1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"select s from Simple s where ( ( s.other.count + 3 ) = (15*2)/2 and s.count = 69) or ( ( s.other.count + 2 ) / 7 ) = 2 order by s.other.count"
|
||||
).list()
|
||||
.size() == 1
|
||||
);
|
||||
Simple min = new Simple( Long.valueOf( 30 ) );
|
||||
min.setCount( -1 );
|
||||
s.save( min );
|
||||
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where s.count > ( select min(sim.count) from Simple sim )" )
|
||||
.list()
|
||||
.size()==2
|
||||
);
|
||||
t.commit();
|
||||
t = s.beginTransaction();
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"from Simple s where s = some( select sim from Simple sim where sim.count>=0 ) and s.count >= 0"
|
||||
).list()
|
||||
.size()==2
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0"
|
||||
).list()
|
||||
.size()==1
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery( "from Simple s where s.count > ( select min(sim.count) from Simple sim )" )
|
||||
.list()
|
||||
.size() == 2
|
||||
);
|
||||
t.commit();
|
||||
t = s.beginTransaction();
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"from Simple s where s = some( select sim from Simple sim where sim.count>=0 ) and s.count >= 0"
|
||||
).list()
|
||||
.size() == 2
|
||||
);
|
||||
assertTrue(
|
||||
s.createQuery(
|
||||
"from Simple s where s = some( select sim from Simple sim where sim.other.count=s.other.count ) and s.other.count > 0"
|
||||
).list()
|
||||
.size() == 1
|
||||
);
|
||||
|
||||
Iterator iter = s.createQuery( "select sum(s.count) from Simple s group by s.count having sum(s.count) > 10" )
|
||||
.iterate();
|
||||
assertTrue( iter.hasNext() );
|
||||
assertEquals( Long.valueOf( 12 ), iter.next() );
|
||||
assertTrue( !iter.hasNext() );
|
||||
iter = s.createQuery( "select s.count from Simple s group by s.count having s.count = 12" ).iterate();
|
||||
assertTrue( iter.hasNext() );
|
||||
List list = s.createQuery( "select sum(s.count) from Simple s group by s.count having sum(s.count) > 10" )
|
||||
.list();
|
||||
assertEquals( 1, list.size() );
|
||||
assertEquals( Long.valueOf( 12 ), list.get( 0 ) );
|
||||
list = s.createQuery( "select s.count from Simple s group by s.count having s.count = 12" ).list();
|
||||
assertFalse( list.isEmpty() );
|
||||
|
||||
s.createQuery(
|
||||
"select s.id, s.count, count(t), max(t.date) from Simple s, Simple t where s.count = t.count group by s.id, s.count order by s.count"
|
||||
).iterate();
|
||||
s.createQuery(
|
||||
"select s.id, s.count, count(t), max(t.date) from Simple s, Simple t where s.count = t.count group by s.id, s.count order by s.count"
|
||||
).list();
|
||||
|
||||
Query q = s.createQuery("from Simple s");
|
||||
q.setMaxResults( 10 );
|
||||
assertTrue( q.list().size()==3 );
|
||||
q = s.createQuery("from Simple s");
|
||||
q.setMaxResults( 1 );
|
||||
assertTrue( q.list().size()==1 );
|
||||
q = s.createQuery("from Simple s");
|
||||
assertTrue( q.list().size() == 3 );
|
||||
q = s.createQuery("from Simple s where s.name = ?");
|
||||
q.setParameter( 0, "Simple 1" );
|
||||
assertTrue( q.list().size()==1 );
|
||||
q = s.createQuery("from Simple s where s.name = ? and upper(s.name) = ?");
|
||||
q.setParameter(1, "SIMPLE 1");
|
||||
q.setParameter( 0, "Simple 1" );
|
||||
q.setFirstResult(0);
|
||||
assertTrue( q.iterate().hasNext() );
|
||||
q = s.createQuery("from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1");
|
||||
q.setParameter( "bar", "SIMPLE 1" );
|
||||
q.setParameter( "foo", "Simple 1" );
|
||||
q.setParameter("count", 69);
|
||||
q.setFirstResult(0);
|
||||
assertTrue( q.iterate().hasNext() );
|
||||
q = s.createQuery("select s.id from Simple s");
|
||||
q.setFirstResult(1);
|
||||
q.setMaxResults( 2 );
|
||||
iter = q.iterate();
|
||||
int i=0;
|
||||
while ( iter.hasNext() ) {
|
||||
assertTrue( iter.next() instanceof Long );
|
||||
i++;
|
||||
Query q = s.createQuery( "from Simple s" );
|
||||
q.setMaxResults( 10 );
|
||||
assertTrue( q.list().size() == 3 );
|
||||
q = s.createQuery( "from Simple s" );
|
||||
q.setMaxResults( 1 );
|
||||
assertTrue( q.list().size() == 1 );
|
||||
q = s.createQuery( "from Simple s" );
|
||||
assertTrue( q.list().size() == 3 );
|
||||
q = s.createQuery( "from Simple s where s.name = ?" );
|
||||
q.setParameter( 0, "Simple 1" );
|
||||
assertTrue( q.list().size() == 1 );
|
||||
q = s.createQuery( "from Simple s where s.name = ? and upper(s.name) = ?" );
|
||||
q.setParameter( 1, "SIMPLE 1" );
|
||||
q.setParameter( 0, "Simple 1" );
|
||||
q.setFirstResult( 0 );
|
||||
assertFalse( q.list().isEmpty() );
|
||||
q = s.createQuery(
|
||||
"from Simple s where s.name = :foo and upper(s.name) = :bar or s.count=:count or s.count=:count + 1" );
|
||||
q.setParameter( "bar", "SIMPLE 1" );
|
||||
q.setParameter( "foo", "Simple 1" );
|
||||
q.setParameter( "count", 69 );
|
||||
q.setFirstResult( 0 );
|
||||
assertFalse( q.list().isEmpty() );
|
||||
q = s.createQuery( "select s.id from Simple s" );
|
||||
q.setFirstResult( 1 );
|
||||
q.setMaxResults( 2 );
|
||||
list = q.list();
|
||||
for ( Object l : list ) {
|
||||
assertTrue( l instanceof Long );
|
||||
}
|
||||
// int i=0;
|
||||
// while ( list.hasNext() ) {
|
||||
// assertTrue( list.next() instanceof Long );
|
||||
// i++;
|
||||
// }
|
||||
assertEquals( 2, list.size() );
|
||||
q = s.createQuery( "select all s, s.other from Simple s where s = :s" );
|
||||
q.setParameter( "s", simple );
|
||||
assertTrue( q.list().size() == 1 );
|
||||
|
||||
|
||||
q = s.createQuery( "from Simple s where s.name in (:name_list) and s.count > :count" );
|
||||
HashSet set = new HashSet();
|
||||
set.add( "Simple 1" );
|
||||
set.add( "foo" );
|
||||
q.setParameterList( "name_list", set );
|
||||
q.setParameter( "count", new Integer( -1 ) );
|
||||
assertTrue( q.list().size() == 1 );
|
||||
|
||||
ScrollableResults sr = s.createQuery( "from Simple s" ).scroll();
|
||||
sr.next();
|
||||
sr.get();
|
||||
sr.close();
|
||||
|
||||
s.delete( other );
|
||||
s.delete( simple );
|
||||
s.delete( min );
|
||||
t.commit();
|
||||
}
|
||||
assertTrue( i == 2 );
|
||||
q = s.createQuery("select all s, s.other from Simple s where s = :s");
|
||||
q.setParameter("s", simple);
|
||||
assertTrue( q.list().size()==1 );
|
||||
|
||||
|
||||
q = s.createQuery("from Simple s where s.name in (:name_list) and s.count > :count");
|
||||
HashSet set = new HashSet();
|
||||
set.add("Simple 1");
|
||||
set.add("foo");
|
||||
q.setParameterList( "name_list", set );
|
||||
q.setParameter("count", new Integer(-1) );
|
||||
assertTrue( q.list().size()==1 );
|
||||
|
||||
ScrollableResults sr = s.createQuery("from Simple s").scroll();
|
||||
sr.next();
|
||||
sr.get();
|
||||
sr.close();
|
||||
|
||||
s.delete( other );
|
||||
s.delete( simple );
|
||||
s.delete( min );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -127,6 +127,7 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
if(s.getTransaction().isActive()){
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw exception;
|
||||
}finally {
|
||||
s.close();
|
||||
}
|
||||
|
@ -139,10 +140,11 @@ public class DiscriminatorTest extends BaseCoreFunctionalTestCase {
|
|||
e = s.get( Employee.class, new Long( employee.getId() ) );
|
||||
c = s.get( Customer.class, new Long( employee.getId() ) );
|
||||
s.getTransaction().commit();
|
||||
}catch (Exception exc){
|
||||
}catch (Exception exception){
|
||||
if(s.getTransaction().isActive()){
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw exception;
|
||||
}finally{
|
||||
s.close();
|
||||
}
|
||||
|
|
|
@ -131,6 +131,7 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
|||
if ( s.getTransaction().isActive() ) {
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
finally {
|
||||
s.close();
|
||||
|
@ -151,6 +152,7 @@ public class SimpleInheritanceTest extends BaseCoreFunctionalTestCase {
|
|||
if ( s.getTransaction().isActive() ) {
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
finally {
|
||||
s.close();
|
||||
|
|
|
@ -247,6 +247,7 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
|
|||
if (s.getTransaction().isActive()){
|
||||
s.getTransaction().rollback();
|
||||
}
|
||||
throw e;
|
||||
}finally {
|
||||
if(s.isOpen()) {
|
||||
s.close();
|
||||
|
|
|
@ -6,6 +6,9 @@
|
|||
*/
|
||||
package org.hibernate.test.keymanytoone.bidir.component;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.boot.Metadata;
|
||||
|
@ -78,54 +81,56 @@ public class EagerKeyManyToOneTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
// test cascading a save to an association with a key-many-to-one which refers to a
|
||||
// just saved entity
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Customer cust = new Customer( "Acme, Inc." );
|
||||
Order order = new Order( new Order.Id( cust, 1 ) );
|
||||
cust.getOrders().add( order );
|
||||
s.save( cust );
|
||||
s.flush();
|
||||
assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() );
|
||||
s.delete( cust );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Customer cust = new Customer( "Acme, Inc." );
|
||||
Order order = new Order( new Order.Id( cust, 1 ) );
|
||||
cust.getOrders().add( order );
|
||||
s.save( cust );
|
||||
s.flush();
|
||||
assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() );
|
||||
s.delete( cust );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLoadingStrategies() {
|
||||
sessionFactory().getStatistics().clear();
|
||||
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
Customer cust = new Customer( "Acme, Inc." );
|
||||
Order order = new Order( new Order.Id( cust, 1 ) );
|
||||
cust.getOrders().add( order );
|
||||
s.save( cust );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Customer cust = new Customer( "Acme, Inc." );
|
||||
Order order = new Order( new Order.Id( cust, 1 ) );
|
||||
cust.getOrders().add( order );
|
||||
s.save( cust );
|
||||
}
|
||||
);
|
||||
|
||||
s = openSession();
|
||||
s.beginTransaction();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Customer cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
|
||||
cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders" ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
|
||||
cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders" ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders as o join fetch o.id.customer" ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
|
||||
cust = ( Customer ) s.createQuery( "from Customer c join fetch c.orders as o join fetch o.id.customer" ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Customer> criteria = criteriaBuilder.createQuery( Customer.class );
|
||||
criteria.from( Customer.class );
|
||||
cust = s.createQuery( criteria ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
|
||||
cust = ( Customer ) s.createCriteria( Customer.class ).uniqueResult();
|
||||
assertEquals( 1, cust.getOrders().size() );
|
||||
s.clear();
|
||||
|
||||
s.delete( cust );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
s.delete( cust );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -8,24 +8,23 @@ package org.hibernate.test.unionsubclass;
|
|||
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
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.query.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.dialect.SQLServerDialect;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.jdbc.SQLServerSnapshotIsolationConnectionProvider;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -47,7 +46,7 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
@Override
|
||||
public void configure(Configuration cfg) {
|
||||
super.configure( cfg );
|
||||
if( SQLServerDialect.class.isAssignableFrom( DIALECT.getClass() )) {
|
||||
if ( SQLServerDialect.class.isAssignableFrom( DIALECT.getClass() ) ) {
|
||||
cfg.getProperties().put( AvailableSettings.CONNECTION_PROVIDER, connectionProvider );
|
||||
}
|
||||
}
|
||||
|
@ -58,7 +57,6 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
connectionProvider.stop();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String[] getMappings() {
|
||||
return new String[] { "unionsubclass/Beings.hbm.xml" };
|
||||
|
@ -66,375 +64,409 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testUnionSubclassCollection() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Location mel = new Location("Earth");
|
||||
s.save(mel);
|
||||
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity("gavin");
|
||||
gavin.setSex('M');
|
||||
gavin.setLocation(mel);
|
||||
mel.addBeing(gavin);
|
||||
|
||||
gavin.getInfo().put("foo", "bar");
|
||||
gavin.getInfo().put("x", "y");
|
||||
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Location mel = new Location( "Earth" );
|
||||
s.save( mel );
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
gavin = (Human) s.createCriteria(Human.class).uniqueResult();
|
||||
assertEquals( gavin.getInfo().size(), 2 );
|
||||
s.delete(gavin);
|
||||
s.delete( gavin.getLocation() );
|
||||
t.commit();
|
||||
s.close();
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity( "gavin" );
|
||||
gavin.setSex( 'M' );
|
||||
gavin.setLocation( mel );
|
||||
mel.addBeing( gavin );
|
||||
|
||||
gavin.getInfo().put( "foo", "bar" );
|
||||
gavin.getInfo().put( "x", "y" );
|
||||
}
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Human> criteria = criteriaBuilder.createQuery( Human.class );
|
||||
criteria.from( Human.class );
|
||||
Human gavin = s.createQuery( criteria ).uniqueResult();
|
||||
assertEquals( gavin.getInfo().size(), 2 );
|
||||
s.delete( gavin );
|
||||
s.delete( gavin.getLocation() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnionSubclassFetchMode() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Location mel = new Location("Earth");
|
||||
s.save(mel);
|
||||
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity("gavin");
|
||||
gavin.setSex('M');
|
||||
gavin.setLocation(mel);
|
||||
mel.addBeing(gavin);
|
||||
Human max = new Human();
|
||||
max.setIdentity( "max" );
|
||||
max.setSex( 'M' );
|
||||
max.setLocation(mel);
|
||||
mel.addBeing(gavin);
|
||||
|
||||
s.flush();
|
||||
s.clear();
|
||||
|
||||
List list = s.createCriteria(Human.class)
|
||||
.setFetchMode("location", FetchMode.JOIN)
|
||||
.setFetchMode("location.beings", FetchMode.JOIN)
|
||||
.list();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Location mel = new Location( "Earth" );
|
||||
s.save( mel );
|
||||
|
||||
for ( Object aList : list ) {
|
||||
Human h = (Human) aList;
|
||||
assertTrue( Hibernate.isInitialized( h.getLocation() ) );
|
||||
assertTrue( Hibernate.isInitialized( h.getLocation().getBeings() ) );
|
||||
s.delete( h );
|
||||
}
|
||||
s.delete( s.get( Location.class, mel.getId() ) );
|
||||
t.commit();
|
||||
s.close();
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity( "gavin" );
|
||||
gavin.setSex( 'M' );
|
||||
gavin.setLocation( mel );
|
||||
mel.addBeing( gavin );
|
||||
Human max = new Human();
|
||||
max.setIdentity( "max" );
|
||||
max.setSex( 'M' );
|
||||
max.setLocation( mel );
|
||||
mel.addBeing( gavin );
|
||||
|
||||
s.flush();
|
||||
s.clear();
|
||||
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Human> criteria = criteriaBuilder.createQuery( Human.class );
|
||||
criteria.from( Human.class ).fetch( "location", JoinType.LEFT ).fetch( "beings", JoinType.LEFT );
|
||||
|
||||
List<Human> list = s.createQuery( criteria ).list();
|
||||
// List list = s.createCriteria( Human.class )
|
||||
// .setFetchMode( "location", FetchMode.JOIN )
|
||||
// .setFetchMode( "location.beings", FetchMode.JOIN )
|
||||
// .list();
|
||||
|
||||
for ( Human h : list ) {
|
||||
assertTrue( Hibernate.isInitialized( h.getLocation() ) );
|
||||
assertTrue( Hibernate.isInitialized( h.getLocation().getBeings() ) );
|
||||
s.delete( h );
|
||||
}
|
||||
s.delete( s.get( Location.class, mel.getId() ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUnionSubclassOneToMany() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Location mel = new Location("Melbourne, Australia");
|
||||
Location mars = new Location("Mars");
|
||||
s.save(mel);
|
||||
s.save(mars);
|
||||
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity("gavin");
|
||||
gavin.setSex('M');
|
||||
gavin.setLocation(mel);
|
||||
mel.addBeing(gavin);
|
||||
|
||||
Alien x23y4 = new Alien();
|
||||
x23y4.setIdentity("x23y4$$hu%3");
|
||||
x23y4.setLocation(mars);
|
||||
x23y4.setSpecies("martian");
|
||||
mars.addBeing(x23y4);
|
||||
|
||||
Alien yy3dk = new Alien();
|
||||
yy3dk.setIdentity("yy3dk&*!!!");
|
||||
yy3dk.setLocation(mars);
|
||||
yy3dk.setSpecies("martian");
|
||||
mars.addBeing(yy3dk);
|
||||
|
||||
Hive hive = new Hive();
|
||||
hive.setLocation(mars);
|
||||
hive.getMembers().add(x23y4);
|
||||
x23y4.setHive(hive);
|
||||
hive.getMembers().add(yy3dk);
|
||||
yy3dk.setHive(hive);
|
||||
s.persist(hive);
|
||||
|
||||
yy3dk.getHivemates().add(x23y4);
|
||||
x23y4.getHivemates().add(yy3dk);
|
||||
|
||||
s.flush();
|
||||
s.clear();
|
||||
|
||||
hive = (Hive) s.createQuery("from Hive h").uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( hive.getMembers() ) );
|
||||
assertEquals( hive.getMembers().size(), 2 );
|
||||
|
||||
s.clear();
|
||||
|
||||
hive = (Hive) s.createQuery("from Hive h left join fetch h.location left join fetch h.members").uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( hive.getMembers() ) );
|
||||
assertEquals( hive.getMembers().size(), 2 );
|
||||
|
||||
s.clear();
|
||||
|
||||
x23y4 = (Alien) s.createQuery("from Alien a left join fetch a.hivemates where a.identity like 'x%'").uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( x23y4.getHivemates() ) );
|
||||
assertEquals( x23y4.getHivemates().size(), 1 );
|
||||
|
||||
s.clear();
|
||||
|
||||
x23y4 = (Alien) s.createQuery("from Alien a where a.identity like 'x%'").uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( x23y4.getHivemates() ) );
|
||||
assertEquals( x23y4.getHivemates().size(), 1 );
|
||||
|
||||
s.clear();
|
||||
|
||||
x23y4 = (Alien) s.createCriteria(Alien.class).addOrder( Order.asc("identity") ).list().get(0);
|
||||
s.delete( x23y4.getHive() );
|
||||
s.delete( s.get(Location.class, mel.getId() ) );
|
||||
s.delete( s.get(Location.class, mars.getId() ) );
|
||||
assertTrue( s.createQuery("from Being").list().isEmpty() );
|
||||
t.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Location mel = new Location( "Melbourne, Australia" );
|
||||
Location mars = new Location( "Mars" );
|
||||
s.save( mel );
|
||||
s.save( mars );
|
||||
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity( "gavin" );
|
||||
gavin.setSex( 'M' );
|
||||
gavin.setLocation( mel );
|
||||
mel.addBeing( gavin );
|
||||
|
||||
Alien x23y4 = new Alien();
|
||||
x23y4.setIdentity( "x23y4$$hu%3" );
|
||||
x23y4.setLocation( mars );
|
||||
x23y4.setSpecies( "martian" );
|
||||
mars.addBeing( x23y4 );
|
||||
|
||||
Alien yy3dk = new Alien();
|
||||
yy3dk.setIdentity( "yy3dk&*!!!" );
|
||||
yy3dk.setLocation( mars );
|
||||
yy3dk.setSpecies( "martian" );
|
||||
mars.addBeing( yy3dk );
|
||||
|
||||
Hive hive = new Hive();
|
||||
hive.setLocation( mars );
|
||||
hive.getMembers().add( x23y4 );
|
||||
x23y4.setHive( hive );
|
||||
hive.getMembers().add( yy3dk );
|
||||
yy3dk.setHive( hive );
|
||||
s.persist( hive );
|
||||
|
||||
yy3dk.getHivemates().add( x23y4 );
|
||||
x23y4.getHivemates().add( yy3dk );
|
||||
|
||||
s.flush();
|
||||
s.clear();
|
||||
|
||||
hive = (Hive) s.createQuery( "from Hive h" ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( hive.getMembers() ) );
|
||||
assertEquals( hive.getMembers().size(), 2 );
|
||||
|
||||
s.clear();
|
||||
|
||||
hive = (Hive) s.createQuery( "from Hive h left join fetch h.location left join fetch h.members" )
|
||||
.uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( hive.getMembers() ) );
|
||||
assertEquals( hive.getMembers().size(), 2 );
|
||||
|
||||
s.clear();
|
||||
|
||||
x23y4 = (Alien) s.createQuery( "from Alien a left join fetch a.hivemates where a.identity like 'x%'" )
|
||||
.uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( x23y4.getHivemates() ) );
|
||||
assertEquals( x23y4.getHivemates().size(), 1 );
|
||||
|
||||
s.clear();
|
||||
|
||||
x23y4 = (Alien) s.createQuery( "from Alien a where a.identity like 'x%'" ).uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( x23y4.getHivemates() ) );
|
||||
assertEquals( x23y4.getHivemates().size(), 1 );
|
||||
|
||||
s.clear();
|
||||
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Alien> criteria = criteriaBuilder.createQuery( Alien.class );
|
||||
Root<Alien> root = criteria.from( Alien.class );
|
||||
criteria.orderBy( criteriaBuilder.asc( root.get("identity") ) );
|
||||
x23y4 = s.createQuery( criteria ).list().get( 0 );
|
||||
// x23y4 = (Alien) s.createCriteria( Alien.class ).addOrder( Order.asc( "identity" ) ).list().get( 0 );
|
||||
s.delete( x23y4.getHive() );
|
||||
s.delete( s.get( Location.class, mel.getId() ) );
|
||||
s.delete( s.get( Location.class, mars.getId() ) );
|
||||
assertTrue( s.createQuery( "from Being" ).list().isEmpty() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnionSubclassManyToOne() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Location mel = new Location("Melbourne, Australia");
|
||||
Location mars = new Location("Mars");
|
||||
s.save(mel);
|
||||
s.save(mars);
|
||||
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity("gavin");
|
||||
gavin.setSex('M');
|
||||
gavin.setLocation(mel);
|
||||
mel.addBeing(gavin);
|
||||
|
||||
Alien x23y4 = new Alien();
|
||||
x23y4.setIdentity("x23y4$$hu%3");
|
||||
x23y4.setLocation(mars);
|
||||
x23y4.setSpecies("martian");
|
||||
mars.addBeing(x23y4);
|
||||
|
||||
Hive hive = new Hive();
|
||||
hive.setLocation(mars);
|
||||
hive.getMembers().add(x23y4);
|
||||
x23y4.setHive(hive);
|
||||
s.persist(hive);
|
||||
|
||||
Thing thing = new Thing();
|
||||
thing.setDescription("some thing");
|
||||
thing.setOwner(gavin);
|
||||
gavin.getThings().add(thing);
|
||||
s.save(thing);
|
||||
s.flush();
|
||||
|
||||
s.clear();
|
||||
|
||||
thing = (Thing) s.createQuery("from Thing t left join fetch t.owner").uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "gavin" );
|
||||
s.clear();
|
||||
|
||||
thing = (Thing) s.createQuery("select t from Thing t left join t.owner where t.owner.identity='gavin'").uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "gavin" );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery("from Human h left join fetch h.things").uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
assertTrue( s.createQuery("from Being b left join fetch b.things").list().size()==2 );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery("from Being b join fetch b.things").uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery("select h from Human h join h.things t where t.description='some thing'").uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery("select b from Being b join b.things t where t.description='some thing'").uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get(0) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
thing = (Thing) s.get( Thing.class, thing.getId() );
|
||||
assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "gavin" );
|
||||
|
||||
thing.getOwner().getThings().remove(thing);
|
||||
thing.setOwner(x23y4);
|
||||
x23y4.getThings().add(thing);
|
||||
|
||||
s.flush();
|
||||
|
||||
s.clear();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Location mel = new Location( "Melbourne, Australia" );
|
||||
Location mars = new Location( "Mars" );
|
||||
s.save( mel );
|
||||
s.save( mars );
|
||||
|
||||
thing = (Thing) s.get( Thing.class, thing.getId() );
|
||||
assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "x23y4$$hu%3" );
|
||||
|
||||
s.delete(thing);
|
||||
x23y4 = (Alien) s.createCriteria(Alien.class).uniqueResult();
|
||||
s.delete( x23y4.getHive() );
|
||||
s.delete( s.get(Location.class, mel.getId() ) );
|
||||
s.delete( s.get(Location.class, mars.getId() ) );
|
||||
assertTrue( s.createQuery("from Being").list().isEmpty() );
|
||||
t.commit();
|
||||
s.close();
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity( "gavin" );
|
||||
gavin.setSex( 'M' );
|
||||
gavin.setLocation( mel );
|
||||
mel.addBeing( gavin );
|
||||
|
||||
Alien x23y4 = new Alien();
|
||||
x23y4.setIdentity( "x23y4$$hu%3" );
|
||||
x23y4.setLocation( mars );
|
||||
x23y4.setSpecies( "martian" );
|
||||
mars.addBeing( x23y4 );
|
||||
|
||||
Hive hive = new Hive();
|
||||
hive.setLocation( mars );
|
||||
hive.getMembers().add( x23y4 );
|
||||
x23y4.setHive( hive );
|
||||
s.persist( hive );
|
||||
|
||||
Thing thing = new Thing();
|
||||
thing.setDescription( "some thing" );
|
||||
thing.setOwner( gavin );
|
||||
gavin.getThings().add( thing );
|
||||
s.save( thing );
|
||||
s.flush();
|
||||
|
||||
s.clear();
|
||||
|
||||
thing = (Thing) s.createQuery( "from Thing t left join fetch t.owner" ).uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "gavin" );
|
||||
s.clear();
|
||||
|
||||
thing = (Thing) s.createQuery( "select t from Thing t left join t.owner where t.owner.identity='gavin'" )
|
||||
.uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "gavin" );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery( "from Human h left join fetch h.things" ).uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get( 0 ) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
assertTrue( s.createQuery( "from Being b left join fetch b.things" ).list().size() == 2 );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery( "from Being b join fetch b.things" ).uniqueResult();
|
||||
assertTrue( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get( 0 ) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery( "select h from Human h join h.things t where t.description='some thing'" )
|
||||
.uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get( 0 ) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
gavin = (Human) s.createQuery( "select b from Being b join b.things t where t.description='some thing'" )
|
||||
.uniqueResult();
|
||||
assertFalse( Hibernate.isInitialized( gavin.getThings() ) );
|
||||
assertEquals( ( (Thing) gavin.getThings().get( 0 ) ).getDescription(), "some thing" );
|
||||
s.clear();
|
||||
|
||||
thing = s.get( Thing.class, thing.getId() );
|
||||
assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "gavin" );
|
||||
|
||||
thing.getOwner().getThings().remove( thing );
|
||||
thing.setOwner( x23y4 );
|
||||
x23y4.getThings().add( thing );
|
||||
|
||||
s.flush();
|
||||
|
||||
s.clear();
|
||||
|
||||
thing = s.get( Thing.class, thing.getId() );
|
||||
assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
|
||||
assertEquals( thing.getOwner().getIdentity(), "x23y4$$hu%3" );
|
||||
|
||||
s.delete( thing );
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Alien> criteria = criteriaBuilder.createQuery( Alien.class );
|
||||
criteria.from( Alien.class );
|
||||
x23y4 = s.createQuery( criteria ).uniqueResult();
|
||||
s.delete( x23y4.getHive() );
|
||||
s.delete( s.get( Location.class, mel.getId() ) );
|
||||
s.delete( s.get( Location.class, mars.getId() ) );
|
||||
assertTrue( s.createQuery( "from Being" ).list().isEmpty() );
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnionSubclass() {
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
Location mel = new Location("Melbourne, Australia");
|
||||
Location atl = new Location("Atlanta, GA");
|
||||
Location mars = new Location("Mars");
|
||||
s.save(mel);
|
||||
s.save(atl);
|
||||
s.save(mars);
|
||||
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity("gavin");
|
||||
gavin.setSex('M');
|
||||
gavin.setLocation(mel);
|
||||
mel.addBeing(gavin);
|
||||
|
||||
Alien x23y4 = new Alien();
|
||||
x23y4.setIdentity("x23y4$$hu%3");
|
||||
x23y4.setLocation(mars);
|
||||
x23y4.setSpecies("martian");
|
||||
mars.addBeing(x23y4);
|
||||
|
||||
Hive hive = new Hive();
|
||||
hive.setLocation(mars);
|
||||
hive.getMembers().add(x23y4);
|
||||
x23y4.setHive(hive);
|
||||
s.persist(hive);
|
||||
|
||||
assertEquals( s.createQuery("from Being").list().size(), 2 );
|
||||
assertEquals( s.createQuery("from Being b where b.class = Alien").list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Being b where type(b) = :what").setParameter("what", Alien.class).list().size(), 1 );
|
||||
assertEquals( s.createQuery("from Being b where type(b) in :what").setParameterList("what", new Class[] { Alien.class, Human.class }).list().size(), 2 );
|
||||
assertEquals( s.createQuery("from Alien").list().size(), 1 );
|
||||
s.clear();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Location mel = new Location( "Melbourne, Australia" );
|
||||
Location atl = new Location( "Atlanta, GA" );
|
||||
Location mars = new Location( "Mars" );
|
||||
s.save( mel );
|
||||
s.save( atl );
|
||||
s.save( mars );
|
||||
|
||||
List beings = s.createQuery("from Being b left join fetch b.location").list();
|
||||
for ( Object being : beings ) {
|
||||
Being b = (Being) being;
|
||||
assertTrue( Hibernate.isInitialized( b.getLocation() ) );
|
||||
assertNotNull( b.getLocation().getName() );
|
||||
assertNotNull( b.getIdentity() );
|
||||
assertNotNull( b.getSpecies() );
|
||||
}
|
||||
assertEquals( beings.size(), 2 );
|
||||
s.clear();
|
||||
|
||||
beings = s.createQuery("from Being").list();
|
||||
for ( Object being : beings ) {
|
||||
Being b = (Being) being;
|
||||
assertFalse( Hibernate.isInitialized( b.getLocation() ) );
|
||||
assertNotNull( b.getLocation().getName() );
|
||||
assertNotNull( b.getIdentity() );
|
||||
assertNotNull( b.getSpecies() );
|
||||
}
|
||||
assertEquals( beings.size(), 2 );
|
||||
s.clear();
|
||||
|
||||
List locations = s.createQuery("from Location").list();
|
||||
int count = 0;
|
||||
for ( Object location : locations ) {
|
||||
Location l = (Location) location;
|
||||
assertNotNull( l.getName() );
|
||||
for ( Object o : l.getBeings() ) {
|
||||
count++;
|
||||
assertSame( ( (Being) o ).getLocation(), l );
|
||||
}
|
||||
}
|
||||
assertEquals(count, 2);
|
||||
assertEquals( locations.size(), 3 );
|
||||
s.clear();
|
||||
Human gavin = new Human();
|
||||
gavin.setIdentity( "gavin" );
|
||||
gavin.setSex( 'M' );
|
||||
gavin.setLocation( mel );
|
||||
mel.addBeing( gavin );
|
||||
|
||||
locations = s.createQuery("from Location loc left join fetch loc.beings").list();
|
||||
count = 0;
|
||||
for ( Object location : locations ) {
|
||||
Location l = (Location) location;
|
||||
assertNotNull( l.getName() );
|
||||
for ( Object o : l.getBeings() ) {
|
||||
count++;
|
||||
assertSame( ( (Being) o ).getLocation(), l );
|
||||
}
|
||||
}
|
||||
assertEquals(count, 2);
|
||||
assertEquals( locations.size(), 3 );
|
||||
s.clear();
|
||||
Alien x23y4 = new Alien();
|
||||
x23y4.setIdentity( "x23y4$$hu%3" );
|
||||
x23y4.setLocation( mars );
|
||||
x23y4.setSpecies( "martian" );
|
||||
mars.addBeing( x23y4 );
|
||||
|
||||
gavin = (Human) s.get( Human.class, gavin.getId() );
|
||||
atl = (Location) s.get( Location.class, atl.getId() );
|
||||
|
||||
atl.addBeing(gavin);
|
||||
assertEquals( s.createQuery("from Human h where h.location.name like '%GA'").list().size(), 1 );
|
||||
s.delete(gavin);
|
||||
x23y4 = (Alien) s.createCriteria(Alien.class).uniqueResult();
|
||||
s.delete( x23y4.getHive() );
|
||||
assertTrue( s.createQuery("from Being").list().isEmpty() );
|
||||
|
||||
s.createQuery("delete from Location").executeUpdate();
|
||||
t.commit();
|
||||
s.close();
|
||||
Hive hive = new Hive();
|
||||
hive.setLocation( mars );
|
||||
hive.getMembers().add( x23y4 );
|
||||
x23y4.setHive( hive );
|
||||
s.persist( hive );
|
||||
|
||||
assertEquals( s.createQuery( "from Being" ).list().size(), 2 );
|
||||
assertEquals( s.createQuery( "from Being b where b.class = Alien" ).list().size(), 1 );
|
||||
assertEquals( s.createQuery( "from Being b where type(b) = :what" ).setParameter(
|
||||
"what",
|
||||
Alien.class
|
||||
).list().size(), 1 );
|
||||
assertEquals( s.createQuery( "from Being b where type(b) in :what" ).setParameterList(
|
||||
"what",
|
||||
new Class[] {
|
||||
Alien.class,
|
||||
Human.class
|
||||
}
|
||||
).list().size(), 2 );
|
||||
assertEquals( s.createQuery( "from Alien" ).list().size(), 1 );
|
||||
s.clear();
|
||||
|
||||
List beings = s.createQuery( "from Being b left join fetch b.location" ).list();
|
||||
for ( Object being : beings ) {
|
||||
Being b = (Being) being;
|
||||
assertTrue( Hibernate.isInitialized( b.getLocation() ) );
|
||||
assertNotNull( b.getLocation().getName() );
|
||||
assertNotNull( b.getIdentity() );
|
||||
assertNotNull( b.getSpecies() );
|
||||
}
|
||||
assertEquals( beings.size(), 2 );
|
||||
s.clear();
|
||||
|
||||
beings = s.createQuery( "from Being" ).list();
|
||||
for ( Object being : beings ) {
|
||||
Being b = (Being) being;
|
||||
assertFalse( Hibernate.isInitialized( b.getLocation() ) );
|
||||
assertNotNull( b.getLocation().getName() );
|
||||
assertNotNull( b.getIdentity() );
|
||||
assertNotNull( b.getSpecies() );
|
||||
}
|
||||
assertEquals( beings.size(), 2 );
|
||||
s.clear();
|
||||
|
||||
List locations = s.createQuery( "from Location" ).list();
|
||||
int count = 0;
|
||||
for ( Object location : locations ) {
|
||||
Location l = (Location) location;
|
||||
assertNotNull( l.getName() );
|
||||
for ( Object o : l.getBeings() ) {
|
||||
count++;
|
||||
assertSame( ( (Being) o ).getLocation(), l );
|
||||
}
|
||||
}
|
||||
assertEquals( count, 2 );
|
||||
assertEquals( locations.size(), 3 );
|
||||
s.clear();
|
||||
|
||||
locations = s.createQuery( "from Location loc left join fetch loc.beings" ).list();
|
||||
count = 0;
|
||||
for ( Object location : locations ) {
|
||||
Location l = (Location) location;
|
||||
assertNotNull( l.getName() );
|
||||
for ( Object o : l.getBeings() ) {
|
||||
count++;
|
||||
assertSame( ( (Being) o ).getLocation(), l );
|
||||
}
|
||||
}
|
||||
assertEquals( count, 2 );
|
||||
assertEquals( locations.size(), 3 );
|
||||
s.clear();
|
||||
|
||||
gavin = s.get( Human.class, gavin.getId() );
|
||||
atl = s.get( Location.class, atl.getId() );
|
||||
|
||||
atl.addBeing( gavin );
|
||||
assertEquals( s.createQuery( "from Human h where h.location.name like '%GA'" ).list().size(), 1 );
|
||||
s.delete( gavin );
|
||||
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Alien> criteria = criteriaBuilder.createQuery( Alien.class );
|
||||
criteria.from( Alien.class );
|
||||
|
||||
x23y4 = s.createQuery( criteria ).uniqueResult();
|
||||
s.delete( x23y4.getHive() );
|
||||
assertTrue( s.createQuery( "from Being" ).list().isEmpty() );
|
||||
|
||||
s.createQuery( "delete from Location" ).executeUpdate();
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedUnionedSubclasses() throws Exception {
|
||||
Session s;
|
||||
Transaction tx;
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
Location mel = new Location("Earth");
|
||||
Human marcf = new Human();
|
||||
marcf.setIdentity("marc");
|
||||
marcf.setSex('M');
|
||||
mel.addBeing(marcf);
|
||||
Employee steve = new Employee();
|
||||
steve.setIdentity("steve");
|
||||
steve.setSex('M');
|
||||
steve.setSalary( (double) 0 );
|
||||
mel.addBeing(steve);
|
||||
s.persist(mel);
|
||||
tx.commit();
|
||||
s.close();
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
Query q = s.createQuery( "from Being h where h.identity = :name1 or h.identity = :name2" );
|
||||
q.setParameter("name1", "marc");
|
||||
q.setParameter("name2", "steve");
|
||||
final List result = q.list();
|
||||
assertEquals( 2, result.size() );
|
||||
s.delete( result.get(0) );
|
||||
s.delete( result.get(1) );
|
||||
s.delete( ( (Human) result.get(0) ).getLocation() );
|
||||
tx.commit();
|
||||
s.close();
|
||||
inTransaction(
|
||||
s -> {
|
||||
Location mel = new Location( "Earth" );
|
||||
Human marcf = new Human();
|
||||
marcf.setIdentity( "marc" );
|
||||
marcf.setSex( 'M' );
|
||||
mel.addBeing( marcf );
|
||||
Employee steve = new Employee();
|
||||
steve.setIdentity( "steve" );
|
||||
steve.setSex( 'M' );
|
||||
steve.setSalary( (double) 0 );
|
||||
mel.addBeing( steve );
|
||||
s.persist( mel );
|
||||
}
|
||||
);
|
||||
|
||||
inTransaction(
|
||||
s -> {
|
||||
Query q = s.createQuery( "from Being h where h.identity = :name1 or h.identity = :name2" );
|
||||
q.setParameter( "name1", "marc" );
|
||||
q.setParameter( "name2", "steve" );
|
||||
final List result = q.list();
|
||||
assertEquals( 2, result.size() );
|
||||
s.delete( result.get( 0 ) );
|
||||
s.delete( result.get( 1 ) );
|
||||
s.delete( ( (Human) result.get( 0 ) ).getLocation() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-11740" )
|
||||
@TestForIssue(jiraKey = "HHH-11740")
|
||||
public void testBulkOperationsWithDifferentConnections() throws Exception {
|
||||
doInHibernate(
|
||||
this::sessionFactory, s -> {
|
||||
|
@ -489,29 +521,29 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
// for a bulk operation.
|
||||
|
||||
doInHibernate( this::sessionFactory, s2 -> {
|
||||
// Check same assertions for s2 as was done for s1.
|
||||
SharedSessionContractImplementor s2Implementor = (SharedSessionContractImplementor) s2;
|
||||
assertTrue( s2Implementor.getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected() );
|
||||
assertEquals(
|
||||
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION,
|
||||
s2Implementor.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode()
|
||||
);
|
||||
// Check same assertions for s2 as was done for s1.
|
||||
SharedSessionContractImplementor s2Implementor = (SharedSessionContractImplementor) s2;
|
||||
assertTrue( s2Implementor.getJdbcCoordinator().getLogicalConnection().isPhysicallyConnected() );
|
||||
assertEquals(
|
||||
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION,
|
||||
s2Implementor.getJdbcCoordinator().getLogicalConnection().getConnectionHandlingMode()
|
||||
);
|
||||
|
||||
// Get the Connection s2 will use.
|
||||
Connection connection2 = s2Implementor.connection();
|
||||
// Get the Connection s2 will use.
|
||||
Connection connection2 = s2Implementor.connection();
|
||||
|
||||
// Assert that connection2 is not the same as connection1
|
||||
assertNotSame( connection1, connection2 );
|
||||
// Assert that connection2 is not the same as connection1
|
||||
assertNotSame( connection1, connection2 );
|
||||
|
||||
// Execute a bulk operation on s2 (using connection2)
|
||||
assertEquals(
|
||||
1,
|
||||
s2.createQuery( "delete from Being where species = 'Martian'" ).executeUpdate()
|
||||
);
|
||||
// Execute a bulk operation on s2 (using connection2)
|
||||
assertEquals(
|
||||
1,
|
||||
s2.createQuery( "delete from Being where species = 'Martian'" ).executeUpdate()
|
||||
);
|
||||
|
||||
// Assert the Connection has not changed
|
||||
assertSame( connection2, s2Implementor.connection() );
|
||||
}
|
||||
// Assert the Connection has not changed
|
||||
assertSame( connection2, s2Implementor.connection() );
|
||||
}
|
||||
);
|
||||
|
||||
// Assert that the Connection used by s1 has hot changed.
|
||||
|
@ -527,7 +559,7 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
// Assert that the Connection used by s1 has hot changed.
|
||||
assertSame( connection1, s1Implementor.connection() );
|
||||
|
||||
});
|
||||
} );
|
||||
|
||||
// Clean up
|
||||
doInHibernate(
|
||||
|
@ -537,7 +569,7 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
|
|||
s.createQuery( "delete from Being" ).executeUpdate();
|
||||
s.createQuery( "delete from Hive" ).executeUpdate();
|
||||
s.createQuery( "delete from Location" ).executeUpdate();
|
||||
} );
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue