6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-17 16:07:55 +01:00
parent 3f3e4c24e8
commit dd5d399f27
6 changed files with 313 additions and 247 deletions

View File

@ -10,6 +10,10 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.junit.Test;
import org.hibernate.query.Query;
@ -417,24 +421,30 @@ public class CompositeIdTest extends BaseCoreFunctionalTestCase {
@Test
public void testQueryInAndComposite() {
Session s = openSession( );
Transaction transaction = s.beginTransaction();
inTransaction(
s -> {
createData( s );
s.flush();
List ids = new ArrayList<SomeEntityId>(2);
ids.add( new SomeEntityId(1,12) );
ids.add( new SomeEntityId(10,23) );
Criteria criteria = s.createCriteria( SomeEntity.class );
Disjunction disjunction = Restrictions.disjunction();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<SomeEntity> criteria = criteriaBuilder.createQuery( SomeEntity.class );
Root<SomeEntity> root = criteria.from( SomeEntity.class );
criteria.where( criteriaBuilder.or( criteriaBuilder.in( root.get( "id" )).value( ids) ) );
List list = s.createQuery( criteria ).list();
disjunction.add( Restrictions.in( "id", ids ) );
criteria.add( disjunction );
List list = criteria.list();
// Criteria criteria = s.createCriteria( SomeEntity.class );
// Disjunction disjunction = Restrictions.disjunction();
//
// disjunction.add( Restrictions.in( "id", ids ) );
// criteria.add( disjunction );
//
// List list = criteria.list();
assertEquals( 2, list.size() );
transaction.rollback();
s.close();
}
);
}
@Test

View File

@ -8,6 +8,9 @@ package org.hibernate.test.annotations.fetch;
import java.util.Date;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.junit.Test;
import org.hibernate.Hibernate;
@ -24,7 +27,7 @@ import static org.junit.Assert.assertTrue;
*/
public class FetchingTest extends BaseCoreFunctionalTestCase {
@Test
public void testLazy() throws Exception {
public void testLazy() {
Session s;
Transaction tx;
s = openSession();
@ -45,7 +48,7 @@ public class FetchingTest extends BaseCoreFunctionalTestCase {
}
@Test
public void testExtraLazy() throws Exception {
public void testExtraLazy() {
Session s;
Transaction tx;
s = openSession();
@ -70,11 +73,10 @@ public class FetchingTest extends BaseCoreFunctionalTestCase {
}
@Test
public void testHibernateFetchingLazy() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
public void testHibernateFetchingLazy() {
try(Session s = openSession()) {
Transaction tx = s.beginTransaction();
try {
Person p = new Person( "Gavin", "King", "JBoss Inc" );
Stay stay = new Stay( null, new Date(), new Date(), "A380", "Blah", "Blah" );
Stay stay2 = new Stay( null, new Date(), new Date(), "A320", "Blah", "Blah" );
@ -96,19 +98,28 @@ public class FetchingTest extends BaseCoreFunctionalTestCase {
assertFalse( "lazy extra is failing", Hibernate.isInitialized( p.getOldStays() ) );
s.clear();
stay = (Stay) s.get( Stay.class, stay.getId() );
assertTrue( ! Hibernate.isInitialized( stay.getOldPerson() ) );
assertTrue( !Hibernate.isInitialized( stay.getOldPerson() ) );
s.clear();
stay3 = (Stay) s.get( Stay.class, stay3.getId() );
assertTrue( "FetchMode.JOIN should overrides lazy options", Hibernate.isInitialized( stay3.getVeryOldPerson() ) );
assertTrue(
"FetchMode.JOIN should overrides lazy options",
Hibernate.isInitialized( stay3.getVeryOldPerson() )
);
s.delete( stay3.getVeryOldPerson() );
tx.commit();
s.close();
}finally {
if ( tx.isActive() ) {
tx.rollback();
}
}
}
}
@Test
public void testOneToManyFetchEager() throws Exception {
public void testOneToManyFetchEager() {
Branch b = new Branch();
Session s = openSession( );
try {
s.getTransaction().begin();
s.persist( b );
s.flush();
@ -119,11 +130,20 @@ public class FetchingTest extends BaseCoreFunctionalTestCase {
s.clear();
s.createCriteria( Branch.class ).list();
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Branch> criteria = criteriaBuilder.createQuery( Branch.class );
criteria.from( Branch.class );
s.createQuery( criteria ).list();
// s.createCriteria( Branch.class ).list();
}
finally {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
s.close();
}
}
@Override
protected Class[] getAnnotatedClasses() {

View File

@ -8,11 +8,14 @@ package org.hibernate.test.annotations.inheritance;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.junit.Test;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.test.annotations.A320;
import org.hibernate.test.annotations.A320b;
import org.hibernate.test.annotations.Plane;
@ -23,7 +26,7 @@ import org.hibernate.test.annotations.inheritance.singletable.Rock;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@ -36,10 +39,11 @@ public class SubclassTest extends BaseCoreFunctionalTestCase {
protected boolean isCleanupTestDataRequired() {
return true;
}
@Test
public void testPolymorphism() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
public void testPolymorphism() {
inTransaction(
s -> {
Plane p = new Plane();
p.setNbrOfSeats( 10 );
A320 a = new A320();
@ -47,11 +51,11 @@ public class SubclassTest extends BaseCoreFunctionalTestCase {
a.setNbrOfSeats( 300 );
s.persist( a );
s.persist( p );
tx.commit();
s.close();
}
);
s = openSession();
tx = s.beginTransaction();
inTransaction(
s -> {
Query q = s.createQuery( "from " + A320.class.getName() );
List a320s = q.list();
assertNotNull( a320s );
@ -62,66 +66,66 @@ public class SubclassTest extends BaseCoreFunctionalTestCase {
List planes = q.list();
assertNotNull( planes );
assertEquals( 2, planes.size() );
tx.commit();
s.close();
}
);
}
@Test
public void test2ndLevelSubClass() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
public void test2ndLevelSubClass() {
inTransaction(
s -> {
A320b a = new A320b();
a.setJavaEmbeddedVersion( "Elephant" );
a.setNbrOfSeats( 300 );
s.persist( a );
tx.commit();
s.close();
}
);
s = openSession();
tx = s.beginTransaction();
inTransaction(
s -> {
Query q = s.createQuery( "from " + A320.class.getName() + " as a where a.javaEmbeddedVersion = :version" );
q.setParameter( "version", "Elephant" );
List a320s = q.list();
assertNotNull( a320s );
assertEquals( 1, a320s.size() );
tx.commit();
s.close();
}
);
}
@Test
public void testEmbeddedSuperclass() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Plane p = new Plane();
p.setAlive( true ); //sic
p.setAltitude( 10000 );
p.setMetricAltitude( 3000 );
p.setNbrOfSeats( 150 );
p.setSerial( "0123456789" );
s.persist( p );
tx.commit();
s.close();
public void testEmbeddedSuperclass() {
Plane plane = new Plane();
inTransaction(
s -> {
plane.setAlive( true ); //sic
plane.setAltitude( 10000 );
plane.setMetricAltitude( 3000 );
plane.setNbrOfSeats( 150 );
plane.setSerial( "0123456789" );
s.persist( plane );
}
);
s = openSession();
tx = s.beginTransaction();
p = (Plane) s.get( Plane.class, p.getId() );
inTransaction(
s -> {
Plane p = s.get( Plane.class, plane.getId() );
assertNotNull( p );
assertEquals( true, p.isAlive() );
assertTrue( p.isAlive() );
assertEquals( 150, p.getNbrOfSeats() );
assertEquals( 10000, p.getAltitude() );
assertEquals( "0123456789", p.getSerial() );
assertFalse( 3000 == p.getMetricAltitude() );
assertNotEquals( 3000, p.getMetricAltitude() );
s.delete( p );
tx.commit();
s.close();
}
);
}
@Test
public void testFormula() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
public void testFormula() {
inTransaction(
s -> {
Rock guns = new Rock();
guns.setAvgBeat( 90 );
guns.setType( 2 );
@ -131,30 +135,36 @@ public class SubclassTest extends BaseCoreFunctionalTestCase {
s.persist( guns );
s.persist( white );
tx.commit();
s.close();
}
);
s = openSession();
tx = s.beginTransaction();
List result = s.createCriteria( Noise.class ).list();
inTransaction(
s -> {
List result = createQueryForClass( s, Noise.class ).list();
assertNotNull( result );
assertEquals( 1, result.size() );
white = (Noise) result.get( 0 );
assertNull( white.getType() );
s.delete( white );
result = s.createCriteria( Rock.class ).list();
Noise w = (Noise) result.get( 0 );
assertNull( w.getType() );
s.delete( w );
result = createQueryForClass( s, Rock.class ).list();
assertEquals( 1, result.size() );
s.delete( result.get( 0 ) );
result = s.createCriteria( Funk.class ).list();
result = createQueryForClass( s, Funk.class ).list();
assertEquals( 0, result.size() );
}
);
}
tx.commit();
s.close();
private Query createQueryForClass(SessionImplementor session, Class clazz) {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery criteria = criteriaBuilder.createQuery( clazz );
criteria.from( clazz );
return session.createQuery( criteria );
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{
return new Class[] {
A320b.class, //subclasses should be properly reordered
Plane.class,
A320.class,

View File

@ -10,6 +10,9 @@ import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
@ -28,31 +31,32 @@ import static org.junit.Assert.fail;
*/
public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
@Test
public void testDefault() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
public void testDefault() {
File doc = new Document( "Enron Stuff To Shred", 1000 );
Folder folder = new Folder( "Enron" );
inTransaction(
s -> {
s.persist( doc );
s.persist( folder );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
List result = s.createCriteria( File.class ).list();
}
);
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<File> criteria = criteriaBuilder.createQuery( File.class );
criteria.from( File.class );
List<File> result = s.createQuery( criteria ).list();
// List result = s.createCriteria( File.class ).list();
assertNotNull( result );
assertEquals( 2, result.size() );
File f2 = (File) result.get( 0 );
File f2 = result.get( 0 );
checkClassType( f2, doc, folder );
f2 = (File) result.get( 1 );
f2 = result.get( 1 );
checkClassType( f2, doc, folder );
s.delete( result.get( 0 ) );
s.delete( result.get( 1 ) );
tx.commit();
s.close();
}
);
}
@Test
@ -62,22 +66,30 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
ProgramExecution remove = new ProgramExecution();
remove.setAction( "remove" );
remove.setAppliesOn( f );
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
try(Session s = openSession()) {
Transaction tx = s.beginTransaction();
try {
s.persist( f );
s.persist( remove );
tx.commit();
s.clear();
tx = s.beginTransaction();
remove = (ProgramExecution) s.get( ProgramExecution.class, remove.getId() );
remove = s.get( ProgramExecution.class, remove.getId() );
assertNotNull( remove );
assertNotNull( remove.getAppliesOn().getName() );
s.delete( remove );
s.delete( remove.getAppliesOn() );
tx.commit();
s.close();
}
catch (Exception e) {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
throw e;
}
}
}
private void checkClassType(File fruitToTest, File f, Folder a) {
@ -93,7 +105,7 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
}
@Test
public void testJoinedAbstractClass() throws Exception {
public void testJoinedAbstractClass() {
Session s;
s = openSession();
s.getTransaction().begin();
@ -107,18 +119,18 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
s = openSession();
s.getTransaction().begin();
sw = (Sweater) s.get( Sweater.class, sw.getId() );
sw = s.get( Sweater.class, sw.getId() );
s.delete( sw );
s.getTransaction().commit();
s.close();
}
@Test
public void testInheritance() throws Exception {
public void testInheritance() {
Session session = openSession();
Transaction transaction = session.beginTransaction();
String eventPK = "event1";
EventInformation event = (EventInformation) session.get( EventInformation.class, eventPK );
EventInformation event = session.get( EventInformation.class, eventPK );
if ( event == null ) {
event = new EventInformation();
event.setNotificationId( eventPK );
@ -162,13 +174,13 @@ public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
s.flush();
s.clear();
c1 = (Client) s.load(Client.class, c1.getId());
c1 = s.load(Client.class, c1.getId());
assertEquals( 5000.0, c1.getAccount().getBalance(), 0.01 );
s.flush();
s.clear();
a1 = (Account) s.load(Account.class,a1.getId());
a1 = s.load(Account.class,a1.getId());
Set<Client> clients = a1.getClients();
assertEquals(1, clients.size());
Iterator<Client> it = clients.iterator();

View File

@ -8,6 +8,9 @@ package org.hibernate.test.annotations.inheritance.mixed;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.junit.Test;
import org.hibernate.Session;
@ -26,16 +29,17 @@ import static org.junit.Assert.fail;
*/
public class SubclassTest extends BaseCoreFunctionalTestCase {
@Test
public void testDefault() throws Exception {
public void testDefault() {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
File doc = new Document( "Enron Stuff To Shred", 1000 );
Folder folder = new Folder( "Enron" );
try {
s.persist( doc );
s.persist( folder );
try {
tx.commit();
}
catch (SQLGrammarException e) {
@ -43,19 +47,24 @@ public class SubclassTest extends BaseCoreFunctionalTestCase {
}
s.close();
s = openSession();
tx = s.beginTransaction();
List result = s.createCriteria( File.class ).list();
inTransaction(
session -> {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<File> criteria = criteriaBuilder.createQuery( File.class );
criteria.from( File.class );
List<File> result = session.createQuery( criteria ).list();
// List result = session.createCriteria( File.class ).list();
assertNotNull( result );
assertEquals( 2, result.size() );
File f2 = (File) result.get( 0 );
File f2 = result.get( 0 );
checkClassType( f2, doc, folder );
f2 = (File) result.get( 1 );
f2 = result.get( 1 );
checkClassType( f2, doc, folder );
s.delete( result.get( 0 ) );
s.delete( result.get( 1 ) );
tx.commit();
s.close();
session.delete( result.get( 0 ) );
session.delete( result.get( 1 ) );
}
);
}
private void checkClassType(File fruitToTest, File f, Folder a) {

View File

@ -8,6 +8,9 @@ package org.hibernate.test.annotations.inheritance.union;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.junit.Test;
import org.hibernate.Session;
@ -26,29 +29,31 @@ import static org.junit.Assert.fail;
*/
public class SubclassTest extends BaseCoreFunctionalTestCase {
@Test
public void testDefault() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
public void testDefault() {
File doc = new Document( "Enron Stuff To Shred", 1000 );
Folder folder = new Folder( "Enron" );
inTransaction(
s -> {
s.persist( doc );
s.persist( folder );
tx.commit();
s.close();
}
);
s = openSession();
tx = s.beginTransaction();
List result = s.createCriteria( File.class ).list();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<File> criteria = criteriaBuilder.createQuery( File.class );
criteria.from( File.class );
List<File> result = s.createQuery( criteria ).list();
// List result = s.createCriteria( File.class ).list();
assertNotNull( result );
assertEquals( 2, result.size() );
File f2 = (File) result.get( 0 );
File f2 = result.get( 0 );
checkClassType( f2, doc, folder );
f2 = (File) result.get( 1 );
f2 = result.get( 1 );
checkClassType( f2, doc, folder );
tx.commit();
s.close();
}
);
}
private void checkClassType(File fruitToTest, File f, Folder a) {