6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-17 14:27:50 +01:00
parent 3dbcf28b41
commit d99a3f1938
6 changed files with 591 additions and 543 deletions

View File

@ -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,8 +322,8 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
s.close();
}
public void testSQLFunctions() throws Exception {
Session s = openSession();
public void testSQLFunctions() {
try(Session s = openSession()) {
Transaction t = s.beginTransaction();
Simple simple = new Simple( Long.valueOf( 10 ) );
simple.setName( "Simple 1" );
@ -346,7 +347,9 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
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
s.createQuery( "from Simple s where lower( concat(s.name, ' foo') ) ='simple 1 foo'" )
.list()
.size() == 1
);
Simple other = new Simple( Long.valueOf( 20 ) );
@ -397,17 +400,16 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
.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();
).list();
Query q = s.createQuery( "from Simple s" );
q.setMaxResults( 10 );
@ -424,23 +426,27 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
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");
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 );
assertTrue( q.iterate().hasNext() );
assertFalse( q.list().isEmpty() );
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++;
list = q.list();
for ( Object l : list ) {
assertTrue( l instanceof Long );
}
assertTrue( i == 2 );
// 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 );
@ -463,7 +469,7 @@ public class SQLFunctionsInterSystemsTest extends BaseCoreFunctionalTestCase {
s.delete( simple );
s.delete( min );
t.commit();
s.close();
}
}

View File

@ -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();
}

View File

@ -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();

View File

@ -247,6 +247,7 @@ public class FunctionNameAsColumnTest extends BaseCoreFunctionalTestCase {
if (s.getTransaction().isActive()){
s.getTransaction().rollback();
}
throw e;
}finally {
if(s.isOpen()) {
s.close();

View File

@ -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,8 +81,8 @@ 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();
inTransaction(
s -> {
Customer cust = new Customer( "Acme, Inc." );
Order order = new Order( new Order.Id( cust, 1 ) );
cust.getOrders().add( order );
@ -87,27 +90,26 @@ public class EagerKeyManyToOneTest extends BaseCoreFunctionalTestCase {
s.flush();
assertEquals( 2, sessionFactory().getStatistics().getEntityInsertCount() );
s.delete( cust );
s.getTransaction().commit();
s.close();
}
);
}
@Test
public void testLoadingStrategies() {
sessionFactory().getStatistics().clear();
Session s = openSession();
s.beginTransaction();
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.getTransaction().commit();
s.close();
}
);
s = openSession();
s.beginTransaction();
cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
inTransaction(
s -> {
Customer cust = ( Customer ) s.createQuery( "from Customer" ).uniqueResult();
assertEquals( 1, cust.getOrders().size() );
s.clear();
@ -119,13 +121,16 @@ public class EagerKeyManyToOneTest extends BaseCoreFunctionalTestCase {
assertEquals( 1, cust.getOrders().size() );
s.clear();
cust = ( Customer ) s.createCriteria( Customer.class ).uniqueResult();
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();
s.delete( cust );
s.getTransaction().commit();
s.close();
}
);
}
@Test

View File

@ -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;
@ -58,7 +57,6 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
connectionProvider.stop();
}
@Override
public String[] getMappings() {
return new String[] { "unionsubclass/Beings.hbm.xml" };
@ -66,8 +64,8 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
@Test
public void testUnionSubclassCollection() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Location mel = new Location( "Earth" );
s.save( mel );
@ -79,24 +77,26 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
gavin.getInfo().put( "foo", "bar" );
gavin.getInfo().put( "x", "y" );
}
);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
gavin = (Human) s.createCriteria(Human.class).uniqueResult();
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() );
t.commit();
s.close();
}
);
}
@Test
public void testUnionSubclassFetchMode() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Location mel = new Location( "Earth" );
s.save( mel );
@ -114,26 +114,30 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
s.flush();
s.clear();
List list = s.createCriteria(Human.class)
.setFetchMode("location", FetchMode.JOIN)
.setFetchMode("location.beings", FetchMode.JOIN)
.list();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Human> criteria = criteriaBuilder.createQuery( Human.class );
criteria.from( Human.class ).fetch( "location", JoinType.LEFT ).fetch( "beings", JoinType.LEFT );
for ( Object aList : list ) {
Human h = (Human) aList;
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() ) );
t.commit();
s.close();
}
);
}
@Test
public void testUnionSubclassOneToMany() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Location mel = new Location( "Melbourne, Australia" );
Location mars = new Location( "Mars" );
s.save( mel );
@ -177,13 +181,15 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
s.clear();
hive = (Hive) s.createQuery("from Hive h left join fetch h.location left join fetch h.members").uniqueResult();
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();
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 );
@ -195,19 +201,24 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
s.clear();
x23y4 = (Alien) s.createCriteria(Alien.class).addOrder( Order.asc("identity") ).list().get(0);
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() );
t.commit();
s.close();
}
);
}
@Test
public void testUnionSubclassManyToOne() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Location mel = new Location( "Melbourne, Australia" );
Location mars = new Location( "Mars" );
s.save( mel );
@ -245,7 +256,8 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
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();
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();
@ -263,17 +275,19 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
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();
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();
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() );
thing = s.get( Thing.class, thing.getId() );
assertFalse( Hibernate.isInitialized( thing.getOwner() ) );
assertEquals( thing.getOwner().getIdentity(), "gavin" );
@ -285,24 +299,28 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
s.clear();
thing = (Thing) s.get( Thing.class, thing.getId() );
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();
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() );
t.commit();
s.close();
}
);
}
@Test
public void testUnionSubclass() {
Session s = openSession();
Transaction t = s.beginTransaction();
inTransaction(
s -> {
Location mel = new Location( "Melbourne, Australia" );
Location atl = new Location( "Atlanta, GA" );
Location mars = new Location( "Mars" );
@ -330,8 +348,17 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
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 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();
@ -385,27 +412,31 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
assertEquals( locations.size(), 3 );
s.clear();
gavin = (Human) s.get( Human.class, gavin.getId() );
atl = (Location) s.get( Location.class, atl.getId() );
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 );
x23y4 = (Alien) s.createCriteria(Alien.class).uniqueResult();
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();
t.commit();
s.close();
}
);
}
@Test
public void testNestedUnionedSubclasses() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
inTransaction(
s -> {
Location mel = new Location( "Earth" );
Human marcf = new Human();
marcf.setIdentity( "marc" );
@ -417,10 +448,11 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
steve.setSalary( (double) 0 );
mel.addBeing( steve );
s.persist( mel );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
}
);
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" );
@ -429,8 +461,8 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
s.delete( result.get( 0 ) );
s.delete( result.get( 1 ) );
s.delete( ( (Human) result.get( 0 ) ).getLocation() );
tx.commit();
s.close();
}
);
}
@Test