From e791f77122bb8dccd3a8efc1090a744a12c1ce43 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 23 Dec 2016 17:36:47 -0800 Subject: [PATCH] HHH-9864 HHH-11357 : Fixes required to backport; improvement to tests --- .../org/hibernate/engine/spi/ActionQueue.java | 16 ++--- .../InsertOrderingStatementInspector.java | 43 ++++++++++++ ...rtOrderingWithBidirectionalManyToMany.java | 48 ++++++------- ...ertOrderingWithBidirectionalOneToMany.java | 46 ++++++------- ...ertOrderingWithJoinedTableInheritance.java | 51 ++++++++------ ...gWithJoinedTableMultiLevelInheritance.java | 67 +++++++++++++------ ...ertOrderingWithSingleTableInheritance.java | 47 +++++++------ ...tOrderingWithTablePerClassInheritance.java | 47 +++++++------ 8 files changed, 221 insertions(+), 144 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingStatementInspector.java diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java index da9b37658d..84008b1c08 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/ActionQueue.java @@ -1016,9 +1016,9 @@ public class ActionQueue { private final String entityName; - private Set parentEntityNames = new HashSet<>( ); + private Set parentEntityNames = new HashSet( ); - private Set childEntityNames = new HashSet<>( ); + private Set childEntityNames = new HashSet( ); public BatchIdentifier( String entityName) { @@ -1034,12 +1034,12 @@ public class ActionQueue { return false; } BatchIdentifier that = (BatchIdentifier) o; - return Objects.equals( entityName, that.entityName ); + return entityName.equals( that.entityName ); } @Override public int hashCode() { - return Objects.hash( entityName ); + return entityName.hashCode(); } public String getEntityName() { @@ -1071,9 +1071,9 @@ public class ActionQueue { */ public void sort(List insertions) { // optimize the hash size to eliminate a rehash. - this.latestBatches = new ArrayList<>( ); - this.entityBatchIdentifier = new HashMap<>( insertions.size() + 1, 1.0f ); - this.actionBatches = new HashMap<>(); + this.latestBatches = new ArrayList( ); + this.entityBatchIdentifier = new HashMap( insertions.size() + 1, 1.0f ); + this.actionBatches = new HashMap>(); for ( AbstractEntityInsertAction action : insertions ) { BatchIdentifier batchIdentifier = new BatchIdentifier( action.getEntityName() ); @@ -1156,7 +1156,7 @@ public class ActionQueue { List actions = actionBatches.get( batchIdentifier ); if ( actions == null ) { - actions = new LinkedList<>(); + actions = new LinkedList(); actionBatches.put( batchIdentifier, actions ); } actions.add( action ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingStatementInspector.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingStatementInspector.java new file mode 100644 index 0000000000..67a55de814 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingStatementInspector.java @@ -0,0 +1,43 @@ +/* + * 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 . + */ +package org.hibernate.test.insertordering; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.hibernate.resource.jdbc.spi.StatementInspector; + +/** + * @author Gail Badner + */ +class InsertOrderingStatementInspector implements StatementInspector { + private Map countBySqlStrings = new HashMap(); + + @Override + public String inspect(String sql) { + Integer count = countBySqlStrings.get( sql ); + if ( count == null ) { + count = 0; + } + countBySqlStrings.put( sql, count + 1 ); + return sql; + } + + int getCount(String sql) { + Integer count = countBySqlStrings.get( sql ); + return count == null ? 0 : count; + } + + void clear() { + countBySqlStrings.clear(); + } + + Map getCountBySqlStrings() { + return Collections.unmodifiableMap( countBySqlStrings ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalManyToMany.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalManyToMany.java index 2b51659a79..81a5753708 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalManyToMany.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalManyToMany.java @@ -6,7 +6,6 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -20,17 +19,14 @@ import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.SequenceGenerator; +import org.hibernate.Session; import org.hibernate.cfg.Environment; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.junit.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; /** * @author Vlad Mihalcea @@ -38,8 +34,7 @@ import static org.mockito.Mockito.verify; @TestForIssue(jiraKey = "HHH-9864") public class InsertOrderingWithBidirectionalManyToMany extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); + private InsertOrderingStatementInspector statementInspector = new InsertOrderingStatementInspector(); @Override protected Class[] getAnnotatedClasses() { @@ -50,21 +45,18 @@ public class InsertOrderingWithBidirectionalManyToMany protected void addSettings(Map settings) { settings.put( Environment.ORDER_INSERTS, "true" ); settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - settings.put( - org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, - connectionProvider - ); - } - - @Override - public void releaseResources() { - super.releaseResources(); - connectionProvider.stop(); + settings.put( Environment.GENERATE_STATISTICS, "true" ); + settings.put( Environment.STATEMENT_INSPECTOR, statementInspector ); } @Test public void testBatching() throws SQLException { - doInHibernate( this::sessionFactory, session -> { + sessionFactory().getStatistics().clear(); + statementInspector.clear(); + + Session session = openSession(); + session.getTransaction().begin(); + Person father = new Person(); Person mother = new Person(); Person son = new Person(); @@ -84,16 +76,14 @@ public class InsertOrderingWithBidirectionalManyToMany session.persist( home ); session.persist( office ); - connectionProvider.clear(); - } ); + session.getTransaction().commit(); + session.close(); - assertEquals( 3, connectionProvider.getPreparedStatements().size() ); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (ID) values (?)" ); - verify( personPreparedStatement, times( 4 ) ).addBatch(); + assertEquals( 1, statementInspector.getCount( "insert into Address (ID) values (?)" ) ); + assertEquals( 2, sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() ).getInsertCount() ); + + assertEquals( 1, statementInspector.getCount( "insert into Person (ID) values (?)" ) ); + assertEquals( 4, sessionFactory().getStatistics().getEntityStatistics( Person.class.getName() ).getInsertCount() ); } @Entity(name = "Address") @@ -105,7 +95,7 @@ public class InsertOrderingWithBidirectionalManyToMany private Long id; @ManyToMany(mappedBy = "addresses", cascade = CascadeType.PERSIST) - private List persons = new ArrayList<>(); + private List persons = new ArrayList(); public void addPerson(Person person) { persons.add( person ); @@ -122,6 +112,6 @@ public class InsertOrderingWithBidirectionalManyToMany private Long id; @ManyToMany - private List
addresses = new ArrayList<>(); + private List
addresses = new ArrayList
(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToMany.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToMany.java index 3e8f9fe726..769b2ba2e6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToMany.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToMany.java @@ -6,7 +6,6 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -21,16 +20,14 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; +import org.hibernate.Session; import org.hibernate.cfg.Environment; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.junit.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.junit.Assert.assertEquals; /** * @author Vlad Mihalcea @@ -38,8 +35,7 @@ import static org.mockito.Mockito.verify; @TestForIssue(jiraKey = "HHH-9864") public class InsertOrderingWithBidirectionalOneToMany extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); + private InsertOrderingStatementInspector statementInspector = new InsertOrderingStatementInspector(); @Override protected Class[] getAnnotatedClasses() { @@ -50,21 +46,18 @@ public class InsertOrderingWithBidirectionalOneToMany protected void addSettings(Map settings) { settings.put( Environment.ORDER_INSERTS, "true" ); settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - settings.put( - org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, - connectionProvider - ); - } - - @Override - public void releaseResources() { - super.releaseResources(); - connectionProvider.stop(); + settings.put( Environment.GENERATE_STATISTICS, "true" ); + settings.put( Environment.STATEMENT_INSPECTOR, statementInspector ); } @Test public void testBatching() throws SQLException { - doInHibernate( this::sessionFactory, session -> { + sessionFactory().getStatistics().clear(); + statementInspector.clear(); + + Session session = openSession(); + session.getTransaction().begin(); + Person father = new Person(); Person mother = new Person(); Person son = new Person(); @@ -84,15 +77,14 @@ public class InsertOrderingWithBidirectionalOneToMany session.persist( home ); session.persist( office ); - connectionProvider.clear(); - } ); + session.getTransaction().commit(); + session.close(); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (address_ID, ID) values (?, ?)" ); - verify( personPreparedStatement, times( 4 ) ).addBatch(); + assertEquals( 1, statementInspector.getCount( "insert into Address (ID) values (?)" ) ); + assertEquals( 2, sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() ).getInsertCount() ); + + assertEquals( 1, statementInspector.getCount( "insert into Person (address_ID, ID) values (?, ?)" ) ); + assertEquals( 4, sessionFactory().getStatistics().getEntityStatistics( Person.class.getName() ).getInsertCount() ); } @Entity(name = "Address") @@ -104,7 +96,7 @@ public class InsertOrderingWithBidirectionalOneToMany private Long id; @OneToMany(mappedBy = "address", cascade = CascadeType.PERSIST) - private List persons = new ArrayList<>(); + private List persons = new ArrayList(); public void addPerson(Person person) { persons.add( person ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableInheritance.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableInheritance.java index aafd95d680..c3b64512d1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableInheritance.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableInheritance.java @@ -27,15 +27,14 @@ import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; +import org.hibernate.Session; import org.hibernate.annotations.BatchSize; import org.hibernate.cfg.Environment; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.junit.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; /** @@ -44,8 +43,7 @@ import static org.junit.Assert.assertEquals; @TestForIssue(jiraKey = "HHH-9864") public class InsertOrderingWithJoinedTableInheritance extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); + private InsertOrderingStatementInspector statementInspector = new InsertOrderingStatementInspector(); @Override protected Class[] getAnnotatedClasses() { @@ -56,21 +54,15 @@ public class InsertOrderingWithJoinedTableInheritance protected void addSettings(Map settings) { settings.put( Environment.ORDER_INSERTS, "true" ); settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - settings.put( - org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, - connectionProvider - ); - } - - @Override - public void releaseResources() { - super.releaseResources(); - connectionProvider.stop(); + settings.put( Environment.GENERATE_STATISTICS, "true" ); + settings.put( Environment.STATEMENT_INSPECTOR, statementInspector ); } @Test public void testBatchOrdering() { - doInHibernate( this::sessionFactory, session -> { + Session session = openSession(); + session.getTransaction().begin(); + final Person person = new Person(); person.addAddress( new Address() ); session.persist( person ); @@ -79,12 +71,19 @@ public class InsertOrderingWithJoinedTableInheritance final SpecialPerson specialPerson = new SpecialPerson(); specialPerson.addAddress( new Address() ); session.persist( specialPerson ); - } ); + + session.getTransaction().commit(); + session.close(); } @Test public void testBatchingAmongstSubClasses() { - doInHibernate( this::sessionFactory, session -> { + sessionFactory().getStatistics().clear(); + statementInspector.clear(); + + Session session = openSession(); + session.getTransaction().begin(); + int iterations = 12; for ( int i = 0; i < iterations; i++ ) { final Person person = new Person(); @@ -95,10 +94,22 @@ public class InsertOrderingWithJoinedTableInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); - } ); - assertEquals( 26, connectionProvider.getPreparedStatements().size() ); + session.getTransaction().commit(); + session.close(); + + assertEquals( 1, statementInspector.getCount( "insert into ADDRESS (PERSONID, ID) values (?, ?)" ) ); + assertEquals( + 24, + sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() ).getInsertCount() + ); + + assertEquals( 1, statementInspector.getCount( "insert into PERSON (CLASSINDICATOR, ID) values (1, ?)" ) ); + assertEquals( 12, sessionFactory().getStatistics().getEntityStatistics( Person.class.getName() ).getInsertCount() ); + + assertEquals( 12, statementInspector.getCount( "insert into PERSON (CLASSINDICATOR, ID) values (2, ?)" ) ); + assertEquals( 12, statementInspector.getCount( "insert into SpecialPerson (special, ID) values (?, ?)" ) ); + assertEquals( 12, sessionFactory().getStatistics().getEntityStatistics( SpecialPerson.class.getName() ).getInsertCount() ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableMultiLevelInheritance.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableMultiLevelInheritance.java index a238e8b590..916a26341c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableMultiLevelInheritance.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithJoinedTableMultiLevelInheritance.java @@ -14,6 +14,7 @@ import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorType; +import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -26,15 +27,14 @@ import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; +import org.hibernate.Session; import org.hibernate.annotations.BatchSize; import org.hibernate.cfg.Environment; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.junit.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; /** @@ -43,8 +43,7 @@ import static org.junit.Assert.assertEquals; @TestForIssue(jiraKey = "HHH-9864") public class InsertOrderingWithJoinedTableMultiLevelInheritance extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); + private InsertOrderingStatementInspector statementInspector = new InsertOrderingStatementInspector(); @Override protected Class[] getAnnotatedClasses() { @@ -62,21 +61,18 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance protected void addSettings(Map settings) { settings.put( Environment.ORDER_INSERTS, "true" ); settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - settings.put( - org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, - connectionProvider - ); - } - - @Override - public void releaseResources() { - super.releaseResources(); - connectionProvider.stop(); + settings.put( Environment.GENERATE_STATISTICS, "true" ); + settings.put( Environment.STATEMENT_INSPECTOR, statementInspector ); } @Test public void testBatchingAmongstSubClasses() { - doInHibernate( this::sessionFactory, session -> { + sessionFactory().getStatistics().clear(); + statementInspector.clear(); + + Session session = openSession(); + session.getTransaction().begin(); + int iterations = 2; for ( int i = 0; i < iterations; i++ ) { final President president = new President(); @@ -96,10 +92,33 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); - } ); - assertEquals( 17, connectionProvider.getPreparedStatements().size() ); + session.getTransaction().commit(); + session.close(); + + assertEquals( 1, statementInspector.getCount( "insert into ADDRESS (PERSONID, ID) values (?, ?)" ) ); + assertEquals( + 4, + sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() ).getInsertCount() + ); + + assertEquals( 1, statementInspector.getCount( "insert into Office (ID) values (?)" ) ); + assertEquals( 2, sessionFactory().getStatistics().getEntityStatistics( Office.class.getName() ).getInsertCount() ); + + assertEquals( 1, statementInspector.getCount( "insert into PERSON (CLASSINDICATOR, ID) values (1, ?)" ) ); + assertEquals( 2, sessionFactory().getStatistics().getEntityStatistics( Person.class.getName() ).getInsertCount() ); + + assertEquals( 2, statementInspector.getCount( "insert into PERSON (CLASSINDICATOR, ID) values (2, ?)" ) ); + assertEquals( 4, statementInspector.getCount( "insert into SpecialPerson (special, ID) values (?, ?)" ) ); + assertEquals( 2, sessionFactory().getStatistics().getEntityStatistics( SpecialPerson.class.getName() ).getInsertCount() ); + + assertEquals( 2, statementInspector.getCount( "insert into PERSON (CLASSINDICATOR, ID) values (3, ?)" ) ); + assertEquals( 2, statementInspector.getCount( "insert into AnotherPerson (office_ID, working, ID) values (?, ?, ?)" ) ); + assertEquals( 2, sessionFactory().getStatistics().getEntityStatistics( AnotherPerson.class.getName() ).getInsertCount() ); + + assertEquals( 2, statementInspector.getCount( "insert into PERSON (CLASSINDICATOR, ID) values (4, ?)" ) ); + assertEquals( 2, statementInspector.getCount( "insert into President (salary, ID) values (?, ?)" ) ); + assertEquals( 2, sessionFactory().getStatistics().getEntityStatistics( President.class.getName() ).getInsertCount() ); } @Override @@ -109,14 +128,18 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance @Override protected void cleanupTestData() throws Exception { - doInHibernate( this::sessionFactory, session -> { + Session session = openSession(); + session.getTransaction().begin(); + session.createQuery( "delete Address" ).executeUpdate(); session.createQuery( "delete Person" ).executeUpdate(); session.createQuery( "delete SpecialPerson" ).executeUpdate(); session.createQuery( "delete AnotherPerson" ).executeUpdate(); session.createQuery( "delete Office" ).executeUpdate(); session.createQuery( "delete President" ).executeUpdate(); - } ); + + session.getTransaction().commit(); + session.close(); } @Entity(name = "Address") @@ -142,6 +165,7 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance @Table(name = "PERSON") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "CLASSINDICATOR", discriminatorType = DiscriminatorType.INTEGER) + @DiscriminatorValue("1") public static class Person { @Id @Column(name = "ID", nullable = false) @@ -152,6 +176,7 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance } @Entity(name = "SpecialPerson") + @DiscriminatorValue("2") public static class SpecialPerson extends Person { @Column(name = "special") private String special; @@ -170,6 +195,7 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance } @Entity(name = "AnotherPerson") + @DiscriminatorValue("3") public static class AnotherPerson extends Person { private boolean working; @@ -178,6 +204,7 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance } @Entity(name = "President") + @DiscriminatorValue("4") public static class President extends SpecialPerson { @Column(name = "salary") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSingleTableInheritance.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSingleTableInheritance.java index 20b0e240cb..882a54b0e1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSingleTableInheritance.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSingleTableInheritance.java @@ -27,15 +27,14 @@ import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; +import org.hibernate.Session; import org.hibernate.annotations.BatchSize; import org.hibernate.cfg.Environment; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.junit.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; /** @@ -44,8 +43,7 @@ import static org.junit.Assert.assertEquals; @TestForIssue(jiraKey = "HHH-9864") public class InsertOrderingWithSingleTableInheritance extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); + private InsertOrderingStatementInspector statementInspector = new InsertOrderingStatementInspector(); @Override protected Class[] getAnnotatedClasses() { @@ -56,21 +54,15 @@ public class InsertOrderingWithSingleTableInheritance protected void addSettings(Map settings) { settings.put( Environment.ORDER_INSERTS, "true" ); settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - settings.put( - org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, - connectionProvider - ); - } - - @Override - public void releaseResources() { - super.releaseResources(); - connectionProvider.stop(); + settings.put( Environment.GENERATE_STATISTICS, "true" ); + settings.put( Environment.STATEMENT_INSPECTOR, statementInspector ); } @Test public void testBatchOrdering() { - doInHibernate( this::sessionFactory, session -> { + Session session = openSession(); + session.getTransaction().begin(); + // First object with dependent object (address) final Person person = new Person(); person.addAddress( new Address() ); @@ -80,12 +72,19 @@ public class InsertOrderingWithSingleTableInheritance final SpecialPerson specialPerson = new SpecialPerson(); specialPerson.addAddress( new Address() ); session.persist( specialPerson ); - } ); + + session.getTransaction().commit(); + session.close(); } @Test public void testBatchingAmongstSubClasses() { - doInHibernate( this::sessionFactory, session -> { + sessionFactory().getStatistics().clear(); + statementInspector.clear(); + + Session session = openSession(); + session.getTransaction().begin(); + int iterations = 12; for ( int i = 0; i < iterations; i++ ) { final Person person = new Person(); @@ -96,10 +95,18 @@ public class InsertOrderingWithSingleTableInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); - } ); - assertEquals( 3, connectionProvider.getPreparedStatements().size() ); + session.getTransaction().commit(); + session.close(); + + assertEquals( 1, statementInspector.getCount( "insert into ADDRESS (PERSONID, ID) values (?, ?)" ) ); + assertEquals( 24, sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() ).getInsertCount() ); + + assertEquals( 1, statementInspector.getCount( "insert into PERSON (CLASSINDICATOR, ID) values (1, ?)" ) ); + assertEquals( 12, sessionFactory().getStatistics().getEntityStatistics( Person.class.getName() ).getInsertCount() ); + + assertEquals( 1, statementInspector.getCount( "insert into PERSON (special, CLASSINDICATOR, ID) values (?, 2, ?)" ) ); + assertEquals( 12, sessionFactory().getStatistics().getEntityStatistics( SpecialPerson.class.getName() ).getInsertCount() ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithTablePerClassInheritance.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithTablePerClassInheritance.java index 73b4da879e..e64fadcb7e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithTablePerClassInheritance.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithTablePerClassInheritance.java @@ -27,15 +27,14 @@ import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; +import org.hibernate.Session; import org.hibernate.annotations.BatchSize; import org.hibernate.cfg.Environment; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; import org.junit.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; /** @@ -44,8 +43,7 @@ import static org.junit.Assert.assertEquals; @TestForIssue(jiraKey = "HHH-9864") public class InsertOrderingWithTablePerClassInheritance extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); + private InsertOrderingStatementInspector statementInspector = new InsertOrderingStatementInspector(); @Override protected Class[] getAnnotatedClasses() { @@ -56,21 +54,15 @@ public class InsertOrderingWithTablePerClassInheritance protected void addSettings(Map settings) { settings.put( Environment.ORDER_INSERTS, "true" ); settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - settings.put( - org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, - connectionProvider - ); - } - - @Override - public void releaseResources() { - super.releaseResources(); - connectionProvider.stop(); + settings.put( Environment.GENERATE_STATISTICS, "true" ); + settings.put( Environment.STATEMENT_INSPECTOR, statementInspector ); } @Test public void testBatchOrdering() { - doInHibernate( this::sessionFactory, session -> { + Session session = openSession(); + session.getTransaction().begin(); + // First object with dependent object (address) final Person person = new Person(); person.addAddress( new Address() ); @@ -80,12 +72,19 @@ public class InsertOrderingWithTablePerClassInheritance final SpecialPerson specialPerson = new SpecialPerson(); specialPerson.addAddress( new Address() ); session.persist( specialPerson ); - } ); + + session.getTransaction().commit(); + session.close(); } @Test public void testBatchingAmongstSubClasses() { - doInHibernate( this::sessionFactory, session -> { + sessionFactory().getStatistics().clear(); + statementInspector.clear(); + + Session session = openSession(); + session.getTransaction().begin(); + int iterations = 12; for ( int i = 0; i < iterations; i++ ) { final Person person = new Person(); @@ -96,10 +95,18 @@ public class InsertOrderingWithTablePerClassInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); - } ); - assertEquals( 3, connectionProvider.getPreparedStatements().size() ); + session.getTransaction().commit(); + session.close(); + + assertEquals( 1, statementInspector.getCount( "insert into ADDRESS (PERSONID, ID) values (?, ?)" ) ); + assertEquals( 24, sessionFactory().getStatistics().getEntityStatistics( Address.class.getName() ).getInsertCount() ); + + assertEquals( 1, statementInspector.getCount( "insert into PERSON (ID) values (?)" ) ); + assertEquals( 12, sessionFactory().getStatistics().getEntityStatistics( Person.class.getName() ).getInsertCount() ); + + assertEquals( 1, statementInspector.getCount( "insert into SpecialPerson (special, ID) values (?, ?)" ) ); + assertEquals( 12, sessionFactory().getStatistics().getEntityStatistics( SpecialPerson.class.getName() ).getInsertCount() ); } @Override