From 5eedda9a467fef44d924f64203023b2345b8415f Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 2 Oct 2020 09:42:36 +0100 Subject: [PATCH 1/5] HHH-14247 Fix automatic releease script, changelog.txt contains wrong Jira release url --- release/release.gradle | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/release/release.gradle b/release/release.gradle index 9a59f4853a..8b6304abc3 100644 --- a/release/release.gradle +++ b/release/release.gradle @@ -299,8 +299,6 @@ task changeLogFile( dependsOn: [releaseChecks] ) { } } - - task addVersionCommit( dependsOn: [changeLogFile] ) { group = "Release" description = "Adds a commit for the released version and push the changes to github" @@ -413,15 +411,14 @@ class ChangeLogFile { } def apiString = "https://hibernate.atlassian.net/rest/api/2/search/?jql=project=HHH%20AND%20fixVersion=${restReleaseVersion}%20order%20by%20issuetype%20ASC" def apiUrl = new URL( apiString ) - def releseNotes = new JsonSlurper().parse( apiUrl ) - - + def jsonReleaseNotes = new JsonSlurper().parse( apiUrl ) def releaseDate = new Date().format( 'MMMM dd, YYYY' ) - def versionId = releseNotes.issues.get( 0 ).fields.fixVersions.get( 0 ).id + def versionId = getVersionId( jsonReleaseNotes, restReleaseVersion ) + ReleaseNote releaseNotes = new ReleaseNote( releaseVersion, releaseDate, versionId ) def issuetype - releseNotes.issues.each { + jsonReleaseNotes.issues.each { issue -> if ( issuetype != issue.fields.issuetype.name ) { issuetype = issue.fields.issuetype.name @@ -433,6 +430,17 @@ class ChangeLogFile { releaseNotes.addEmptyLine() return releaseNotes.notes } + + private static getVersionId(jsonReleaseNotes, String restReleaseVersion) { + def fixVersions = jsonReleaseNotes.issues.get( 0 ).fields.fixVersions + + for ( def fixVersion : fixVersions ) { + if ( fixVersion.name.equals( restReleaseVersion ) ) { + return fixVersion.id + } + } + throw new GradleException( "Unable to determine the version id of the current release." ) + } } class ReleaseNote { From de6736ba3217c3965121f43ee4fdffab5afc7c1f Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Mon, 5 Oct 2020 10:57:58 +0100 Subject: [PATCH 2/5] HHH-14227 Insert statements are not ordered with entities that use inheritance and reference a subclass --- .../src/main/java/org/hibernate/engine/spi/ActionQueue.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 67099bdc66..9bfacb2151 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 @@ -1268,7 +1268,9 @@ public class ActionQueue { for ( int i = 0; i < propertyValues.length; i++ ) { Object value = propertyValues[i]; Type type = propertyTypes[i]; - addParentChildEntityNameByPropertyAndValue( action, batchIdentifier, type, value ); + if ( value != null ) { + addParentChildEntityNameByPropertyAndValue( action, batchIdentifier, type, value ); + } } if ( identifierType.isComponentType() ) { From 9de5e3e0056d44e624a0b2428a2e207ce09e2949 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Mon, 5 Oct 2020 10:58:33 +0100 Subject: [PATCH 3/5] HHH-14227 Some additional typos and message improvements --- .../src/main/java/org/hibernate/engine/spi/ActionQueue.java | 2 +- .../util/jdbc/PreparedStatementSpyConnectionProvider.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 9bfacb2151..89759e5515 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 @@ -1101,7 +1101,7 @@ public class ActionQueue { } /** - * Check if the this {@link BatchIdentifier} has a parent or grand parent + * Check if this {@link BatchIdentifier} has a parent or grand parent * matching the given {@link BatchIdentifier} reference. * * @param batchIdentifier {@link BatchIdentifier} reference diff --git a/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java b/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java index 455404356b..69ee14569c 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java +++ b/hibernate-core/src/test/java/org/hibernate/test/util/jdbc/PreparedStatementSpyConnectionProvider.java @@ -157,11 +157,11 @@ public class PreparedStatementSpyConnectionProvider extends ConnectionProviderDe List preparedStatements = getPreparedStatements( sql ); if ( preparedStatements.isEmpty() ) { throw new IllegalArgumentException( - "There is no PreparedStatement for this SQL statement " + sql ); + "There is no PreparedStatement for this SQL statement: " + sql ); } else if ( preparedStatements.size() > 1 ) { throw new IllegalArgumentException( "There are " + preparedStatements - .size() + " PreparedStatements for this SQL statement " + sql ); + .size() + " PreparedStatements for this SQL statement: " + sql ); } return preparedStatements.get( 0 ); } From d045797aa7307247d0f964198206de55d063e3fa Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Mon, 5 Oct 2020 11:00:15 +0100 Subject: [PATCH 4/5] HHH-14227 Add some more tests to cover order of batched insert executions --- .../BaseInsertOrderingTest.java | 72 ++++++++++ .../insertordering}/BatchSortingTest.java | 38 ++--- .../insertordering/ElementCollectionTest.java | 22 +-- .../InsertOrderingDuplicateTest.java | 38 ++--- .../InsertOrderingHasParentTest.java | 22 +-- ...rderingReferenceDifferentSubclassTest.java | 84 +++++++++++ ...ngSelfReferenceSingleTableInheritance.java | 5 - .../InsertOrderingSelfReferenceTest.java | 131 ++++++++++++++++++ ...eringWithBaseClassReferencingSubclass.java | 1 - ...rtOrderingWithBidirectionalManyToMany.java | 49 ++----- ...deringWithBidirectionalMapsIdOneToOne.java | 50 ++----- ...ertOrderingWithBidirectionalOneToMany.java | 50 ++----- ...ithBidirectionalOneToManyFlushProblem.java | 43 ++++-- ...sertOrderingWithBidirectionalOneToOne.java | 60 +++----- ...WithBidirectionalOneToOneFlushProblem.java | 52 ++++--- ...ertOrderingWithJoinedTableInheritance.java | 34 +---- ...gWithJoinedTableMultiLevelInheritance.java | 34 +---- .../InsertOrderingWithManyToOne.java | 54 ++------ .../InsertOrderingWithMultipleManyToOne.java | 62 ++------- .../InsertOrderingWithSecondaryTable.java | 35 ++--- ...ertOrderingWithSingleTableInheritance.java | 34 +---- ...tOrderingWithTablePerClassInheritance.java | 34 +---- ...ertOrderingWithUnidirectionalOneToOne.java | 50 ++----- ...gWithUnidirectionalOneToOneJoinColumn.java | 37 +++-- 24 files changed, 537 insertions(+), 554 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/insertordering/BaseInsertOrderingTest.java rename hibernate-core/src/test/java/org/hibernate/{engine/spi => test/insertordering}/BatchSortingTest.java (88%) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingReferenceDifferentSubclassTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/BaseInsertOrderingTest.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/BaseInsertOrderingTest.java new file mode 100644 index 0000000000..f2882b17e2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/BaseInsertOrderingTest.java @@ -0,0 +1,72 @@ +package org.hibernate.test.insertordering; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Map; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.cfg.Environment; + +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * @author Nathan Xu + */ +abstract class BaseInsertOrderingTest extends BaseNonConfigCoreFunctionalTestCase { + + static class Batch { + String sql; + int size; + + Batch(String sql, int size) { + this.sql = sql; + this.size = size; + } + + Batch(String sql) { + this( sql, 1 ); + } + } + + private final PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); + + @Override + protected void addSettings(Map settings) { + settings.put( Environment.ORDER_INSERTS, "true" ); + settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); + settings.put( AvailableSettings.CONNECTION_PROVIDER, connectionProvider ); + } + + @Override + public void releaseResources() { + super.releaseResources(); + connectionProvider.stop(); + } + + void verifyContainsBatches(Batch... expectedBatches) { + for ( Batch expectedBatch : expectedBatches ) { + PreparedStatement preparedStatement = connectionProvider.getPreparedStatement( expectedBatch.sql ); + try { + verify( preparedStatement, times( expectedBatch.size ) ).addBatch(); + verify( preparedStatement, times( 1 ) ).executeBatch(); + } catch (SQLException e) { + throw new RuntimeException( e ); + } + } + } + + void verifyPreparedStatementCount(int expectedBatchCount) { + final int realBatchCount = connectionProvider.getPreparedSQLStatements().size(); + assertEquals( String.format( "expected %d batch%s; but found %d batch%s", expectedBatchCount, (expectedBatchCount == 1 ? "" : "es"), realBatchCount, (realBatchCount == 1 ? "" : "es" ) ), + expectedBatchCount, realBatchCount ); + } + + void clearBatches() { + connectionProvider.clear(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/engine/spi/BatchSortingTest.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/BatchSortingTest.java similarity index 88% rename from hibernate-core/src/test/java/org/hibernate/engine/spi/BatchSortingTest.java rename to hibernate-core/src/test/java/org/hibernate/test/insertordering/BatchSortingTest.java index 74f4836561..a1e4b3492c 100644 --- a/hibernate-core/src/test/java/org/hibernate/engine/spi/BatchSortingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/BatchSortingTest.java @@ -4,33 +4,32 @@ * 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.engine.spi; +package org.hibernate.test.insertordering; -import org.hibernate.cfg.Environment; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.jta.TestingJtaBootstrap; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; - -import javax.persistence.*; import java.util.ArrayList; import java.util.List; -import java.util.Map; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; + +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; /* * @author gajendra.jatav(raaz2.gajendra@gmail.com) */ -public class BatchSortingTest extends BaseNonConfigCoreFunctionalTestCase { - - @Override - protected void addSettings(Map settings) { - settings.put( Environment.ORDER_INSERTS, "true" ); - settings.put( Environment.ORDER_UPDATES, "true" ); - settings.put( Environment.STATEMENT_BATCH_SIZE, "5" ); - TestingJtaBootstrap.prepare( settings ); - } +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class BatchSortingTest extends BaseInsertOrderingTest { protected Class[] getAnnotatedClasses() { return new Class[]{ @@ -59,7 +58,10 @@ public class BatchSortingTest extends BaseNonConfigCoreFunctionalTestCase { country.setDistricts( geoDistricts ); session.persist( geoDistrict ); session.persist( nation ); + clearBatches(); }); + + verifyPreparedStatementCount( 4 ); } @Entity(name = "GeoCountry") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/ElementCollectionTest.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/ElementCollectionTest.java index 1f1cdd755b..90284b3957 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/ElementCollectionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/ElementCollectionTest.java @@ -7,7 +7,6 @@ package org.hibernate.test.insertordering; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.CollectionTable; import javax.persistence.ElementCollection; @@ -21,10 +20,9 @@ import javax.persistence.JoinColumn; import javax.persistence.SequenceGenerator; import javax.persistence.Table; -import org.hibernate.cfg.Environment; - +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; @@ -33,19 +31,14 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; * @author Andrea Boriero */ @TestForIssue(jiraKey = "HHH-11216") -public class ElementCollectionTest extends BaseNonConfigCoreFunctionalTestCase { +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class ElementCollectionTest extends BaseInsertOrderingTest { @Override protected Class[] getAnnotatedClasses() { return new Class[] {Task.class}; } - @Override - protected void addSettings(Map settings) { - settings.put( Environment.ORDER_INSERTS, "true" ); - settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - } - @Test public void testBatchOrdering() { doInHibernate( this::sessionFactory, session -> { @@ -56,7 +49,14 @@ public class ElementCollectionTest extends BaseNonConfigCoreFunctionalTestCase { Task task1 = new Task(); task1.addCategory(Category.A); session.persist( task1 ); + + clearBatches(); } ); + + verifyContainsBatches( + new Batch( "insert into TASK (id) values (?)", 2 ), + new Batch( "insert into TASK_CATEGORY (TASK_ID, categories) values (?, ?)", 2 ) + ); } @Entity diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingDuplicateTest.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingDuplicateTest.java index 7c471f0b9b..4aa2606870 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingDuplicateTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingDuplicateTest.java @@ -7,9 +7,7 @@ package org.hibernate.test.insertordering; import java.math.BigDecimal; -import java.sql.SQLException; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; @@ -20,22 +18,19 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; -import org.hibernate.cfg.Environment; - +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.verify; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-11634") -public class InsertOrderingDuplicateTest - extends BaseNonConfigCoreFunctionalTestCase { +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingDuplicateTest extends BaseInsertOrderingTest { @Override protected Class[] getAnnotatedClasses() { @@ -47,19 +42,8 @@ public class InsertOrderingDuplicateTest }; } - @Override - protected void addSettings(Map settings) { - settings.put( Environment.ORDER_INSERTS, "true" ); - settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); - } - - @Override - public void releaseResources() { - super.releaseResources(); - } - @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { SaleDocumentItem saleDocumentItem = new SaleDocumentItem(); @@ -82,9 +66,17 @@ public class InsertOrderingDuplicateTest session.persist(correction); saleDocument.setCorerctionsubject( correction); - } ); - } + clearBatches(); + } ); + + verifyContainsBatches( + new Batch( "insert into SaleDocumentSummary (sale_number, totalPrice, id) values (?, ?, ?)" ), + new Batch( "insert into Product (description, name, price, quantity, id) values (?, ?, ?, ?, ?)" ), + new Batch( "insert into SaleDocument (ID_SALE_DOCUMENT_CORRECTION, sale_number, totalPrice, id) values (?, ?, ?, ?)", 2 ), + new Batch( "insert into SaleDocumentItem (lp, product_id, quantity, ID_SALE_DOCUMENT, ID_SALE_DOCUMENT_SUMAMRY, id) values (?, ?, ?, ?, ?, ?)" ) + ); + } @Entity(name = "Product") public static class Product { diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingHasParentTest.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingHasParentTest.java index 6e2b1dab0d..8900e86aad 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingHasParentTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingHasParentTest.java @@ -9,7 +9,6 @@ package org.hibernate.test.insertordering; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -19,10 +18,9 @@ import javax.persistence.ManyToMany; import javax.persistence.OneToOne; import javax.persistence.Table; -import org.hibernate.cfg.Environment; - +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; @@ -31,18 +29,14 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; * @author Andrea Boriero */ @TestForIssue(jiraKey = "HHH-12380") -public class InsertOrderingHasParentTest extends BaseNonConfigCoreFunctionalTestCase { +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingHasParentTest extends BaseInsertOrderingTest { @Override protected Class[] getAnnotatedClasses() { return new Class[] { Author.class, Book.class, Comment.class }; } - @Override - protected void addSettings(Map settings) { - settings.put( Environment.ORDER_INSERTS, "true" ); - } - @Test public void testInsert() { @@ -55,8 +49,16 @@ public class InsertOrderingHasParentTest extends BaseNonConfigCoreFunctionalTest author.setBook( book ); session.persist( author ); + + clearBatches(); } ); + verifyContainsBatches( + new Batch( "insert into book_comment (book_comment, id) values (?, ?)", 2 ), + new Batch( "insert into Book (comment_id, id) values (?, ?)" ), + new Batch( "insert into Author (book_id, id) values (?, ?)" ), + new Batch( "insert into Book_book_comment (Book_id, comments_id) values (?, ?)" ) + ); } @Entity(name = "Author") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingReferenceDifferentSubclassTest.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingReferenceDifferentSubclassTest.java new file mode 100644 index 0000000000..1231fe76cf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingReferenceDifferentSubclassTest.java @@ -0,0 +1,84 @@ +package org.hibernate.test.insertordering; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.MappedSuperclass; +import javax.persistence.OneToOne; + +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +/** + * @author Normunds Gavars + * @author Nathan Xu + */ +@TestForIssue( jiraKey = "HHH-14227" ) +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingReferenceDifferentSubclassTest extends BaseInsertOrderingTest { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + SubclassA.class, + SubclassB.class + }; + } + + @Test + public void testReferenceDifferentSubclass() { + doInHibernate(this::sessionFactory, session -> { + SubclassA subclassA1 = new SubclassA(); + SubclassB subclassB1 = new SubclassB(); + + SubclassA subclassA2 = new SubclassA(); + SubclassB subclassB2 = new SubclassB(); + + subclassA1.referenceB = subclassB2; + subclassB2.referenceA = subclassA2; + + subclassA2.referenceB = subclassB1; + + session.save( subclassA1 ); + session.save( subclassA2 ); + + clearBatches(); + }); + + verifyContainsBatches( + new Batch( "insert into SubclassB (referenceA_id, id) values (?, ?)", 2 ), + new Batch( "insert into SubclassA (referenceB_id, id) values (?, ?)", 2 ) + ); + } + + @MappedSuperclass + static class BaseClass { + + @Id @GeneratedValue + Long id; + + } + + @Entity(name = "SubclassA") + static class SubclassA extends BaseClass { + + @OneToOne(cascade = CascadeType.ALL) + SubclassB referenceB; + + } + + @Entity(name = "SubclassB") + static class SubclassB extends BaseClass { + + @ManyToOne(fetch = FetchType.LAZY) + SubclassA referenceA; + + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceSingleTableInheritance.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceSingleTableInheritance.java index 474a098447..d4b748c07e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceSingleTableInheritance.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceSingleTableInheritance.java @@ -19,17 +19,12 @@ import javax.persistence.OneToOne; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; -import org.hibernate.annotations.GenericGenerator; -import org.hibernate.annotations.Parameter; import org.hibernate.cfg.Environment; -import org.hibernate.id.enhanced.SequenceStyleGenerator; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceTest.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceTest.java new file mode 100644 index 0000000000..03d92a97a3 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingSelfReferenceTest.java @@ -0,0 +1,131 @@ +package org.hibernate.test.insertordering; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.CascadeType; +import javax.persistence.DiscriminatorColumn; +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.ManyToOne; +import javax.persistence.MappedSuperclass; +import javax.persistence.OneToMany; + +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; +import org.hibernate.annotations.Fetch; +import org.hibernate.annotations.FetchMode; +import org.hibernate.annotations.SortNatural; +import org.hibernate.annotations.Where; + +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +/** + * @author Normunds Gavars + * @author Nathan Xu + */ +@TestForIssue( jiraKey = "HHH-14227" ) +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingSelfReferenceTest extends BaseInsertOrderingTest { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Parameter.class, + InputParameter.class, + OutputParameter.class, + Placeholder.class, + }; + } + + @Test + public void testReferenceItself() { + doInHibernate( this::sessionFactory, session -> { + Placeholder placeholder = new Placeholder(); + session.save( placeholder ); + + OutputParameter outputParameter1 = new OutputParameter(); + + OutputParameter childOutputParameter = new OutputParameter(); + outputParameter1.children.add( childOutputParameter ); + childOutputParameter.parent = outputParameter1; + + session.save( outputParameter1 ); + + Placeholder placeholder2 = new Placeholder(); + session.save( placeholder2 ); + + InputParameter inputParameter = new InputParameter(); + session.save( inputParameter ); + + OutputParameter outputParameter2 = new OutputParameter(); + session.save( outputParameter2 ); + + clearBatches(); + } ); + + verifyContainsBatches( + new Batch( "insert into Placeholder (id) values (?)", 2 ), + new Batch( "insert into Parameter (parent_id, TYPE, id) values (?, 'INPUT', ?)" ), + new Batch( "insert into Parameter (parent_id, TYPE, id) values (?, 'OUTPUT', ?)", 3 ) + ); + } + + @MappedSuperclass + static class AbstractEntity { + + @Id + @GeneratedValue + Long id; + + } + + @Entity(name = "Placeholder") + static class Placeholder extends AbstractEntity { + } + + @Entity(name = "Parameter") + @DiscriminatorColumn(name = "TYPE") + @Inheritance(strategy = InheritanceType.SINGLE_TABLE) + static abstract class Parameter extends AbstractEntity { + } + + @Entity(name = "InputParameter") + @DiscriminatorValue("INPUT") + static class InputParameter extends Parameter { + + @ManyToOne(fetch = FetchType.LAZY) + InputParameter parent; + + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent") + @SortNatural + @Where(clause = "TYPE = 'INPUT'") + @Fetch(FetchMode.SUBSELECT) + List children = new ArrayList<>(); + } + + @Entity(name = "OutputParameter") + @DiscriminatorValue("OUTPUT") + static class OutputParameter extends Parameter { + + @ManyToOne(fetch = FetchType.LAZY) + OutputParameter parent; + + @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent") + @SortNatural + @Where(clause = "TYPE = 'OUTPUT'") + @Fetch(FetchMode.SUBSELECT) + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) + List children = new ArrayList<>(); + + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBaseClassReferencingSubclass.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBaseClassReferencingSubclass.java index f17796f987..81b0a8dbdd 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBaseClassReferencingSubclass.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBaseClassReferencingSubclass.java @@ -19,7 +19,6 @@ 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; 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 f46e17cb73..8dd142a0dc 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,11 +6,8 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -20,53 +17,27 @@ import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.SequenceGenerator; -import org.hibernate.cfg.Environment; - import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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 */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithBidirectionalManyToMany - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); +public class InsertOrderingWithBidirectionalManyToMany extends BaseInsertOrderingTest { @Override protected Class[] getAnnotatedClasses() { return new Class[] { Address.class, Person.class }; } - @Override - 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(); - } - @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { Person father = new Person(); Person mother = new Person(); @@ -87,18 +58,14 @@ public class InsertOrderingWithBidirectionalManyToMany session.persist( home ); session.persist( office ); - connectionProvider.clear(); + clearBatches(); } ); - assertEquals( 3, connectionProvider.getPreparedStatements().size() ); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - verify( addressPreparedStatement, times( 1 ) ).executeBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (ID) values (?)" ); - verify( personPreparedStatement, times( 4 ) ).addBatch(); - verify( personPreparedStatement, times( 1 ) ).executeBatch(); + verifyContainsBatches( + new Batch( "insert into Address (ID) values (?)", 2 ), + new Batch( "insert into Person (ID) values (?)", 4 ), + new Batch( "insert into Person_Address (persons_ID, addresses_ID) values (?, ?)", 6 ) + ); } @Entity(name = "Address") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalMapsIdOneToOne.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalMapsIdOneToOne.java index bcdc7adbad..d947ef4c27 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalMapsIdOneToOne.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalMapsIdOneToOne.java @@ -6,9 +6,6 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -19,52 +16,27 @@ import javax.persistence.MapsId; import javax.persistence.OneToOne; import javax.persistence.SequenceGenerator; -import org.hibernate.cfg.Environment; - import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithBidirectionalMapsIdOneToOne - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); +public class InsertOrderingWithBidirectionalMapsIdOneToOne extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class }; } @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { Person worker = new Person(); Person homestay = new Person(); @@ -79,17 +51,13 @@ public class InsertOrderingWithBidirectionalMapsIdOneToOne session.persist( home ); session.persist( office ); - connectionProvider.clear(); + clearBatches(); } ); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - verify( addressPreparedStatement, times( 1 ) ).executeBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (address_ID) values (?)" ); - verify( personPreparedStatement, times( 2 ) ).addBatch(); - verify( personPreparedStatement, times( 1 ) ).executeBatch(); + verifyContainsBatches( + new Batch( "insert into Address (ID) values (?)", 2 ), + new Batch( "insert into Person (address_ID) values (?)", 2 ) + ); } @Entity(name = "Address") 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 d29a306b76..8d4cfe7611 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,11 +6,8 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -21,52 +18,27 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; -import org.hibernate.cfg.Environment; - import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithBidirectionalOneToMany - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); +public class InsertOrderingWithBidirectionalOneToMany extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class }; } @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { Person father = new Person(); Person mother = new Person(); @@ -87,17 +59,13 @@ public class InsertOrderingWithBidirectionalOneToMany session.persist( home ); session.persist( office ); - connectionProvider.clear(); + clearBatches(); } ); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - verify( addressPreparedStatement, times( 1 ) ).executeBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (address_ID, ID) values (?, ?)" ); - verify( personPreparedStatement, times( 4 ) ).addBatch(); - verify( personPreparedStatement, times( 1 ) ).executeBatch(); + verifyContainsBatches( + new Batch( "insert into Address (ID) values (?)", 2 ), + new Batch( "insert into Person (address_ID, ID) values (?, ?)", 4 ) + ); } @Entity(name = "Address") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToManyFlushProblem.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToManyFlushProblem.java index 3bc04ea866..2fd66bb53d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToManyFlushProblem.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToManyFlushProblem.java @@ -8,7 +8,6 @@ package org.hibernate.test.insertordering; import java.util.ArrayList; import java.util.List; -import java.util.Map; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -17,19 +16,18 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; import static javax.persistence.CascadeType.PERSIST; import static javax.persistence.GenerationType.SEQUENCE; -import static org.hibernate.cfg.AvailableSettings.ORDER_INSERTS; -import static org.hibernate.cfg.AvailableSettings.STATEMENT_BATCH_SIZE; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; @TestForIssue(jiraKey = "HHH-12074") -public class InsertOrderingWithBidirectionalOneToManyFlushProblem - extends BaseNonConfigCoreFunctionalTestCase { +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingWithBidirectionalOneToManyFlushProblem extends BaseInsertOrderingTest { @Test public void testBatchingWithFlush() { @@ -40,12 +38,16 @@ public class InsertOrderingWithBidirectionalOneToManyFlushProblem session.persist( top1 ); + clearBatches(); + // InsertActionSorter#sort is invoked during this flush. // // input: [top1] // output: [top1] session.flush(); + verifyContainsBatches( new Batch( "insert into TopEntity (id) values (?)" ) ); + MiddleEntity middle1 = new MiddleEntity(); middle1.addBottom( new BottomEntity() ); @@ -71,8 +73,16 @@ public class InsertOrderingWithBidirectionalOneToManyFlushProblem // when the attempt to insert middle2 before top2 is made. // // correct ordering is: [top2,middle1,middle2,bottom1,bottom2] + + clearBatches(); } ); + + verifyContainsBatches( + new Batch( "insert into TopEntity (id) values (?)" ), + new Batch( "insert into MiddleEntity (top_id, id) values (?, ?)", 2 ), + new Batch( "insert into BottomEntity (middle_id, id) values (?, ?)", 2 ) + ); } @Test @@ -89,8 +99,11 @@ public class InsertOrderingWithBidirectionalOneToManyFlushProblem // // input: [top1] // output: [top1] + clearBatches(); session.flush(); + verifyContainsBatches( new Batch( "insert into TopEntity (id) values (?)" ) ); + MiddleEntity middle1 = new MiddleEntity(); middle1.addBottom( new BottomEntity() ); @@ -120,19 +133,21 @@ public class InsertOrderingWithBidirectionalOneToManyFlushProblem // when the attempt to insert middle2 before top2 is made. // // correct ordering is: [top2,middle1,middle2,bottom1,bottom2] + clearBatches(); } ); + + verifyContainsBatches( + new Batch( "insert into TopEntity (id) values (?)", 2 ), + new Batch( "insert into MiddleEntity (top_id, id) values (?, ?)", 2 ), + new Batch( "insert into BottomEntity (middle_id, id) values (?, ?)", 2 ), + new Batch( "insert into BottomEntity2 (middle_id, id) values (?, ?)", 2 ) + ); } @Override - protected void addSettings(Map settings) { - settings.put( ORDER_INSERTS, "true" ); - settings.put( STATEMENT_BATCH_SIZE, "10" ); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { TopEntity.class, MiddleEntity.class, BottomEntity.class, BottomEntity2.class }; + protected Class[] getAnnotatedClasses() { + return new Class[] { TopEntity.class, MiddleEntity.class, BottomEntity.class, BottomEntity2.class }; } @Entity(name = "BottomEntity") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOne.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOne.java index 099dcdce9b..4df01459fc 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOne.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOne.java @@ -6,56 +6,36 @@ */ package org.hibernate.test.insertordering; -import org.hibernate.cfg.Environment; -import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.SequenceGenerator; + import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; import org.junit.Test; -import javax.persistence.*; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Map; - import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithBidirectionalOneToOne - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); +public class InsertOrderingWithBidirectionalOneToOne extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class }; } @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { Person worker = new Person(); Person homestay = new Person(); @@ -70,17 +50,13 @@ public class InsertOrderingWithBidirectionalOneToOne session.persist( home ); session.persist( office ); - connectionProvider.clear(); + clearBatches(); } ); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - verify( addressPreparedStatement, times( 1 ) ).executeBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (address_ID, ID) values (?, ?)" ); - verify( personPreparedStatement, times( 2 ) ).addBatch(); - verify( personPreparedStatement, times( 1 ) ).executeBatch(); + verifyContainsBatches( + new Batch( "insert into Address (ID) values (?)", 2 ), + new Batch( "insert into Person (address_ID, ID) values (?, ?)", 2 ) + ); } @Entity(name = "Address") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOneFlushProblem.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOneFlushProblem.java index 306a4d852c..c744dda5b8 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOneFlushProblem.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithBidirectionalOneToOneFlushProblem.java @@ -6,27 +6,25 @@ */ package org.hibernate.test.insertordering; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; - +import java.util.ArrayList; +import java.util.List; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.OneToOne; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import static org.hibernate.cfg.AvailableSettings.ORDER_INSERTS; -import static org.hibernate.cfg.AvailableSettings.STATEMENT_BATCH_SIZE; +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.TestForIssue; +import org.junit.Test; + import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; @TestForIssue(jiraKey = "HHH-12105") -public class InsertOrderingWithBidirectionalOneToOneFlushProblem - extends BaseNonConfigCoreFunctionalTestCase { +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingWithBidirectionalOneToOneFlushProblem extends BaseInsertOrderingTest { @Test public void testInsertSortingWithFlushPersistLeftBeforeRight() { @@ -36,8 +34,11 @@ public class InsertOrderingWithBidirectionalOneToOneFlushProblem TopEntity top1 = new TopEntity(); session.persist(top1); + clearBatches(); session.flush(); + verifyContainsBatches( new Batch( "insert into TopEntity (id) values (?)" ) ); + LeftEntity left = new LeftEntity(); RightEntity right = new RightEntity(); TopEntity top2 = new TopEntity(); @@ -54,8 +55,16 @@ public class InsertOrderingWithBidirectionalOneToOneFlushProblem session.persist(left); session.persist(right); session.persist(top2); + + clearBatches(); } ); + + verifyContainsBatches( + new Batch( "insert into TopEntity (id) values (?)" ), + new Batch( "insert into LeftEntity (top_id, id) values (?, ?)" ), + new Batch( "insert into RightEntity (left_id, top_id, id) values (?, ?, ?)" ) + ); } @Test @@ -66,8 +75,11 @@ public class InsertOrderingWithBidirectionalOneToOneFlushProblem TopEntity top1 = new TopEntity(); session.persist(top1); + clearBatches(); session.flush(); + verifyContainsBatches( new Batch( "insert into TopEntity (id) values (?)" ) ); + LeftEntity left = new LeftEntity(); RightEntity right = new RightEntity(); TopEntity top2 = new TopEntity(); @@ -84,19 +96,21 @@ public class InsertOrderingWithBidirectionalOneToOneFlushProblem session.persist(right); session.persist(left); session.persist(top2); + + clearBatches(); } ); + + verifyContainsBatches( + new Batch( "insert into TopEntity (id) values (?)" ), + new Batch( "insert into LeftEntity (top_id, id) values (?, ?)" ), + new Batch( "insert into RightEntity (left_id, top_id, id) values (?, ?, ?)" ) + ); } @Override - protected void addSettings(Map settings) { - settings.put( ORDER_INSERTS, "true" ); - settings.put( STATEMENT_BATCH_SIZE, "10" ); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[]{ + protected Class[] getAnnotatedClasses() { + return new Class[]{ LeftEntity.class, RightEntity.class, TopEntity.class, }; } 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 7a4092ccc3..d065a7c77b 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 @@ -7,7 +7,6 @@ package org.hibernate.test.insertordering; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.Access; import javax.persistence.AccessType; @@ -28,47 +27,24 @@ import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.hibernate.annotations.BatchSize; -import org.hibernate.cfg.Environment; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithJoinedTableInheritance - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( false, false ); +public class InsertOrderingWithJoinedTableInheritance extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class, SpecialPerson.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class, SpecialPerson.class }; } @Test @@ -98,10 +74,10 @@ public class InsertOrderingWithJoinedTableInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); + clearBatches(); } ); - assertEquals( 26, connectionProvider.getPreparedStatements().size() ); + verifyPreparedStatementCount( 26 ); } @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 ab4fb1b94c..c12df3fc3b 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 @@ -8,7 +8,6 @@ package org.hibernate.test.insertordering; import java.math.BigDecimal; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -27,31 +26,24 @@ import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.hibernate.annotations.BatchSize; -import org.hibernate.cfg.Environment; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithJoinedTableMultiLevelInheritance - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( false, false ); +public class InsertOrderingWithJoinedTableMultiLevelInheritance extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class, SpecialPerson.class, @@ -61,22 +53,6 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance }; } - @Override - 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(); - } - @Test public void testBatchingAmongstSubClasses() { doInHibernate( this::sessionFactory, session -> { @@ -99,10 +75,10 @@ public class InsertOrderingWithJoinedTableMultiLevelInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); + clearBatches(); } ); - assertEquals( 17, connectionProvider.getPreparedStatements().size() ); + verifyPreparedStatementCount( 17 ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithManyToOne.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithManyToOne.java index 8773f270f5..03e3b8e777 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithManyToOne.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithManyToOne.java @@ -6,67 +6,35 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; -import org.hibernate.cfg.Environment; - import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithManyToOne - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); +public class InsertOrderingWithManyToOne extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class }; } @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { Person father = new Person(); Person mother = new Person(); @@ -89,17 +57,13 @@ public class InsertOrderingWithManyToOne session.persist( son ); session.persist( daughter ); - connectionProvider.clear(); + clearBatches(); } ); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - verify( addressPreparedStatement, times( 1 ) ).executeBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (address_ID, ID) values (?, ?)" ); - verify( personPreparedStatement, times( 4 ) ).addBatch(); - verify( personPreparedStatement, times( 1 ) ).executeBatch(); + verifyContainsBatches( + new Batch( "insert into Address (ID) values (?)", 2 ), + new Batch( "insert into Person (address_ID, ID) values (?, ?)", 4 ) + ); } @Entity(name = "Address") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithMultipleManyToOne.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithMultipleManyToOne.java index 8b7b29c575..09030869c1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithMultipleManyToOne.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithMultipleManyToOne.java @@ -6,72 +6,36 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import javax.persistence.CascadeType; -import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; import javax.persistence.Id; -import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; -import javax.persistence.SequenceGenerator; - -import org.hibernate.cfg.Environment; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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 */ @TestForIssue(jiraKey = "HHH-11996") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithMultipleManyToOne - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( false, false ); +public class InsertOrderingWithMultipleManyToOne extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { + protected Class[] getAnnotatedClasses() { + return new Class[] { Parent.class, ChildA.class, ChildB.class, }; } - @Override - 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(); - } - @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { Parent parent = new Parent(); session.persist(parent); @@ -84,18 +48,16 @@ public class InsertOrderingWithMultipleManyToOne childB.setParent(parent); session.persist(childB); - connectionProvider.clear(); + clearBatches(); } ); - assertEquals( 3, connectionProvider.getPreparedStatements().size() ); - /*PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (ID) values (?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - verify( addressPreparedStatement, times( 1 ) ).executeBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (ID) values (?)" ); - verify( personPreparedStatement, times( 4 ) ).addBatch(); - verify( personPreparedStatement, times( 1 ) ).executeBatch();*/ + verifyPreparedStatementCount( 3 ); + /* + Map expectedBatching = new HashMap<>(); + expectedBatching.put( "insert into Address (ID) values (?)", 2 ); + expectedBatching.put( "insert into Person (ID) values (?)", 4 ); + verifyBatching( expectedBatching ); + */ } @Entity(name = "Parent") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSecondaryTable.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSecondaryTable.java index bad6246af7..27972f42f2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSecondaryTable.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithSecondaryTable.java @@ -8,8 +8,6 @@ package org.hibernate.test.insertordering; import java.util.ArrayList; import java.util.List; -import java.util.Map; - import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; @@ -30,25 +28,19 @@ import javax.persistence.SecondaryTable; import javax.persistence.SequenceGenerator; import javax.persistence.Table; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.TestForIssue; import org.junit.Test; -import org.hibernate.testing.TestForIssue; - -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; /** * @author Chris Cranford */ @TestForIssue(jiraKey = "HHH-11714") -public class InsertOrderingWithSecondaryTable extends BaseEntityManagerFunctionalTestCase { - @Override - protected void addConfigOptions(Map options) { - super.addConfigOptions( options ); - options.put( AvailableSettings.ORDER_INSERTS, Boolean.TRUE ); - options.put( AvailableSettings.ORDER_UPDATES, Boolean.TRUE ); - } +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingWithSecondaryTable extends BaseInsertOrderingTest { @Override protected Class[] getAnnotatedClasses() { @@ -63,7 +55,7 @@ public class InsertOrderingWithSecondaryTable extends BaseEntityManagerFunctiona @Test public void testInheritanceWithSecondaryTable() { - doInJPA( this::entityManagerFactory, entityManager -> { + doInHibernate( this::sessionFactory, session -> { final TopLevelEntity top = new TopLevelEntity(); final GeographicArea area1 = new GeographicArea(); @@ -79,9 +71,18 @@ public class InsertOrderingWithSecondaryTable extends BaseEntityManagerFunctiona area2.setShape( circle ); top.getGeographicAreas().add( area2 ); - entityManager.persist( top ); - entityManager.flush(); + session.save( top ); + + clearBatches(); } ); + + verifyContainsBatches( + new Batch( "insert into TOP_LEVEL (id) values (?)" ), + new Batch( "insert into SHAPE (SHAPE_TYPE, SHAPE_ID) values ('POLYGON', ?)" ), + new Batch( "insert into SHAPE (SHAPE_TYPE, SHAPE_ID) values ('CIRCLE', ?)" ), + new Batch( "insert into SHAPE_CIRCLE (centre, SHAPE_ID) values (?, ?)" ), + new Batch( "insert into GEOGRAPHIC_AREA (SHAPE_ID, TOP_LEVEL_ID, id) values (?, ?, ?)", 2 ) + ); } @Entity 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 b0b84c92ef..a859e2452e 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 @@ -7,7 +7,6 @@ package org.hibernate.test.insertordering; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.Access; import javax.persistence.AccessType; @@ -28,47 +27,24 @@ import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.hibernate.annotations.BatchSize; -import org.hibernate.cfg.Environment; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Steve Ebersole */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithSingleTableInheritance - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( false, false ); +public class InsertOrderingWithSingleTableInheritance extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class, SpecialPerson.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class, SpecialPerson.class }; } @Test @@ -99,10 +75,10 @@ public class InsertOrderingWithSingleTableInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); + clearBatches(); } ); - assertEquals( 3, connectionProvider.getPreparedStatements().size() ); + verifyPreparedStatementCount( 3 ); } @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 a3e48b8783..474fc8d429 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 @@ -7,7 +7,6 @@ package org.hibernate.test.insertordering; import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.persistence.Access; import javax.persistence.AccessType; @@ -28,47 +27,24 @@ import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.hibernate.annotations.BatchSize; -import org.hibernate.cfg.Environment; import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithTablePerClassInheritance - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( false, false ); +public class InsertOrderingWithTablePerClassInheritance extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class, SpecialPerson.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class, SpecialPerson.class }; } @Test @@ -99,10 +75,10 @@ public class InsertOrderingWithTablePerClassInheritance specialPerson.addAddress( new Address() ); session.persist( specialPerson ); } - connectionProvider.clear(); + clearBatches(); } ); - assertEquals( 3, connectionProvider.getPreparedStatements().size() ); + verifyPreparedStatementCount( 3 ); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOne.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOne.java index 95b4884d03..e392bbb71b 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOne.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOne.java @@ -6,9 +6,6 @@ */ package org.hibernate.test.insertordering; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.Map; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; @@ -18,52 +15,27 @@ import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.SequenceGenerator; -import org.hibernate.cfg.Environment; - import org.hibernate.testing.DialectChecks; import org.hibernate.testing.RequiresDialectFeature; 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; /** * @author Vlad Mihalcea */ @TestForIssue(jiraKey = "HHH-9864") @RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) -public class InsertOrderingWithUnidirectionalOneToOne - extends BaseNonConfigCoreFunctionalTestCase { - - private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider( true, false ); +public class InsertOrderingWithUnidirectionalOneToOne extends BaseInsertOrderingTest { @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Address.class, Person.class }; - } - - @Override - 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(); + protected Class[] getAnnotatedClasses() { + return new Class[] { Address.class, Person.class }; } @Test - public void testBatching() throws SQLException { + public void testBatching() { doInHibernate( this::sessionFactory, session -> { Person worker = new Person(); Person homestay = new Person(); @@ -78,17 +50,13 @@ public class InsertOrderingWithUnidirectionalOneToOne session.persist( home ); session.persist( office ); - connectionProvider.clear(); + clearBatches(); } ); - PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Address (person_ID, ID) values (?, ?)" ); - verify( addressPreparedStatement, times( 2 ) ).addBatch(); - verify( addressPreparedStatement, times( 1 ) ).executeBatch(); - PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( - "insert into Person (ID) values (?)" ); - verify( personPreparedStatement, times( 2 ) ).addBatch(); - verify( personPreparedStatement, times( 1 ) ).executeBatch(); + verifyContainsBatches( + new Batch( "insert into Address (person_ID, ID) values (?, ?)", 2 ), + new Batch( "insert into Person (ID) values (?)", 2 ) + ); } @Entity(name = "Address") diff --git a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOneJoinColumn.java b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOneJoinColumn.java index d65c774eb9..8513030baf 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOneJoinColumn.java +++ b/hibernate-core/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithUnidirectionalOneToOneJoinColumn.java @@ -7,8 +7,6 @@ package org.hibernate.test.insertordering; import java.io.Serializable; -import java.util.Map; - import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.EmbeddedId; @@ -18,20 +16,20 @@ import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Query; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; +import org.hibernate.testing.TestForIssue; import org.junit.Test; -import org.hibernate.testing.TestForIssue; - -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; /** * @author Chris Cranford */ @TestForIssue(jiraKey = "HHH-12569") -public class InsertOrderingWithUnidirectionalOneToOneJoinColumn extends BaseEntityManagerFunctionalTestCase { +@RequiresDialectFeature(DialectChecks.SupportsJdbcDriverProxying.class) +public class InsertOrderingWithUnidirectionalOneToOneJoinColumn extends BaseInsertOrderingTest { @Embeddable public static class PersonAddressId implements Serializable { @Column(name = "id") @@ -90,30 +88,31 @@ public class InsertOrderingWithUnidirectionalOneToOneJoinColumn extends BaseEnti return new Class[] { Person.class, Address.class }; } - @Override - protected void addConfigOptions(Map options) { - super.addConfigOptions( options ); - options.put( AvailableSettings.ORDER_INSERTS, "true" ); - } - @Test public void testBatchingWithEmbeddableId() { - doInJPA( this::entityManagerFactory, entityManager -> { + doInHibernate( this::sessionFactory, session -> { final PersonAddressId id = new PersonAddressId(); id.setId( 1 ); Person person = new Person(); person.setId( id ); - entityManager.persist( person ); + session.persist( person ); Address address = new Address(); address.setId( id ); address.setPerson( person ); - entityManager.persist( address ); + session.persist( address ); + + clearBatches(); } ); - doInJPA( this::entityManagerFactory, entityManager -> { - Query query = entityManager.createQuery( "FROM Person" ); + verifyContainsBatches( + new Batch( "insert into Person (id) values (?)" ), + new Batch( "insert into Address (id) values (?)" ) + ); + + doInHibernate( this::sessionFactory, session -> { + Query query = session.createQuery( "FROM Person" ); assertEquals( 1, query.getResultList().size() ); } ); } From 05f47ce80bfb656ee19c78db1258f670725a0ce5 Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Mon, 5 Oct 2020 16:13:21 -0400 Subject: [PATCH 5/5] HHH-14227 fix testing failure on Oracle --- .../internal/ScrollableResultsObjectArrayCastingTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/test/java/org/hibernate/internal/ScrollableResultsObjectArrayCastingTest.java b/hibernate-core/src/test/java/org/hibernate/internal/ScrollableResultsObjectArrayCastingTest.java index aa985c344a..46378b86ed 100644 --- a/hibernate-core/src/test/java/org/hibernate/internal/ScrollableResultsObjectArrayCastingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/internal/ScrollableResultsObjectArrayCastingTest.java @@ -9,6 +9,8 @@ import javax.persistence.TypedQuery; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; +import org.hibernate.testing.DialectChecks; +import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; import org.junit.Before; import org.junit.Test; @@ -20,6 +22,7 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; * @author Nathan Xu */ @TestForIssue( jiraKey = "HHH-14231" ) +@RequiresDialectFeature( DialectChecks.SupportsExpectedLobUsagePattern.class ) public class ScrollableResultsObjectArrayCastingTest extends BaseEntityManagerFunctionalTestCase { @Override @@ -31,7 +34,7 @@ public class ScrollableResultsObjectArrayCastingTest extends BaseEntityManagerFu public void SetUp() { doInJPA( this::entityManagerFactory, entityManager -> { Product product = new Product(); - product.binaryValue = "".getBytes(); + product.binaryValue = new byte[] { 1, 2, 3 }; entityManager.persist( product ); } ); }