6 - SQM based on JPA type system

This commit is contained in:
Andrea Boriero 2019-07-18 15:43:17 +01:00
parent 665808b5af
commit 42bd76d565
16 changed files with 1012 additions and 742 deletions

View File

@ -6,15 +6,15 @@
*/
package org.hibernate.test.annotations;
import org.junit.Test;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.annotations.inheritance.Carrot;
import org.hibernate.test.annotations.inheritance.Tomato;
import org.hibernate.test.annotations.inheritance.Vegetable;
import org.hibernate.test.annotations.inheritance.VegetablePk;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -26,85 +26,83 @@ import static org.junit.Assert.assertTrue;
public class JoinedSubclassTest extends BaseCoreFunctionalTestCase {
@Test
public void testDefaultValues() {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Ferry f = new Ferry();
f.setSize( 2 );
f.setSea( "Channel" );
s.persist( f );
tx.commit();
s.close();
Ferry ferry = new Ferry();
inTransaction(
s -> {
ferry.setSize( 2 );
ferry.setSea( "Channel" );
s.persist( ferry );
}
);
s = openSession();
tx = s.beginTransaction();
f = (Ferry) s.get( Ferry.class, f.getId() );
assertNotNull( f );
assertEquals( "Channel", f.getSea() );
assertEquals( 2, f.getSize() );
s.delete( f );
tx.commit();
s.close();
inTransaction(
s -> {
Ferry f = s.get( Ferry.class, ferry.getId() );
assertNotNull( f );
assertEquals( "Channel", f.getSea() );
assertEquals( 2, f.getSize() );
s.delete( f );
}
);
}
@Test
public void testDeclaredValues() {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Country c = new Country();
c.setName( "France" );
AmericaCupClass f = new AmericaCupClass();
f.setSize( 2 );
f.setCountry( c );
s.persist( c );
s.persist( f );
tx.commit();
s.close();
Country country = new Country();
AmericaCupClass americaCupClass = new AmericaCupClass();
inTransaction(
s -> {
country.setName( "France" );
americaCupClass.setSize( 2 );
americaCupClass.setCountry( country );
s.persist( country );
s.persist( americaCupClass );
}
);
s = openSession();
tx = s.beginTransaction();
f = (AmericaCupClass) s.get( AmericaCupClass.class, f.getId() );
assertNotNull( f );
assertEquals( c, f.getCountry() );
assertEquals( 2, f.getSize() );
s.delete( f );
s.delete( f.getCountry() );
tx.commit();
s.close();
inTransaction(
s -> {
AmericaCupClass f = s.get( AmericaCupClass.class, americaCupClass.getId() );
assertNotNull( f );
assertEquals( country, f.getCountry() );
assertEquals( 2, f.getSize() );
s.delete( f );
s.delete( f.getCountry() );
}
);
}
@Test
public void testCompositePk() throws Exception {
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
Carrot c = new Carrot();
VegetablePk pk = new VegetablePk();
pk.setFarmer( "Bill" );
pk.setHarvestDate( "2004-08-15" );
c.setId( pk );
c.setLength( 23 );
s.persist( c );
tx.commit();
s.close();
public void testCompositePk() {
inTransaction(
s -> {
Carrot c = new Carrot();
VegetablePk pk = new VegetablePk();
pk.setFarmer( "Bill" );
pk.setHarvestDate( "2004-08-15" );
c.setId( pk );
c.setLength( 23 );
s.persist( c );
}
);
s = openSession();
tx = s.beginTransaction();
Vegetable v = (Vegetable) s.createCriteria( Vegetable.class ).uniqueResult();
assertTrue( v instanceof Carrot );
Carrot result = (Carrot) v;
assertEquals( 23, result.getLength() );
tx.commit();
s.close();
inTransaction(
s -> {
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
CriteriaQuery<Vegetable> criteria = criteriaBuilder.createQuery( Vegetable.class );
criteria.from( Vegetable.class );
Vegetable v = s.createQuery( criteria ).uniqueResult();
// Vegetable v = (Vegetable) s.createCriteria( Vegetable.class ).uniqueResult();
assertTrue( v instanceof Carrot );
Carrot result = (Carrot) v;
assertEquals( 23, result.getLength() );
}
);
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{
return new Class[] {
Boat.class,
Ferry.class,
AmericaCupClass.class,

View File

@ -89,7 +89,7 @@ public class BatchTest extends BaseCoreFunctionalTestCase {
ScrollableResults sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get( 0 );
DataPoint dp = ( DataPoint ) sr.get();
dp.setDescription( "done!" );
if ( ++i % nBeforeFlush == 0 ) {
s.flush();
@ -106,7 +106,7 @@ public class BatchTest extends BaseCoreFunctionalTestCase {
sr = s.createQuery( "from DataPoint dp order by dp.x asc" )
.scroll( ScrollMode.FORWARD_ONLY );
while ( sr.next() ) {
DataPoint dp = ( DataPoint ) sr.get( 0 );
DataPoint dp = ( DataPoint ) sr.get();
s.delete( dp );
if ( ++i % nBeforeFlush == 0 ) {
s.flush();

View File

@ -81,9 +81,9 @@ public class CascadeManagedAndTransientTest extends BaseCoreFunctionalTestCase
Session s = openSession();
s.beginTransaction();
Route route = (Route) s.createQuery("FROM Route WHERE name = :name").setString("name", "Route 1").uniqueResult();
Node n2 = (Node) s.createQuery("FROM Node WHERE name = :name").setString("name", "Node 2").uniqueResult();
Node n3 = (Node) s.createQuery("FROM Node WHERE name = :name").setString("name", "Node 3").uniqueResult();
Route route = (Route) s.createQuery("FROM Route WHERE name = :name").setParameter("name", "Route 1").uniqueResult();
Node n2 = (Node) s.createQuery("FROM Node WHERE name = :name").setParameter("name", "Node 2").uniqueResult();
Node n3 = (Node) s.createQuery("FROM Node WHERE name = :name").setParameter("name", "Node 3").uniqueResult();
Vehicle vehicle = new Vehicle();
vehicle.setName("Bus");

View File

@ -140,38 +140,6 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
done();
}
@Test
public void testQueryIteration() throws Throwable {
prepare();
Session s = getSessionUnderTest();
Silly silly = new Silly( "silly" );
s.save( silly );
s.flush();
Iterator itr = s.createQuery( "from Silly" ).iterate();
assertTrue( itr.hasNext() );
Silly silly2 = ( Silly ) itr.next();
assertEquals( silly, silly2 );
Hibernate.close( itr );
itr = s.createQuery( "from Silly" ).iterate();
Iterator itr2 = s.createQuery( "from Silly where name = 'silly'" ).iterate();
assertTrue( itr.hasNext() );
assertEquals( silly, itr.next() );
assertTrue( itr2.hasNext() );
assertEquals( silly, itr2.next() );
Hibernate.close( itr );
Hibernate.close( itr2 );
s.delete( silly );
s.flush();
release( s );
done();
}
@Test
public void testQueryScrolling() throws Throwable {
prepare();
@ -182,7 +150,7 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
assertTrue( sr.next() );
Silly silly2 = ( Silly ) sr.get( 0 );
Silly silly2 = ( Silly ) sr.get();
assertEquals( silly, silly2 );
sr.close();
@ -190,9 +158,9 @@ public class AggressiveReleaseTest extends ConnectionManagementTestCase {
ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll();
assertTrue( sr.next() );
assertEquals( silly, sr.get( 0 ) );
assertEquals( silly, sr.get() );
assertTrue( sr2.next() );
assertEquals( silly, sr2.get( 0 ) );
assertEquals( silly, sr2.get() );
sr.close();
sr2.close();

View File

@ -151,8 +151,8 @@ public class JoinedFilteredBulkManipulationTest extends BaseCoreFunctionalTestCa
s.beginTransaction();
s.enableFilter( "sex" ).setParameter( "sexCode", Character.valueOf( 'M' ) );
int count = s.createQuery( "update User u set u.username = :un where u.name = :n" )
.setString( "un", "charlie" )
.setString( "n", "Wanda" )
.setParameter( "un", "charlie" )
.setParameter( "n", "Wanda" )
.executeUpdate();
assertEquals( 0, count );
s.getTransaction().commit();

View File

@ -0,0 +1,65 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.iterate;
import org.hibernate.Hibernate;
import org.hibernate.ScrollableResults;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class ScrollTest extends BaseCoreFunctionalTestCase {
public String[] getMappings() {
return new String[] { "iterate/Item.hbm.xml" };
}
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.USE_QUERY_CACHE, "true" );
cfg.setProperty( Environment.CACHE_REGION_PREFIX, "foo" );
cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "true" );
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
@Test
public void testScroll() {
inTransaction(
s -> {
Item i1 = new Item( "foo" );
Item i2 = new Item( "bar" );
s.persist( "Item", i1 );
s.persist( "Item", i2 );
}
);
inTransaction(
s -> {
ScrollableResults sr = s.getNamedQuery( "Item.nameDesc" ).scroll();
assertTrue( sr.next() );
Item i1 = (Item) sr.get();
assertTrue( sr.next() );
Item i2 = (Item) sr.get();
assertTrue( Hibernate.isInitialized( i1 ) );
assertTrue( Hibernate.isInitialized( i2 ) );
assertEquals( i1.getName(), "foo" );
assertEquals( i2.getName(), "bar" );
assertFalse( sr.next() );
s.delete( i1 );
s.delete( i2 );
}
);
assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 );
}
}

View File

@ -31,6 +31,7 @@ import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jdbc.Work;

View File

@ -21,6 +21,7 @@ import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jdbc.Work;
import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor;

View File

@ -22,6 +22,7 @@ import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jdbc.Work;
import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor;

View File

@ -24,6 +24,7 @@ import java.util.Set;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jdbc.Work;
import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor;

View File

@ -17,6 +17,7 @@ import java.util.List;
import org.hibernate.Session;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.jdbc.Work;
import org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor;

View File

@ -6,18 +6,15 @@
*/
package org.hibernate.test.orphan;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "HHH-565" )
@TestForIssue(jiraKey = "HHH-565")
public class PropertyRefTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
@ -26,28 +23,33 @@ public class PropertyRefTest extends BaseCoreFunctionalTestCase {
@Test
public void testDeleteParentWithBidirOrphanDeleteCollectionBasedOnPropertyRef() {
Session session = openSession();
Transaction txn = session.beginTransaction();
User user = new User( "test" );
user.addMail( "test" );
user.addMail( "test" );
session.save( user );
txn.commit();
session.close();
inTransaction(
session -> {
user.addMail( "test" );
user.addMail( "test" );
session.save( user );
}
);
session = openSession();
txn = session.beginTransaction();
user = ( User ) session.load( User.class, user.getId() );
session.delete( user );
txn.commit();
session.close();
inTransaction(
session -> {
User u = session.load( User.class, user.getId() );
session.delete( u );
}
);
session = openSession();
txn = session.beginTransaction();
session.createQuery( "delete from Mail where alias = :alias" ).setString( "alias", "test" ).executeUpdate();
session.createQuery( "delete from User where userid = :userid" ).setString( "userid", "test" ).executeUpdate();
txn.commit();
session.close();
inTransaction(
s -> {
session.createQuery( "delete from Mail where alias = :alias" )
.setParameter( "alias", "test" )
.executeUpdate();
session.createQuery( "delete from User where userid = :userid" )
.setParameter( "userid", "test" )
.executeUpdate();
}
);
}
}

View File

@ -0,0 +1,99 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.test.connections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.ConnectionReleaseMode;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Environment;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorBuilderImpl;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.jta.TestingJtaBootstrap;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Andrea Boriero
*/
@RequiresDialect(H2Dialect.class)
public class AggressiveReleaseQueryIterationTest extends ConnectionManagementTestCase {
@Override
@SuppressWarnings("unchecked")
protected void addSettings(Map settings) {
super.addSettings( settings );
TestingJtaBootstrap.prepare( settings );
// settings.put( Environment.TRANSACTION_STRATEGY, CMTTransactionFactory.class.getName() );
settings.put( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, JtaTransactionCoordinatorBuilderImpl.class.getName() );
settings.put( Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.AFTER_STATEMENT.toString() );
settings.put( Environment.GENERATE_STATISTICS, "true" );
settings.put( Environment.STATEMENT_BATCH_SIZE, "0" );
}
@Override
protected Session getSessionUnderTest() throws Throwable {
return openSession();
}
@Override
protected void reconnect(Session session) {
}
@Override
protected void prepare() throws Throwable {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
}
@Override
protected void done() throws Throwable {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
}
@Test
public void testQueryIteration() throws Throwable {
prepare();
Session s = getSessionUnderTest();
Silly silly = new Silly( "silly" );
s.save( silly );
s.flush();
List itr = s.createQuery( "from Silly" ).iterate();
assertTrue( itr.hasNext() );
Silly silly2 = ( Silly ) itr.next();
assertEquals( silly, silly2 );
Hibernate.close( itr );
itr = s.createQuery( "from Silly" ).iterate();
Iterator itr2 = s.createQuery( "from Silly where name = 'silly'" ).iterate();
assertTrue( itr.hasNext() );
assertEquals( silly, itr.next() );
assertTrue( itr2.hasNext() );
assertEquals( silly, itr2.next() );
Hibernate.close( itr );
Hibernate.close( itr2 );
s.delete( silly );
s.flush();
release( s );
done();
}
}

View File

@ -71,35 +71,6 @@ public class IterateTest extends BaseCoreFunctionalTestCase {
assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 2 );
}
@Test
public void testScroll() throws Exception {
sessionFactory().getStatistics().clear();
Session s = openSession();
Transaction t = s.beginTransaction();
Item i1 = new Item("foo");
Item i2 = new Item("bar");
s.persist("Item", i1);
s.persist("Item", i2);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
ScrollableResults sr = s.getNamedQuery("Item.nameDesc").scroll();
assertTrue( sr.next() );
i1 = (Item) sr.get(0);
assertTrue( sr.next() );
i2 = (Item) sr.get(0);
assertTrue( Hibernate.isInitialized(i1) );
assertTrue( Hibernate.isInitialized(i2) );
assertEquals( i1.getName(), "foo" );
assertEquals( i2.getName(), "bar" );
assertFalse( sr.next() );
s.delete(i1);
s.delete(i2);
t.commit();
s.close();
assertEquals( sessionFactory().getStatistics().getEntityFetchCount(), 0 );
}
}