HHH-9864 HHH-11357 : Fixes required to backport; improvement to tests

This commit is contained in:
Gail Badner 2016-12-23 17:36:47 -08:00
parent 9500f92bc6
commit e791f77122
8 changed files with 221 additions and 144 deletions

View File

@ -1016,9 +1016,9 @@ public class ActionQueue {
private final String entityName;
private Set<String> parentEntityNames = new HashSet<>( );
private Set<String> parentEntityNames = new HashSet<String>( );
private Set<String> childEntityNames = new HashSet<>( );
private Set<String> childEntityNames = new HashSet<String>( );
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<AbstractEntityInsertAction> 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<BatchIdentifier>( );
this.entityBatchIdentifier = new HashMap<Object, BatchIdentifier>( insertions.size() + 1, 1.0f );
this.actionBatches = new HashMap<BatchIdentifier, List<AbstractEntityInsertAction>>();
for ( AbstractEntityInsertAction action : insertions ) {
BatchIdentifier batchIdentifier = new BatchIdentifier( action.getEntityName() );
@ -1156,7 +1156,7 @@ public class ActionQueue {
List<AbstractEntityInsertAction> actions = actionBatches.get( batchIdentifier );
if ( actions == null ) {
actions = new LinkedList<>();
actions = new LinkedList<AbstractEntityInsertAction>();
actionBatches.put( batchIdentifier, actions );
}
actions.add( action );

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String, Integer> countBySqlStrings = new HashMap<String, Integer>();
@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<String, Integer> getCountBySqlStrings() {
return Collections.unmodifiableMap( countBySqlStrings );
}
}

View File

@ -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<Person> persons = new ArrayList<>();
private List<Person> persons = new ArrayList<Person>();
public void addPerson(Person person) {
persons.add( person );
@ -122,6 +112,6 @@ public class InsertOrderingWithBidirectionalManyToMany
private Long id;
@ManyToMany
private List<Address> addresses = new ArrayList<>();
private List<Address> addresses = new ArrayList<Address>();
}
}

View File

@ -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<Person> persons = new ArrayList<>();
private List<Person> persons = new ArrayList<Person>();
public void addPerson(Person person) {
persons.add( person );

View File

@ -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

View File

@ -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")

View File

@ -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

View File

@ -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