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

View File

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

View File

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

View File

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

View File

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