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 ) );
assertEquals( 2, list.size() ); // criteria.add( disjunction );
transaction.rollback(); //
s.close(); // List list = criteria.list();
assertEquals( 2, list.size() );
}
);
} }
@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,59 +73,76 @@ 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" ); Stay stay3 = new Stay( null, new Date(), new Date(), "A340", "Blah", "Blah" );
Stay stay3 = new Stay( null, new Date(), new Date(), "A340", "Blah", "Blah" ); stay.setOldPerson( p );
stay.setOldPerson( p ); stay2.setVeryOldPerson( p );
stay2.setVeryOldPerson( p ); stay3.setVeryOldPerson( p );
stay3.setVeryOldPerson( p ); p.addOldStay( stay );
p.addOldStay( stay ); p.addVeryOldStay( stay2 );
p.addVeryOldStay( stay2 ); p.addVeryOldStay( stay3 );
p.addVeryOldStay( stay3 ); s.persist( p );
s.persist( p ); tx.commit();
tx.commit(); s.clear();
s.clear(); tx = s.beginTransaction();
tx = s.beginTransaction(); p = (Person) s.createQuery( "from Person p where p.firstName = :name" )
p = (Person) s.createQuery( "from Person p where p.firstName = :name" ) .setParameter( "name", "Gavin" ).uniqueResult();
.setParameter( "name", "Gavin" ).uniqueResult(); assertFalse( Hibernate.isInitialized( p.getOldStays() ) );
assertFalse( Hibernate.isInitialized( p.getOldStays() ) ); assertEquals( 1, p.getOldStays().size() );
assertEquals( 1, p.getOldStays().size() ); 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(
assertTrue( "FetchMode.JOIN should overrides lazy options", Hibernate.isInitialized( stay3.getVeryOldPerson() ) ); "FetchMode.JOIN should overrides lazy options",
s.delete( stay3.getVeryOldPerson() ); Hibernate.isInitialized( stay3.getVeryOldPerson() )
tx.commit(); );
s.close(); s.delete( stay3.getVeryOldPerson() );
tx.commit();
}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( );
s.getTransaction().begin(); try {
s.persist( b ); s.getTransaction().begin();
s.flush(); s.persist( b );
Leaf l = new Leaf(); s.flush();
l.setBranch( b ); Leaf l = new Leaf();
s.persist( l ); l.setBranch( b );
s.flush(); s.persist( l );
s.flush();
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();
s.getTransaction().rollback(); }
s.close(); finally {
if ( s.getTransaction().isActive() ) {
s.getTransaction().rollback();
}
s.close();
}
} }
@Override @Override

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,125 +39,132 @@ public class SubclassTest extends BaseCoreFunctionalTestCase {
protected boolean isCleanupTestDataRequired() { protected boolean isCleanupTestDataRequired() {
return true; return true;
} }
@Test
public void testPolymorphism() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Plane p = new Plane();
p.setNbrOfSeats( 10 );
A320 a = new A320();
a.setJavaEmbeddedVersion( "5.0" );
a.setNbrOfSeats( 300 );
s.persist( a );
s.persist( p );
tx.commit();
s.close();
s = openSession(); @Test
tx = s.beginTransaction(); public void testPolymorphism() {
Query q = s.createQuery( "from " + A320.class.getName() ); inTransaction(
List a320s = q.list(); s -> {
assertNotNull( a320s ); Plane p = new Plane();
assertEquals( 1, a320s.size() ); p.setNbrOfSeats( 10 );
assertTrue( a320s.get( 0 ) instanceof A320 ); A320 a = new A320();
assertEquals( "5.0", ( (A320) a320s.get( 0 ) ).getJavaEmbeddedVersion() ); a.setJavaEmbeddedVersion( "5.0" );
q = s.createQuery( "from " + Plane.class.getName() ); a.setNbrOfSeats( 300 );
List planes = q.list(); s.persist( a );
assertNotNull( planes ); s.persist( p );
assertEquals( 2, planes.size() ); }
tx.commit(); );
s.close();
inTransaction(
s -> {
Query q = s.createQuery( "from " + A320.class.getName() );
List a320s = q.list();
assertNotNull( a320s );
assertEquals( 1, a320s.size() );
assertTrue( a320s.get( 0 ) instanceof A320 );
assertEquals( "5.0", ( (A320) a320s.get( 0 ) ).getJavaEmbeddedVersion() );
q = s.createQuery( "from " + Plane.class.getName() );
List planes = q.list();
assertNotNull( planes );
assertEquals( 2, planes.size() );
}
);
} }
@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(); Rock guns = new Rock();
tx = s.beginTransaction(); guns.setAvgBeat( 90 );
Rock guns = new Rock(); guns.setType( 2 );
guns.setAvgBeat( 90 ); Noise white = new Noise();
guns.setType( 2 ); white.setAvgBeat( 0 );
Noise white = new Noise(); white.setType( null );
white.setAvgBeat( 0 );
white.setType( null );
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" );
s.persist( doc ); inTransaction(
s.persist( folder ); s -> {
tx.commit(); s.persist( doc );
s.close(); s.persist( folder );
}
s = openSession(); );
tx = s.beginTransaction(); inTransaction(
List result = s.createCriteria( File.class ).list(); s -> {
assertNotNull( result ); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
assertEquals( 2, result.size() ); CriteriaQuery<File> criteria = criteriaBuilder.createQuery( File.class );
File f2 = (File) result.get( 0 ); criteria.from( File.class );
checkClassType( f2, doc, folder ); List<File> result = s.createQuery( criteria ).list();
f2 = (File) result.get( 1 ); // List result = s.createCriteria( File.class ).list();
checkClassType( f2, doc, folder ); assertNotNull( result );
s.delete( result.get( 0 ) ); assertEquals( 2, result.size() );
s.delete( result.get( 1 ) ); File f2 = result.get( 0 );
tx.commit(); checkClassType( f2, doc, folder );
s.close(); f2 = result.get( 1 );
checkClassType( f2, doc, folder );
s.delete( result.get( 0 ) );
s.delete( result.get( 1 ) );
}
);
} }
@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();
s.close(); tx.commit();
}
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" );
s.persist( doc );
s.persist( folder );
try { try {
s.persist( doc );
s.persist( folder );
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();
assertNotNull( result ); CriteriaQuery<File> criteria = criteriaBuilder.createQuery( File.class );
assertEquals( 2, result.size() ); criteria.from( File.class );
File f2 = (File) result.get( 0 ); List<File> result = session.createQuery( criteria ).list();
checkClassType( f2, doc, folder );
f2 = (File) result.get( 1 ); // List result = session.createCriteria( File.class ).list();
checkClassType( f2, doc, folder ); assertNotNull( result );
s.delete( result.get( 0 ) ); assertEquals( 2, result.size() );
s.delete( result.get( 1 ) ); File f2 = result.get( 0 );
tx.commit(); checkClassType( f2, doc, folder );
s.close(); f2 = result.get( 1 );
checkClassType( f2, doc, folder );
session.delete( result.get( 0 ) );
session.delete( result.get( 1 ) );
}
);
} }
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" );
s.persist( doc ); inTransaction(
s.persist( folder ); s -> {
tx.commit(); s.persist( doc );
s.close(); s.persist( folder );
}
);
s = openSession(); inTransaction(
tx = s.beginTransaction(); s -> {
List result = s.createCriteria( File.class ).list(); CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
assertNotNull( result ); CriteriaQuery<File> criteria = criteriaBuilder.createQuery( File.class );
assertEquals( 2, result.size() ); criteria.from( File.class );
File f2 = (File) result.get( 0 ); List<File> result = s.createQuery( criteria ).list();
checkClassType( f2, doc, folder ); // List result = s.createCriteria( File.class ).list();
f2 = (File) result.get( 1 ); assertNotNull( result );
checkClassType( f2, doc, folder ); assertEquals( 2, result.size() );
tx.commit(); File f2 = result.get( 0 );
s.close(); checkClassType( f2, doc, folder );
f2 = result.get( 1 );
checkClassType( f2, doc, folder );
}
);
} }
private void checkClassType(File fruitToTest, File f, Folder a) { private void checkClassType(File fruitToTest, File f, Folder a) {