From 39d5d344fb5b404648a4933574028ba64b53f621 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 1 Jun 2021 15:48:28 +0200 Subject: [PATCH] Fix throw exception when more than one row with the given identifier was found and re-enabled additiona tests --- .../SingleIdEntityLoaderStandardImpl.java | 4 +- .../loader/ast/internal/SingleIdLoadPlan.java | 8 +- .../JdbcSelectExecutorStandardImpl.java | 24 +++- .../sql/exec/spi/JdbcSelectExecutor.java | 8 ++ .../RowProcessingStateStandardImpl.java | 17 +++ .../sql/results/spi/ListResultsConsumer.java | 83 +++++++---- .../hibernate/{ => orm}/test/id/Car.hbm.xml | 2 +- .../org/hibernate/{ => orm}/test/id/Car.java | 2 +- .../test/id/ForeignGeneratorTest.java | 42 +++--- .../id/MultipleHiLoPerTableGeneratorTest.java | 81 +++++++++++ ...HiLoPerTableGeneratorWithRollbackTest.java | 73 ++++++++++ .../{ => orm}/test/id/NonUniqueIdTest.java | 60 ++++---- .../{ => orm}/test/id/Person.hbm.xml | 2 +- .../hibernate/{ => orm}/test/id/Person.java | 2 +- .../hibernate/{ => orm}/test/id/Plane.hbm.xml | 2 +- .../hibernate/{ => orm}/test/id/Plane.java | 2 +- .../id/PooledHiLoSequenceIdentifierTest.java | 133 ++++++++--------- .../{ => orm}/test/id/Product.hbm.xml | 2 +- .../hibernate/{ => orm}/test/id/Product.java | 2 +- .../hibernate/{ => orm}/test/id/Radio.hbm.xml | 2 +- .../hibernate/{ => orm}/test/id/Radio.java | 2 +- .../test/id/SQLServer2012Person.hbm.xml | 2 +- ...er2012SequenceGeneratorAnnotationTest.java | 49 ++++--- .../SQLServer2012SequenceGeneratorTest.java | 52 +++++++ .../orm/test/id/SequenceGeneratorTest.java | 65 +++++++++ .../test/id/UseIdentifierRollbackTest.java | 53 +++++++ .../orm/test/id/array/ByteArrayIdTest.java | 133 +++++++++++++++++ .../test/id/array/CharacterArrayIdTest.java | 127 ++++++++++++++++ .../id/array/PrimitiveByteArrayIdTest.java | 132 +++++++++++++++++ .../array/PrimitiveCharacterArrayIdTest.java | 127 ++++++++++++++++ .../id/sequence/LegacySequenceExportTest.java | 52 +++---- .../sequence/NegativeValueSequenceTest.java | 9 +- .../orm/test/id/sequence/OptimizerTest.java | 71 +++++++++ .../PostgreSQLIdentitySequenceTest.java | 70 +++++---- .../PostgreSQLIdentitySupportTest.java | 40 +++--- .../id/usertype/UserTypeComparableIdTest.java | 74 +++++----- .../usertype/UserTypeNonComparableIdTest.java | 60 ++++---- .../id/MultipleHiLoPerTableGeneratorTest.java | 108 -------------- .../SQLServer2012SequenceGeneratorTest.java | 55 ------- .../test/id/SequenceGeneratorTest.java | 72 ---------- .../test/id/UseIdentifierRollbackTest.java | 46 ------ .../test/id/array/ByteArrayIdTest.java | 134 ----------------- .../test/id/array/CharacterArrayIdTest.java | 129 ----------------- .../id/array/PrimitiveByteArrayIdTest.java | 135 ------------------ .../array/PrimitiveCharacterArrayIdTest.java | 129 ----------------- .../test/id/sequence/OptimizerTest.java | 71 --------- 46 files changed, 1327 insertions(+), 1221 deletions(-) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Car.hbm.xml (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Car.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/ForeignGeneratorTest.java (53%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorWithRollbackTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/NonUniqueIdTest.java (55%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Person.hbm.xml (91%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Person.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Plane.hbm.xml (93%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Plane.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/PooledHiLoSequenceIdentifierTest.java (51%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Product.hbm.xml (90%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Product.java (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Radio.hbm.xml (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/Radio.java (94%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/SQLServer2012Person.hbm.xml (92%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/SQLServer2012SequenceGeneratorAnnotationTest.java (54%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012SequenceGeneratorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/SequenceGeneratorTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/UseIdentifierRollbackTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/array/ByteArrayIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/array/CharacterArrayIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveByteArrayIdTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveCharacterArrayIdTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/sequence/LegacySequenceExportTest.java (72%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/sequence/NegativeValueSequenceTest.java (98%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/OptimizerTest.java rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/sequence/PostgreSQLIdentitySequenceTest.java (62%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/sequence/PostgreSQLIdentitySupportTest.java (54%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/usertype/UserTypeComparableIdTest.java (76%) rename hibernate-core/src/test/java/org/hibernate/{ => orm}/test/id/usertype/UserTypeNonComparableIdTest.java (78%) delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/MultipleHiLoPerTableGeneratorTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012SequenceGeneratorTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/UseIdentifierRollbackTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/sequence/OptimizerTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java index a604bea7dc..b6332a86a1 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdEntityLoaderStandardImpl.java @@ -68,7 +68,7 @@ public class SingleIdEntityLoaderStandardImpl extends SingleIdEntityLoaderSup session.getFactory() ); - return loadPlan.load( key, lockOptions, readOnly, session ); + return loadPlan.load( key, lockOptions, null, readOnly,true, session ); } @Override @@ -93,7 +93,7 @@ public class SingleIdEntityLoaderStandardImpl extends SingleIdEntityLoaderSup else { lockOptionsToUse = lockOptions; } - return loadPlan.load( key, lockOptionsToUse, entityInstance, readOnly, session ); + return loadPlan.load( key, lockOptionsToUse, entityInstance, readOnly, false, session ); } @Internal diff --git a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java index a15a49a006..46aac0ae6f 100644 --- a/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/ast/internal/SingleIdLoadPlan.java @@ -75,14 +75,14 @@ public class SingleIdLoadPlan implements SingleEntityLoadPlan { LockOptions lockOptions, Boolean readOnly, SharedSessionContractImplementor session) { - return load( restrictedValue, lockOptions, null, readOnly, session ); + return load( restrictedValue, lockOptions, null, readOnly,false, session ); } T load( Object restrictedValue, LockOptions lockOptions, SharedSessionContractImplementor session) { - return load( restrictedValue, lockOptions, null, null, session ); + return load( restrictedValue, lockOptions, null, null,false, session ); } T load( @@ -90,6 +90,7 @@ public class SingleIdLoadPlan implements SingleEntityLoadPlan { LockOptions lockOptions, Object entityInstance, Boolean readOnly, + Boolean singleResultExpected, SharedSessionContractImplementor session) { final SessionFactoryImplementor sessionFactory = session.getFactory(); final JdbcServices jdbcServices = sessionFactory.getJdbcServices(); @@ -153,7 +154,8 @@ public class SingleIdLoadPlan implements SingleEntityLoadPlan { } }, RowTransformerPassThruImpl.instance(), - true + true, + singleResultExpected ); if ( list.isEmpty() ) { diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java index ca8918cd7b..947819368a 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/internal/JdbcSelectExecutorStandardImpl.java @@ -80,7 +80,29 @@ public class JdbcSelectExecutorStandardImpl implements JdbcSelectExecutor { .getJdbcCoordinator() .getStatementPreparer() .prepareStatement( sql ), - ListResultsConsumer.instance( uniqueFilter ) + ListResultsConsumer.instance( uniqueFilter, false ) + ); + } + + @Override + public List list( + JdbcSelect jdbcSelect, + JdbcParameterBindings jdbcParameterBindings, + ExecutionContext executionContext, + RowTransformer rowTransformer, + boolean uniqueFilter, + boolean singleResultExpected) { + // Only do auto flushing for top level queries + return executeQuery( + jdbcSelect, + jdbcParameterBindings, + executionContext, + rowTransformer, + (sql) -> executionContext.getSession() + .getJdbcCoordinator() + .getStatementPreparer() + .prepareStatement( sql ), + ListResultsConsumer.instance( uniqueFilter, singleResultExpected ) ); } diff --git a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcSelectExecutor.java b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcSelectExecutor.java index 032977c70e..98ff065d2d 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcSelectExecutor.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/exec/spi/JdbcSelectExecutor.java @@ -30,6 +30,14 @@ public interface JdbcSelectExecutor { RowTransformer rowTransformer, boolean uniqueFilter); + List list( + JdbcSelect jdbcSelect, + JdbcParameterBindings jdbcParameterBindings, + ExecutionContext executionContext, + RowTransformer rowTransformer, + boolean uniqueFilter, + boolean onlyOne); + ScrollableResultsImplementor scroll( JdbcSelect jdbcSelect, ScrollMode scrollMode, diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java index c610808813..a3f5cef57d 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/internal/RowProcessingStateStandardImpl.java @@ -16,6 +16,7 @@ import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.sql.exec.spi.Callback; import org.hibernate.sql.exec.spi.ExecutionContext; import org.hibernate.sql.results.graph.Initializer; +import org.hibernate.sql.results.graph.collection.CollectionInitializer; import org.hibernate.sql.results.graph.entity.EntityFetch; import org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl; import org.hibernate.sql.results.jdbc.spi.JdbcValues; @@ -36,6 +37,7 @@ public class RowProcessingStateStandardImpl implements RowProcessingState { private final RowReader rowReader; private final JdbcValues jdbcValues; private final ExecutionContext executionContext; + public final boolean hasCollectionInitializers; public RowProcessingStateStandardImpl( JdbcValuesSourceProcessingStateStandardImpl resultSetProcessingState, @@ -50,13 +52,28 @@ public class RowProcessingStateStandardImpl implements RowProcessingState { final List initializers = rowReader.getInitializers(); if ( initializers == null || initializers.isEmpty() ) { this.initializers = NO_INITIALIZERS; + hasCollectionInitializers = false; } else { //noinspection ToArrayCallWithZeroLengthArrayArgument this.initializers = initializers.toArray( new Initializer[initializers.size()] ); + hasCollectionInitializers = hasCollectionInitializers(this.initializers); } } + private static boolean hasCollectionInitializers(Initializer[] initializers) { + for ( int i = 0; i < initializers.length; i++ ) { + if ( initializers[i] instanceof CollectionInitializer ) { + return true; + } + } + return false; + } + + public boolean hasCollectionInitializers(){ + return this.hasCollectionInitializers; + } + @Override public JdbcValuesSourceProcessingState getJdbcValuesSourceProcessingState() { return resultSetProcessingState; diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java b/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java index 7001494809..e0f38900e8 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/spi/ListResultsConsumer.java @@ -6,10 +6,10 @@ */ package org.hibernate.sql.results.spi; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import org.hibernate.HibernateException; import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.sql.results.jdbc.spi.JdbcValues; @@ -17,6 +17,7 @@ import org.hibernate.sql.results.jdbc.spi.JdbcValuesSourceProcessingOptions; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl; import org.hibernate.sql.results.internal.RowProcessingStateStandardImpl; +import org.hibernate.type.descriptor.java.JavaTypeDescriptor; /** * @author Steve Ebersole @@ -25,18 +26,28 @@ public class ListResultsConsumer implements ResultsConsumer, R> { /** * Singleton access */ - private static final ListResultsConsumer UNIQUE_FILTER_INSTANCE = new ListResultsConsumer(true); - private static final ListResultsConsumer NORMAL_INSTANCE = new ListResultsConsumer(false); + private static final ListResultsConsumer UNIQUE_FILTER_INSTANCE = new ListResultsConsumer( UniqueSemantic.FILTER ); + private static final ListResultsConsumer NORMAL_INSTANCE = new ListResultsConsumer( UniqueSemantic.NONE ); + private static final ListResultsConsumer UNIQUE_INSTANCE = new ListResultsConsumer( UniqueSemantic.ASSERT ); @SuppressWarnings("unchecked") - public static ListResultsConsumer instance(boolean uniqueFilter) { + public static ListResultsConsumer instance(boolean uniqueFilter, boolean singleResultExpected) { + if ( singleResultExpected ) { + return UNIQUE_INSTANCE; + } return uniqueFilter ? UNIQUE_FILTER_INSTANCE : NORMAL_INSTANCE; } - private final boolean uniqueFilter; + public enum UniqueSemantic { + NONE, + FILTER, + ASSERT; + } - public ListResultsConsumer(boolean uniqueFilter) { - this.uniqueFilter = uniqueFilter; + private final UniqueSemantic uniqueSemantic; + + public ListResultsConsumer(UniqueSemantic uniqueSemantic) { + this.uniqueSemantic = uniqueSemantic; } @Override @@ -51,35 +62,49 @@ public class ListResultsConsumer implements ResultsConsumer, R> { final PersistenceContext persistenceContext = session.getPersistenceContext(); persistenceContext.getLoadContexts().register( jdbcValuesSourceProcessingState ); - boolean uniqueRows = false; - final Class resultJavaType = rowReader.getResultJavaType(); - if ( uniqueFilter && resultJavaType != null && ! resultJavaType.isArray() ) { - final EntityPersister entityDescriptor = session.getFactory().getMetamodel().findEntityDescriptor( resultJavaType ); - if ( entityDescriptor != null ) { - uniqueRows = true; - } - } - final List results = new ArrayList<>(); - while ( rowProcessingState.next() ) { - final R row = rowReader.readRow( rowProcessingState, processingOptions ); - - boolean add = true; - if ( uniqueRows ) { - if ( results.contains( row ) ) { - add = false; + if ( uniqueSemantic == UniqueSemantic.NONE ) { + while ( rowProcessingState.next() ) { + results.add( rowReader.readRow( rowProcessingState, processingOptions ) ); + rowProcessingState.finishRowProcessing(); + } + } + else { + boolean uniqueRows = false; + final Class resultJavaType = rowReader.getResultJavaType(); + if ( resultJavaType != null && !resultJavaType.isArray() ) { + final EntityPersister entityDescriptor = session.getFactory().getMetamodel().findEntityDescriptor( + resultJavaType ); + if ( entityDescriptor != null ) { + uniqueRows = true; } } - - if ( add ) { - results.add( row ); + while ( rowProcessingState.next() ) { + final R row = rowReader.readRow( rowProcessingState, processingOptions ); + boolean add = true; + if ( uniqueRows ) { + if ( results.contains( row ) ) { + if ( uniqueSemantic == UniqueSemantic.ASSERT && !rowProcessingState.hasCollectionInitializers() ) { + throw new HibernateException( + "More than one row with the given identifier was found: " + + jdbcValuesSourceProcessingState.getExecutionContext() + .getEntityId() + + ", for class: " + + resultJavaType.getName() + ); + } + add = false; + } + } + if ( add ) { + results.add( row ); + } + rowProcessingState.finishRowProcessing(); } - - rowProcessingState.finishRowProcessing(); } persistenceContext.initializeNonLazyCollections(); - jdbcValuesSourceProcessingState.finishUp( ); + jdbcValuesSourceProcessingState.finishUp(); return results; } finally { diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Car.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Car.hbm.xml similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/id/Car.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Car.hbm.xml index 6ffe723da7..fb98fb3f1d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Car.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Car.hbm.xml @@ -16,7 +16,7 @@ --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Car.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Car.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/id/Car.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Car.java index 58bac9af1d..dc7e26a86e 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Car.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Car.java @@ -6,7 +6,7 @@ */ //$Id: Car.java 5686 2005-02-12 07:27:32Z steveebersole $ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/ForeignGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/ForeignGeneratorTest.java similarity index 53% rename from hibernate-core/src/test/java/org/hibernate/test/id/ForeignGeneratorTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/ForeignGeneratorTest.java index d9d222488d..ae07234eea 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/ForeignGeneratorTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/ForeignGeneratorTest.java @@ -1,44 +1,40 @@ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.MapsId; import javax.persistence.OneToOne; -import org.hibernate.Transaction; + import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; /** * @author Nathan Xu */ -public class ForeignGeneratorTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Product.class, - ProductDetails.class - }; - } +@DomainModel( + annotatedClasses = { + ForeignGeneratorTest.Product.class, + ForeignGeneratorTest.ProductDetails.class + } +) +@SessionFactory +public class ForeignGeneratorTest { @Test - @TestForIssue( jiraKey = "HHH-13456") - public void testForeignGeneratorInStatelessSession() { - - inStatelessSession(statelessSession -> { - - Transaction tx = statelessSession.beginTransaction(); + @TestForIssue(jiraKey = "HHH-13456") + public void testForeignGeneratorInStatelessSession(SessionFactoryScope scope) { + scope.inStatelessTransaction( statelessSession -> { Product product = new Product(); ProductDetails productDetails = new ProductDetails( product ); statelessSession.insert( productDetails ); - - tx.commit(); - }); + } ); } @Entity(name = "Product") @@ -63,7 +59,7 @@ public class ForeignGeneratorTest extends BaseCoreFunctionalTestCase { public ProductDetails() { } - public ProductDetails( Product product ) { + public ProductDetails(Product product) { this.product = product; } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorTest.java new file mode 100644 index 0000000000..8e0e20014e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorTest.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id; + + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Emmanuel Bernard + */ +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/id/Car.hbm.xml", + "org/hibernate/orm/test/id/Plane.hbm.xml", + "org/hibernate/orm/test/id/Radio.hbm.xml" + } +) +@SessionFactory +public class MultipleHiLoPerTableGeneratorTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from Car" ).executeUpdate(); + session.createQuery( "delete from Plane" ).executeUpdate(); + session.createQuery( "delete from Radio" ).executeUpdate(); + } + ); + } + + @Test + public void testDistinctId(SessionFactoryScope scope) { + int testLength = 8; + Car[] cars = new Car[testLength]; + scope.inTransaction( + session -> { + Plane[] planes = new Plane[testLength]; + for ( int i = 0; i < testLength; i++ ) { + cars[i] = new Car(); + cars[i].setColor( "Color" + i ); + planes[i] = new Plane(); + planes[i].setNbrOfSeats( i ); + session.persist( cars[i] ); + //s.persist(planes[i]); + } + } + ); + + for ( int i = 0; i < testLength; i++ ) { + assertEquals( i + 1, cars[i].getId().intValue() ); + //assertEquals(i+1, planes[i].getId().intValue()); + } + } + + @Test + public void testAllParams(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Radio radio = new Radio(); + radio.setFrequency( "32 MHz" ); + session.persist( radio ); + assertEquals( new Integer( 1 ), radio.getId() ); + radio = new Radio(); + radio.setFrequency( "32 MHz" ); + session.persist( radio ); + assertEquals( new Integer( 2 ), radio.getId() ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorWithRollbackTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorWithRollbackTest.java new file mode 100644 index 0000000000..db31177884 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/MultipleHiLoPerTableGeneratorWithRollbackTest.java @@ -0,0 +1,73 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id; + + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author Emmanuel Bernard + */ +@DomainModel( + xmlMappings = { + "org/hibernate/orm/test/id/Car.hbm.xml" + } +) +@SessionFactory +public class MultipleHiLoPerTableGeneratorWithRollbackTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + session.createQuery( "delete from Car" ).executeUpdate(); + } + ); + } + + + @Test + public void testRollingBack(SessionFactoryScope scope) { + int testLength = 3; + Long lastId = scope.fromSession( + session -> { + session.getTransaction().begin(); + try { + Long id = null; + for ( int i = 0; i < testLength; i++ ) { + Car car = new Car(); + car.setColor( "color " + i ); + session.save( car ); + id = car.getId(); + } + return id; + } + finally { + session.getTransaction().rollback(); + } + } + ); + + Car car = new Car(); + scope.inTransaction( + session -> { + car.setColor( "blue" ); + session.save( car ); + session.flush(); + } + ); + + assertEquals( lastId.longValue() + 1, car.getId().longValue(), "id generation was rolled back" ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/NonUniqueIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/NonUniqueIdTest.java similarity index 55% rename from hibernate-core/src/test/java/org/hibernate/test/id/NonUniqueIdTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/NonUniqueIdTest.java index 397feeb27c..aaa6e6cee1 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/NonUniqueIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/NonUniqueIdTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; import javax.persistence.Entity; import javax.persistence.Id; @@ -13,37 +13,37 @@ import javax.persistence.Table; import org.hibernate.HibernateException; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Before; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; /** * @author Gail Badner */ -public class NonUniqueIdTest extends BaseNonConfigCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = NonUniqueIdTest.Category.class +) +@SessionFactory +public class NonUniqueIdTest { - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Category.class }; - } - - @Before - public void setup() { + @BeforeAll + public void setup(SessionFactoryScope scope) { // Drop and recreate table so it has no primary key. The drop is done in a separate transaction because // some databases do not support dropping and recreating in the same transaction. - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { session.createNativeQuery( "DROP TABLE CATEGORY" ).executeUpdate(); } ); - doInHibernate( - this::sessionFactory, + + scope.inTransaction( session -> { session.createNativeQuery( "create table CATEGORY( id integer not null, name varchar(255) )" @@ -58,24 +58,32 @@ public class NonUniqueIdTest extends BaseNonConfigCoreFunctionalTestCase { ); } + @AfterAll + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from Category" ).executeUpdate() + + ); + } + @Test - @TestForIssue( jiraKey = "HHH-12802" ) - public void testLoadEntityWithNonUniqueId() { + @TestForIssue(jiraKey = "HHH-12802") + public void testLoadEntityWithNonUniqueId(SessionFactoryScope scope) { try { - doInHibernate( - this::sessionFactory, + scope.inTransaction( session -> { - session.get( Category.class, 1 ); - fail( "should have failed because there are 2 entities with id == 1" ); + session.get( Category.class, 1 ); + fail( "should have failed because there are 2 entities with id == 1" ); } ); } - catch ( HibernateException ex) { + catch (HibernateException ex) { // expected } } - @Entity + @Entity(name = "Category") @Table(name = "CATEGORY") public static class Category { @Id diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Person.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Person.hbm.xml similarity index 91% rename from hibernate-core/src/test/java/org/hibernate/test/id/Person.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Person.hbm.xml index 30e56432ab..91ce337e82 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Person.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Person.hbm.xml @@ -9,7 +9,7 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Person.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Person.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/id/Person.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Person.java index bccd4acea1..a902d0d0b5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Person.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Person.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; public class Person { diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Plane.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Plane.hbm.xml similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/test/id/Plane.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Plane.hbm.xml index 150a54631e..a1995201d2 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Plane.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Plane.hbm.xml @@ -16,7 +16,7 @@ --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Plane.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Plane.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/id/Plane.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Plane.java index 8c4bb82a95..926255b609 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Plane.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Plane.java @@ -6,7 +6,7 @@ */ //$Id: Plane.java 5686 2005-02-12 07:27:32Z steveebersole $ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/PooledHiLoSequenceIdentifierTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/PooledHiLoSequenceIdentifierTest.java similarity index 51% rename from hibernate-core/src/test/java/org/hibernate/test/id/PooledHiLoSequenceIdentifierTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/PooledHiLoSequenceIdentifierTest.java index 5f7b6d5b98..920f039857 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/PooledHiLoSequenceIdentifierTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/PooledHiLoSequenceIdentifierTest.java @@ -21,95 +21,84 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; -import java.sql.Connection; import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.List; -import java.util.Properties; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import org.hibernate.Session; -import org.hibernate.Transaction; import org.hibernate.annotations.GenericGenerator; -import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; -import org.hibernate.jdbc.Work; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import org.jboss.logging.Logger; - -import static org.junit.Assert.assertEquals; +import static org.hibernate.cfg.AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Andrea Boriero */ @TestForIssue(jiraKey = "HHH-9287") -public class PooledHiLoSequenceIdentifierTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = PooledHiLoSequenceIdentifierTest.SequenceIdentifier.class +) +@SessionFactory +@ServiceRegistry(settings = @Setting(name = USE_NEW_ID_GENERATOR_MAPPINGS, value = "true")) +public class PooledHiLoSequenceIdentifierTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from sequenceIdentifier" ).executeUpdate() + ); + } @Test - public void testSequenceIdentifierGenerator() { - Session s = null; - Transaction tx = null; - try { - s = openSession(); - tx = s.beginTransaction(); + public void testSequenceIdentifierGenerator(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + for ( int i = 0; i < 5; i++ ) { + session.persist( new SequenceIdentifier() ); + } + session.flush(); - for ( int i = 0; i < 5; i++ ) { - s.persist( new SequenceIdentifier() ); - } - s.flush(); + assertEquals( 5, countInsertedRows( session ) ); - assertEquals( 5, countInsertedRows( s ) ); + insertNewRow( session ); + insertNewRow( session ); - insertNewRow( s ); - insertNewRow( s ); + assertEquals( 7, countInsertedRows( session ) ); - assertEquals( 7, countInsertedRows( s ) ); + List ids = session.createQuery( "SELECT id FROM sequenceIdentifier" ).list(); - List ids = s.createQuery( "SELECT id FROM sequenceIdentifier" ).list(); - for ( Number id : ids ) { - log.debug( "Found id: " + id ); - } + assertEquals( 7, ids.size() ); - for ( int i = 0; i < 3; i++ ) { - s.persist( new SequenceIdentifier() ); - } - s.flush(); + for ( int i = 0; i < 3; i++ ) { + session.persist( new SequenceIdentifier() ); + } + session.flush(); - assertEquals( 10, countInsertedRows( s ) ); - } - finally { - if ( tx != null ) { - tx.rollback(); - } - - s.close(); - } + assertEquals( 10, countInsertedRows( session ) ); + } + ); } private int countInsertedRows(Session s) { - return ((Number) s.createNativeQuery( "SELECT COUNT(*) FROM sequenceIdentifier" ) - .uniqueResult()).intValue(); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] {SequenceIdentifier.class}; - } - - @Override - protected void configure(Configuration configuration) { - Properties properties = configuration.getProperties(); - properties.put( "hibernate.id.new_generator_mappings", "true" ); + return ( (Number) s.createNativeQuery( "SELECT COUNT(*) FROM sequenceIdentifier" ) + .uniqueResult() ).intValue(); } @Entity(name = "sequenceIdentifier") @@ -125,6 +114,8 @@ public class PooledHiLoSequenceIdentifierTest extends BaseCoreFunctionalTestCase ) @GeneratedValue(strategy = GenerationType.TABLE, generator = "sampleGenerator") private Long id; + + private String name; } private void insertNewRow(Session session) { @@ -132,19 +123,21 @@ public class PooledHiLoSequenceIdentifierTest extends BaseCoreFunctionalTestCase final SessionFactoryImplementor sfi = si.getFactory(); session.doWork( - new Work() { - @Override - public void execute(Connection connection) throws SQLException { - PreparedStatement statement = null; - try { - statement = connection.prepareStatement( "INSERT INTO sequenceIdentifier VALUES (?)" ); - statement.setObject( 1, sfi.getIdentifierGenerator( SequenceIdentifier.class.getName() ).generate( si, null ) ); - statement.executeUpdate(); - } - finally { - if ( statement != null ) { - statement.close(); - } + connection -> { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement( "INSERT INTO sequenceIdentifier VALUES (?,?)" ); + statement.setObject( + 1, + sfi.getIdentifierGenerator( SequenceIdentifier.class.getName() ) + .generate( si, null ) + ); + statement.setString( 2,"name" ); + statement.executeUpdate(); + } + finally { + if ( statement != null ) { + statement.close(); } } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Product.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Product.hbm.xml similarity index 90% rename from hibernate-core/src/test/java/org/hibernate/test/id/Product.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Product.hbm.xml index 196ac44e5f..563c1d2489 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Product.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Product.hbm.xml @@ -14,7 +14,7 @@ --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Product.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Product.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/id/Product.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Product.java index 7ad6aaee60..267c94e9a6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Product.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Product.java @@ -6,7 +6,7 @@ */ //$Id: $ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Radio.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Radio.hbm.xml similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/id/Radio.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Radio.hbm.xml index 5edbc8f990..37841120e5 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Radio.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Radio.hbm.xml @@ -17,7 +17,7 @@ --> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/Radio.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Radio.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/test/id/Radio.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/Radio.java index 9c1bf3bb3a..4e01bffe2a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/Radio.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/Radio.java @@ -6,7 +6,7 @@ */ //$Id: Radio.java 5686 2005-02-12 07:27:32Z steveebersole $ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; /** diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012Person.hbm.xml b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012Person.hbm.xml similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012Person.hbm.xml rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012Person.hbm.xml index 687971da1b..5f5d76318a 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012Person.hbm.xml +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012Person.hbm.xml @@ -9,7 +9,7 @@ "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> - + diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012SequenceGeneratorAnnotationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012SequenceGeneratorAnnotationTest.java similarity index 54% rename from hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012SequenceGeneratorAnnotationTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012SequenceGeneratorAnnotationTest.java index 1d1837cea3..38e6ce14fa 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012SequenceGeneratorAnnotationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012SequenceGeneratorAnnotationTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id; +package org.hibernate.orm.test.id; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -12,23 +12,31 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.dialect.SQLServer2012Dialect; +import org.hibernate.dialect.SQLServerDialect; -import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class SQLServer2012SequenceGeneratorAnnotationTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class }; + +@DomainModel( + annotatedClasses = SQLServer2012SequenceGeneratorAnnotationTest.Person.class +) +@SessionFactory +public class SQLServer2012SequenceGeneratorAnnotationTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope){ + scope.inTransaction( + session -> + session.createQuery( "delete from Person" ).executeUpdate() + ); } /** @@ -38,20 +46,15 @@ public class SQLServer2012SequenceGeneratorAnnotationTest extends BaseCoreFuncti */ @Test @TestForIssue(jiraKey = "HHH-8814") - @RequiresDialect(value=SQLServer2012Dialect.class) - public void testStartOfSequence() { - final Person person = doInHibernate( this::sessionFactory, session -> { + @RequiresDialect(value = SQLServerDialect.class, version = 2012) + public void testStartOfSequence(SessionFactoryScope scope) { + final Person person = scope.fromTransaction( session -> { final Person _person = new Person(); session.persist(_person); return _person; } ); - assertTrue(person.getId() == 10); - } - - @Override - protected boolean isCleanupTestDataRequired() { - return true; + assertEquals(10, person.getId()); } @Entity(name = "Person") diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012SequenceGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012SequenceGeneratorTest.java new file mode 100644 index 0000000000..6b4a5051b6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SQLServer2012SequenceGeneratorTest.java @@ -0,0 +1,52 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id; + +import org.hibernate.dialect.SQLServerDialect; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DomainModel( + xmlMappings = "org/hibernate/orm/test/id/SQLServer2012Person.hbm.xml" +) +@SessionFactory +public class SQLServer2012SequenceGeneratorTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from Person" ).executeUpdate() + ); + } + + /** + * SQL server requires that sequence be initialized to something other than the minimum value for the type + * (e.g., Long.MIN_VALUE). For generator = "sequence", the initial value must be provided as a parameter. + * For this test, the sequence is initialized to 10. + */ + @Test + @TestForIssue(jiraKey = "HHH-8814") + @RequiresDialect(value = SQLServerDialect.class, version = 2012) + public void testStartOfSequence(SessionFactoryScope scope) { + final Person person = scope.fromTransaction( session -> { + final Person _person = new Person(); + session.persist( _person ); + return _person; + } ); + + assertEquals( 10, person.getId() ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/SequenceGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SequenceGeneratorTest.java new file mode 100644 index 0000000000..1498429605 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/SequenceGeneratorTest.java @@ -0,0 +1,65 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id; + +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.SQLServerDialect; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.jdbc.SQLStatementInspector; +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.hibernate.testing.orm.junit.SkipForDialect; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +@DomainModel( + xmlMappings = "org/hibernate/orm/test/id/Person.hbm.xml" +) +@SessionFactory(statementInspectorClass = SQLStatementInspector.class) +@ServiceRegistry( + settings = @Setting(name = Environment.USE_NEW_ID_GENERATOR_MAPPINGS, value = "false") +) +public class SequenceGeneratorTest { + + + /** + * This seems a little trivial, but we need to guarantee that all Dialects start their sequences on a non-0 value. + */ + @Test + @TestForIssue(jiraKey = "HHH-8814") + @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsSequences.class) + @SkipForDialect( + dialectClass = SQLServerDialect.class, + version = 2012, + reason = "SQLServer2012Dialect initializes sequence to minimum value (e.g., Long.MIN_VALUE; Hibernate assumes it is uninitialized.", + matchSubTypes = true + ) + public void testStartOfSequence(SessionFactoryScope scope) { + final Person person = new Person(); + scope.inTransaction( + session -> { + session.persist( person ); + } + ); + + assertTrue( person.getId() > 0 ); + final SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector(); + assertTrue( statementInspector.getSqlQueries() + .stream() + .filter( sql -> sql.contains( "product_sequence" ) ) + .findFirst() + .isPresent() ); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/UseIdentifierRollbackTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/UseIdentifierRollbackTest.java new file mode 100644 index 0000000000..fb62908750 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/UseIdentifierRollbackTest.java @@ -0,0 +1,53 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ + +package org.hibernate.orm.test.id; + +import org.hibernate.cfg.Environment; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +/** + * @author Emmanuel Bernard + */ +@DomainModel( + xmlMappings = "org/hibernate/orm/test/id/Product.hbm.xml" +) +@ServiceRegistry(settings = @Setting(name = Environment.USE_IDENTIFIER_ROLLBACK, value = "true")) +@SessionFactory +public class UseIdentifierRollbackTest { + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from Product" ).executeUpdate() + ); + } + + @Test + public void testSimpleRollback(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Product prod = new Product(); + assertNull( prod.getName() ); + session.persist( prod ); + session.flush(); + assertNotNull( prod.getName() ); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/ByteArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/ByteArrayIdTest.java new file mode 100644 index 0000000000..0e351a9709 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/ByteArrayIdTest.java @@ -0,0 +1,133 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.dialect.MySQLDialect; +import org.hibernate.dialect.OracleDialect; +import org.hibernate.query.Query; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.SkipForDialect; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +@SkipForDialect(dialectClass = MySQLDialect.class, version = 5, matchSubTypes = true, reason = "BLOB/TEXT column 'id' used in key specification without a key length") +@SkipForDialect(dialectClass = OracleDialect.class, version = 9, matchSubTypes = true, reason = "ORA-02329: column of datatype LOB cannot be unique or a primary key") +@DomainModel( + annotatedClasses = ByteArrayIdTest.DemoEntity.class +) +@SessionFactory +public class ByteArrayIdTest { + + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new Byte[] { + (byte) ( i + 1 ), + (byte) ( i + 2 ), + (byte) ( i + 3 ), + (byte) ( i + 4 ) + }; + entity.name = "Simple name " + i; + session.persist( entity ); + } + } + ); + } + + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from ByteArrayIdTest$DemoEntity" ).executeUpdate() + ); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleDeletions(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + List results = query.list(); + session.delete( results.get( 0 ) ); + session.delete( results.get( 1 ) ); + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + } + ); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleUpdates(SessionFactoryScope scope) { + final String lastResultName = scope.fromTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + return results.get( 0 ).name; + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + final Set names = new HashSet(); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + } + ); + } + + + @Entity + @Table(name = "DemoEntity") + public static class DemoEntity { + @Id + public Byte[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/CharacterArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/CharacterArrayIdTest.java new file mode 100644 index 0000000000..a9e4dff94a --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/CharacterArrayIdTest.java @@ -0,0 +1,127 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.query.Query; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +@DomainModel( + annotatedClasses = CharacterArrayIdTest.DemoEntity.class +) +@SessionFactory +public class CharacterArrayIdTest { + + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new Character[] { + (char) ( i + 'a' ), + (char) ( i + 'b' ), + (char) ( i + 'c' ), + (char) ( i + 'd' ) + }; + entity.name = "Simple name " + i; + session.persist( entity ); + } + } + ); + } + + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from CharacterArrayIdTest$DemoEntity" ).executeUpdate() + ); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleDeletions(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + List results = query.list(); + session.delete( results.get( 0 ) ); + session.delete( results.get( 1 ) ); + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + } + ); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleUpdates(SessionFactoryScope scope) { + final String lastResultName = scope.fromTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + return results.get( 0 ).name; + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + final Set names = new HashSet(); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + } + ); + } + + @Entity + @Table(name = "DemoEntity") + public static class DemoEntity { + @Id + public Character[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveByteArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveByteArrayIdTest.java new file mode 100644 index 0000000000..9b5aba2075 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveByteArrayIdTest.java @@ -0,0 +1,132 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.dialect.MySQLDialect; +import org.hibernate.dialect.OracleDialect; +import org.hibernate.query.Query; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.SkipForDialect; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +@SkipForDialect(dialectClass = MySQLDialect.class, version = 5, reason = "BLOB/TEXT column 'id' used in key specification without a key length") +@SkipForDialect(dialectClass = OracleDialect.class, version = 9, matchSubTypes = true, reason = "ORA-02329: column of datatype LOB cannot be unique or a primary key") +@DomainModel( + annotatedClasses = PrimitiveByteArrayIdTest.DemoEntity.class +) +@SessionFactory +public class PrimitiveByteArrayIdTest { + + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new byte[] { + (byte) ( i + 1 ), + (byte) ( i + 2 ), + (byte) ( i + 3 ), + (byte) ( i + 4 ) + }; + entity.name = "Simple name " + i; + session.persist( entity ); + } + } + ); + } + + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from PrimitiveByteArrayIdTest$DemoEntity" ).executeUpdate() + ); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleDeletions(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + List results = query.list(); + session.delete( results.get( 0 ) ); + session.delete( results.get( 1 ) ); + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + } + ); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleUpdates(SessionFactoryScope scope) { + final String lastResultName = scope.fromTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + return results.get( 0 ).name; + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + final Set names = new HashSet(); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + } + ); + } + + @Entity + @Table(name = "DemoEntity") + public static class DemoEntity { + @Id + public byte[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveCharacterArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveCharacterArrayIdTest.java new file mode 100644 index 0000000000..5e82ae7587 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/array/PrimitiveCharacterArrayIdTest.java @@ -0,0 +1,127 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id.array; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.query.Query; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Piotr Krauzowicz + * @author Gail Badner + */ +@DomainModel( + annotatedClasses = PrimitiveCharacterArrayIdTest.DemoEntity.class +) +@SessionFactory +public class PrimitiveCharacterArrayIdTest { + + @BeforeEach + public void prepare(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + for ( int i = 0; i < 3; i++ ) { + DemoEntity entity = new DemoEntity(); + entity.id = new char[] { + (char) ( i + 'a' ), + (char) ( i + 'b' ), + (char) ( i + 'c' ), + (char) ( i + 'd' ) + }; + entity.name = "Simple name " + i; + session.persist( entity ); + } + } + ); + } + + @AfterEach + public void cleanup(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createQuery( "delete from PrimitiveCharacterArrayIdTest$DemoEntity" ).executeUpdate() + ); + } + + /** + * Removes two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleDeletions(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + List results = query.list(); + session.delete( results.get( 0 ) ); + session.delete( results.get( 1 ) ); + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + assertEquals( 1, query.list().size() ); + } + ); + } + + /** + * Updates two records from database. + */ + @Test + @TestForIssue(jiraKey = "HHH-8999") + public void testMultipleUpdates(SessionFactoryScope scope) { + final String lastResultName = scope.fromTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + results.get( 0 ).name = "Different 0"; + results.get( 1 ).name = "Different 1"; + return results.get( 0 ).name; + } + ); + + scope.inTransaction( + session -> { + Query query = session.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); + List results = (List) query.list(); + final Set names = new HashSet(); + for ( DemoEntity entity : results ) { + names.add( entity.name ); + } + assertTrue( names.contains( "Different 0" ) ); + assertTrue( names.contains( "Different 1" ) ); + assertTrue( names.contains( lastResultName ) ); + } + ); + } + + @Entity + @Table(name = "DemoEntity") + public static class DemoEntity { + @Id + public char[] id; + public String name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/LegacySequenceExportTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/LegacySequenceExportTest.java similarity index 72% rename from hibernate-core/src/test/java/org/hibernate/test/id/sequence/LegacySequenceExportTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/LegacySequenceExportTest.java index b7e0a00628..0de654adac 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/LegacySequenceExportTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/LegacySequenceExportTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id.sequence; +package org.hibernate.orm.test.id.sequence; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -14,7 +14,6 @@ import javax.persistence.SequenceGenerator; import javax.persistence.Table; import org.hibernate.boot.MetadataSources; -import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject; import org.hibernate.boot.model.relational.Namespace; import org.hibernate.boot.model.relational.Sequence; import org.hibernate.boot.registry.StandardServiceRegistry; @@ -23,33 +22,34 @@ import org.hibernate.boot.spi.MetadataImplementor; import org.hibernate.cfg.AvailableSettings; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseUnitTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.hibernate.testing.orm.junit.BaseUnitTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Steve Ebersole */ -public class LegacySequenceExportTest extends BaseUnitTestCase { +@BaseUnitTest +public class LegacySequenceExportTest { private StandardServiceRegistry ssr; - @Before + @BeforeEach public void prepare() { ssr = new StandardServiceRegistryBuilder() .applySetting( AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "false" ) .build(); } - @After + @AfterEach public void destroy() { StandardServiceRegistryBuilder.destroy( ssr ); } @Test - @TestForIssue( jiraKey = "HHH-9936" ) + @TestForIssue(jiraKey = "HHH-9936") public void testMultipleUsesOfDefaultSequenceName() { final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( Entity1.class ) @@ -70,7 +70,7 @@ public class LegacySequenceExportTest extends BaseUnitTestCase { } @Test - @TestForIssue( jiraKey = "HHH-9936" ) + @TestForIssue(jiraKey = "HHH-9936") public void testMultipleUsesOfExplicitSequenceName() { final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( Entity3.class ) @@ -90,37 +90,37 @@ public class LegacySequenceExportTest extends BaseUnitTestCase { assertEquals( 1, count ); } - @Entity( name = "Entity1" ) - @Table( name = "Entity1" ) + @Entity(name = "Entity1") + @Table(name = "Entity1") public static class Entity1 { @Id - @GeneratedValue( strategy = GenerationType.SEQUENCE ) + @GeneratedValue(strategy = GenerationType.SEQUENCE) public Integer id; } - @Entity( name = "Entity2" ) - @Table( name = "Entity2" ) + @Entity(name = "Entity2") + @Table(name = "Entity2") public static class Entity2 { @Id - @GeneratedValue( strategy = GenerationType.SEQUENCE ) + @GeneratedValue(strategy = GenerationType.SEQUENCE) public Integer id; } - @Entity( name = "Entity3" ) - @Table( name = "Entity3" ) + @Entity(name = "Entity3") + @Table(name = "Entity3") public static class Entity3 { @Id - @GeneratedValue( strategy = GenerationType.SEQUENCE ) - @SequenceGenerator( name = "my_sequence" ) + @GeneratedValue(strategy = GenerationType.SEQUENCE) + @SequenceGenerator(name = "my_sequence") public Integer id; } - @Entity( name = "Entity4" ) - @Table( name = "Entity4" ) + @Entity(name = "Entity4") + @Table(name = "Entity4") public static class Entity4 { @Id - @GeneratedValue( strategy = GenerationType.SEQUENCE ) - @SequenceGenerator( name = "my_sequence" ) + @GeneratedValue(strategy = GenerationType.SEQUENCE) + @SequenceGenerator(name = "my_sequence") public Integer id; } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/NegativeValueSequenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/NegativeValueSequenceTest.java similarity index 98% rename from hibernate-core/src/test/java/org/hibernate/test/id/sequence/NegativeValueSequenceTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/NegativeValueSequenceTest.java index 9e3aa7c46a..78f9f53618 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/NegativeValueSequenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/NegativeValueSequenceTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id.sequence; +package org.hibernate.orm.test.id.sequence; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -30,12 +30,13 @@ import org.hibernate.testing.TestForIssue; import org.hibernate.testing.logger.LoggerInspectionRule; import org.hibernate.testing.logger.Triggerable; import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jboss.logging.Logger; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + /** * @author Gail Badner diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/OptimizerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/OptimizerTest.java new file mode 100644 index 0000000000..b002578bc7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/OptimizerTest.java @@ -0,0 +1,71 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.id.sequence; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.SequenceGenerator; +import javax.persistence.Table; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsNull.notNullValue; + +/** + * @author Steve Ebersole + */ +@DomainModel( + annotatedClasses = OptimizerTest.TheEntity.class +) +@SessionFactory +public class OptimizerTest { + + @Test + @TestForIssue(jiraKey = "HHH-10166") + public void testGenerationPastBound(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + for ( int i = 0; i < 100; i++ ) { + TheEntity entity = new TheEntity( Integer.toString( i ) ); + session.save( entity ); + } + } + ); + + scope.inTransaction( + session -> { + TheEntity number100 = session.get( TheEntity.class, 100 ); + assertThat( number100, notNullValue() ); + session.createQuery( "delete TheEntity" ).executeUpdate(); + } + ); + } + + @Entity(name = "TheEntity") + @Table(name = "TheEntity") + public static class TheEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq1") + @SequenceGenerator(name = "seq1", sequenceName = "the_sequence") + public Integer id; + public String someString; + + public TheEntity() { + } + + public TheEntity(String someString) { + this.someString = someString; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/PostgreSQLIdentitySequenceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/PostgreSQLIdentitySequenceTest.java similarity index 62% rename from hibernate-core/src/test/java/org/hibernate/test/id/sequence/PostgreSQLIdentitySequenceTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/PostgreSQLIdentitySequenceTest.java index 2ec22309c6..7a3f323731 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/PostgreSQLIdentitySequenceTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/PostgreSQLIdentitySequenceTest.java @@ -4,68 +4,66 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id.sequence; - -import org.hibernate.cfg.Environment; -import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.RequiresDialect; -import org.junit.Test; +package org.hibernate.orm.test.id.sequence; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.SequenceGenerator; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.fail; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.PostgreSQLDialect; +import org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.fail; + /** * @author Vlad Mhalcea */ -@RequiresDialect(jiraKey = "HHH-13106", value = PostgreSQLDialect.class) -public class PostgreSQLIdentitySequenceTest extends BaseEntityManagerFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Role.class }; - } +@TestForIssue(jiraKey = "HHH-13106") +@RequiresDialect(value = PostgreSQLDialect.class, version = 10) +@Jpa( + annotatedClasses = PostgreSQLIdentitySequenceTest.Role.class +) +public class PostgreSQLIdentitySequenceTest { private DriverManagerConnectionProviderImpl connectionProvider; - @Override - public void buildEntityManagerFactory() { + @BeforeAll + public void produceEntityManagerFactory() throws SQLException { connectionProvider = new DriverManagerConnectionProviderImpl(); connectionProvider.configure( Environment.getProperties() ); - try(Connection connection = connectionProvider.getConnection(); - Statement statement = connection.createStatement()) { + try (Connection connection = connectionProvider.getConnection(); + Statement statement = connection.createStatement()) { statement.execute( "DROP TABLE IF EXISTS roles CASCADE" ); statement.execute( "CREATE TABLE roles ( id BIGINT NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY )" ); } - catch (SQLException e) { - fail(e.getMessage()); - } - - super.buildEntityManagerFactory(); } - @Override + @AfterAll public void releaseResources() { - super.releaseResources(); - try(Connection connection = connectionProvider.getConnection(); - Statement statement = connection.createStatement()) { + try (Connection connection = connectionProvider.getConnection(); + Statement statement = connection.createStatement()) { statement.execute( "DROP TABLE IF EXISTS roles CASCADE" ); } catch (SQLException e) { - fail(e.getMessage()); + fail( e.getMessage() ); } if ( connectionProvider != null ) { @@ -74,8 +72,8 @@ public class PostgreSQLIdentitySequenceTest extends BaseEntityManagerFunctionalT } @Test - public void test() { - doInJPA( this::entityManagerFactory, entityManager -> { + public void test(EntityManagerFactoryScope scope) { + scope.inTransaction( entityManager -> { Role role = new Role(); entityManager.persist( role ); } ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/PostgreSQLIdentitySupportTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/PostgreSQLIdentitySupportTest.java similarity index 54% rename from hibernate-core/src/test/java/org/hibernate/test/id/sequence/PostgreSQLIdentitySupportTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/PostgreSQLIdentitySupportTest.java index 957367ea6b..7ef1c73dc6 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/PostgreSQLIdentitySupportTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/sequence/PostgreSQLIdentitySupportTest.java @@ -4,35 +4,37 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id.sequence; - -import org.hibernate.dialect.PostgreSQLDialect; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; -import org.hibernate.testing.RequiresDialect; -import org.junit.Test; +package org.hibernate.orm.test.id.sequence; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertNotNull; +import org.hibernate.dialect.PostgreSQLDialect; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialect; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + /** * @author Vlad Mhalcea */ -@RequiresDialect(jiraKey = "HHH-13202", value = PostgreSQLDialect.class) -public class PostgreSQLIdentitySupportTest extends BaseEntityManagerFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Role.class }; - } +@TestForIssue(jiraKey = "HHH-13202") +@RequiresDialect(value = PostgreSQLDialect.class) +@Jpa( + annotatedClasses = PostgreSQLIdentitySupportTest.Role.class +) +public class PostgreSQLIdentitySupportTest { @Test - public void test() { - Role _role = doInJPA( this::entityManagerFactory, entityManager -> { + public void test(EntityManagerFactoryScope scope) { + Role _role = scope.fromTransaction( entityManager -> { Role role = new Role(); entityManager.persist( role ); @@ -40,9 +42,9 @@ public class PostgreSQLIdentitySupportTest extends BaseEntityManagerFunctionalTe return role; } ); - doInJPA( this::entityManagerFactory, entityManager -> { + scope.inTransaction( entityManager -> { Role role = entityManager.find( Role.class, _role.getId() ); - assertNotNull(role); + assertNotNull( role ); } ); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeComparableIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/usertype/UserTypeComparableIdTest.java similarity index 76% rename from hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeComparableIdTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/usertype/UserTypeComparableIdTest.java index 496cdcdd83..0f50da992f 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeComparableIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/usertype/UserTypeComparableIdTest.java @@ -4,68 +4,65 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id.usertype; +package org.hibernate.orm.test.id.usertype; import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Comparator; - import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; -import org.junit.Test; - import org.hibernate.HibernateException; -import org.hibernate.Session; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.type.LongType; import org.hibernate.usertype.UserType; -public class UserTypeComparableIdTest extends BaseCoreFunctionalTestCase { - @Override - protected void configure(Configuration configuration) { - configuration.setProperty( AvailableSettings.ORDER_UPDATES, "true" ); - } +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +@DomainModel( + annotatedClasses = UserTypeComparableIdTest.SomeEntity.class +) +@SessionFactory +@ServiceRegistry( + settings = @Setting(name = AvailableSettings.ORDER_UPDATES, value = "true") +) +public class UserTypeComparableIdTest { @Test @TestForIssue(jiraKey = "HHH-8999") - public void testUserTypeId() { - Session s = openSession(); - s.beginTransaction(); + public void testUserTypeId(SessionFactoryScope scope) { SomeEntity e1 = new SomeEntity(); - CustomId e1Id = new CustomId( 1L ); - e1.setCustomId( e1Id ); SomeEntity e2 = new SomeEntity(); - CustomId e2Id = new CustomId( 2L ); - e2.setCustomId( e2Id ); - s.persist( e1 ); - s.persist( e2 ); - s.getTransaction().commit(); - s.close(); + scope.inTransaction( + session -> { + CustomId e1Id = new CustomId( 1L ); + e1.setCustomId( e1Id ); + CustomId e2Id = new CustomId( 2L ); + e2.setCustomId( e2Id ); + session.persist( e1 ); + session.persist( e2 ); + } + ); - s = openSession(); - s.beginTransaction(); - e1 = s.get( SomeEntity.class, e1Id ); - e2 = s.get( SomeEntity.class, e2Id ); - s.delete( e1 ); - s.delete( e2 ); - s.getTransaction().commit(); - s.close(); - } - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { SomeEntity.class }; + scope.inTransaction( + session -> { + session.delete( session.get( SomeEntity.class, e1.getCustomId() ) ); + session.delete( session.get( SomeEntity.class, e2.getCustomId() ) ); + } + ); } @@ -139,7 +136,8 @@ public class UserTypeComparableIdTest extends BaseCoreFunctionalTestCase { } @Override - public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { + public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) + throws SQLException { Long value = rs.getLong( position ); return new CustomId( value ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeNonComparableIdTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/id/usertype/UserTypeNonComparableIdTest.java similarity index 78% rename from hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeNonComparableIdTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/id/usertype/UserTypeNonComparableIdTest.java index 019c789e84..497c004e64 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/id/usertype/UserTypeNonComparableIdTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/id/usertype/UserTypeNonComparableIdTest.java @@ -4,7 +4,7 @@ * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or . */ -package org.hibernate.test.id.usertype; +package org.hibernate.orm.test.id.usertype; import java.io.Serializable; import java.sql.PreparedStatement; @@ -16,7 +16,6 @@ import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.HibernateException; -import org.hibernate.Session; import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.engine.spi.SharedSessionContractImplementor; @@ -24,43 +23,41 @@ import org.hibernate.type.LongType; import org.hibernate.usertype.UserType; import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; -public class UserTypeNonComparableIdTest extends BaseCoreFunctionalTestCase { +@DomainModel( + annotatedClasses = UserTypeNonComparableIdTest.SomeEntity.class +) +@SessionFactory +public class UserTypeNonComparableIdTest { @Test @TestForIssue(jiraKey = "HHH-8999") - public void testUserTypeId() { - Session s = openSession(); - s.beginTransaction(); + public void testUserTypeId(SessionFactoryScope scope) { SomeEntity e1 = new SomeEntity(); - CustomId e1Id = new CustomId( 1L ); - e1.setCustomId( e1Id ); SomeEntity e2 = new SomeEntity(); - CustomId e2Id = new CustomId( 2L ); - e2.setCustomId( e2Id ); - s.persist( e1 ); - s.persist( e2 ); - s.getTransaction().commit(); - s.close(); + scope.inTransaction( + session -> { + CustomId e1Id = new CustomId( 1L ); + e1.setCustomId( e1Id ); + CustomId e2Id = new CustomId( 2L ); + e2.setCustomId( e2Id ); + session.persist( e1 ); + session.persist( e2 ); + } + ); - s = openSession(); - s.beginTransaction(); - e1 = s.get( SomeEntity.class, e1Id ); - e2 = s.get( SomeEntity.class, e2Id ); - s.delete( e1 ); - s.delete( e2 ); - s.getTransaction().commit(); - s.close(); + scope.inTransaction( + session -> { + session.delete( session.get( SomeEntity.class, e1.getCustomId() ) ); + session.delete( session.get( SomeEntity.class, e2.getCustomId() ) ); + } + ); } - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { SomeEntity.class }; - } - - @TypeDef( name = "customId", typeClass = CustomIdType.class @@ -126,7 +123,8 @@ public class UserTypeNonComparableIdTest extends BaseCoreFunctionalTestCase { } @Override - public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { + public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) + throws SQLException { Long value = rs.getLong( position ); return new CustomId( value ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/MultipleHiLoPerTableGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/MultipleHiLoPerTableGeneratorTest.java deleted file mode 100644 index 95d98a9da8..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/MultipleHiLoPerTableGeneratorTest.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id; - -import org.junit.Test; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; - -/** - * @author Emmanuel Bernard - */ -public class MultipleHiLoPerTableGeneratorTest extends BaseCoreFunctionalTestCase { - public String[] getMappings() { - return new String[]{ "id/Car.hbm.xml", "id/Plane.hbm.xml", "id/Radio.hbm.xml" }; - } - - @Test - public void testDistinctId() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - int testLength = 8; - Car[] cars = new Car[testLength]; - Plane[] planes = new Plane[testLength]; - for (int i = 0; i < testLength ; i++) { - cars[i] = new Car(); - cars[i].setColor("Color" + i); - planes[i] = new Plane(); - planes[i].setNbrOfSeats(i); - s.persist(cars[i]); - //s.persist(planes[i]); - } - tx.commit(); - s.close(); - for (int i = 0; i < testLength ; i++) { - assertEquals(i+1, cars[i].getId().intValue()); - //assertEquals(i+1, planes[i].getId().intValue()); - } - - s = openSession(); - tx = s.beginTransaction(); - s.createQuery( "delete from Car" ).executeUpdate(); - tx.commit(); - s.close(); - } - - @Test - public void testRollingBack() throws Throwable { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - int testLength = 3; - Long lastId = null; - for (int i = 0; i < testLength ; i++) { - Car car = new Car(); - car.setColor( "color " + i ); - s.save( car ); - lastId = car.getId(); - } - tx.rollback(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - Car car = new Car(); - car.setColor( "blue" ); - s.save( car ); - s.flush(); - tx.commit(); - s.close(); - - assertEquals( "id generation was rolled back", lastId.longValue() + 1, car.getId().longValue() ); - - s = openSession(); - tx = s.beginTransaction(); - s.createQuery( "delete Car" ).executeUpdate(); - tx.commit(); - s.close(); - } - - @Test - public void testAllParams() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - Radio radio = new Radio(); - radio.setFrequency("32 MHz"); - s.persist(radio); - assertEquals( new Integer(1), radio.getId() ); - radio = new Radio(); - radio.setFrequency("32 MHz"); - s.persist(radio); - assertEquals( new Integer(2), radio.getId() ); - tx.commit(); - s.close(); - - s = openSession(); - tx = s.beginTransaction(); - s.createQuery( "delete from Radio" ).executeUpdate(); - tx.commit(); - s.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012SequenceGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012SequenceGeneratorTest.java deleted file mode 100644 index 112e303e8a..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/SQLServer2012SequenceGeneratorTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id; - -import java.util.List; - -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.dialect.SQLServer2012Dialect; -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.RequiresDialect; -import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.Test; - -public class SQLServer2012SequenceGeneratorTest extends BaseCoreFunctionalTestCase { - - @Override - public String[] getMappings() { - return new String[] { "id/SQLServer2012Person.hbm.xml" }; - } - - /** - * SQL server requires that sequence be initialized to something other than the minimum value for the type - * (e.g., Long.MIN_VALUE). For generator = "sequence", the initial value must be provided as a parameter. - * For this test, the sequence is initialized to 10. - */ - @Test - @TestForIssue(jiraKey = "HHH-8814") - @RequiresDialect(value=SQLServer2012Dialect.class) - public void testStartOfSequence() { - final Person person = doInHibernate( this::sessionFactory, session -> { - final Person _person = new Person(); - session.persist(_person); - return _person; - } ); - - assertTrue(person.getId() == 10); - } - - @Override - protected boolean isCleanupTestDataRequired() { - return true; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java deleted file mode 100644 index 67258c4551..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/SequenceGeneratorTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id; - -import java.util.Map; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.boot.SessionFactoryBuilder; -import org.hibernate.cfg.Environment; -import org.hibernate.dialect.SQLServer2012Dialect; - -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.hibernate.testing.jdbc.SQLStatementInterceptor; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -public class SequenceGeneratorTest extends BaseNonConfigCoreFunctionalTestCase { - - private SQLStatementInterceptor sqlStatementInterceptor; - - @Override - protected void configureSessionFactoryBuilder(SessionFactoryBuilder sfb) { - sqlStatementInterceptor = new SQLStatementInterceptor( sfb ); - } - - @Override - public String[] getMappings() { - return new String[] { "id/Person.hbm.xml" }; - } - - @Override - protected void addSettings(Map settings) { - settings.put( Environment.USE_NEW_ID_GENERATOR_MAPPINGS, "false" ); - } - - /** - * This seems a little trivial, but we need to guarantee that all Dialects start their sequences on a non-0 value. - */ - @Test - @TestForIssue(jiraKey = "HHH-8814") - @RequiresDialectFeature(DialectChecks.SupportsSequences.class) - @SkipForDialect( - value = SQLServer2012Dialect.class, - comment = "SQLServer2012Dialect initializes sequence to minimum value (e.g., Long.MIN_VALUE; Hibernate assumes it is uninitialized." - ) - public void testStartOfSequence() throws Exception { - Session s = openSession(); - Transaction tx = s.beginTransaction(); - final Person person = new Person(); - s.persist( person ); - tx.commit(); - s.close(); - - assertTrue( person.getId() > 0 ); - assertTrue( sqlStatementInterceptor.getSqlQueries() - .stream() - .filter( sql -> sql.contains( "product_sequence" ) ) - .findFirst() - .isPresent() ); - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/UseIdentifierRollbackTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/UseIdentifierRollbackTest.java deleted file mode 100644 index 8556410dbb..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/UseIdentifierRollbackTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ - -package org.hibernate.test.id; - -import org.junit.Test; - -import org.hibernate.Session; -import org.hibernate.Transaction; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -/** - * @author Emmanuel Bernard - */ -public class UseIdentifierRollbackTest extends BaseCoreFunctionalTestCase { - public String[] getMappings() { - return new String[] { "id/Product.hbm.xml" }; - } - - public void configure(Configuration cfg) { - cfg.setProperty( Environment.USE_IDENTIFIER_ROLLBACK, "true"); - super.configure( cfg ); - } - - @Test - public void testSimpleRollback() { - Session session = openSession(); - Transaction t = session.beginTransaction(); - Product prod = new Product(); - assertNull( prod.getName() ); - session.persist(prod); - session.flush(); - assertNotNull( prod.getName() ); - t.rollback(); - session.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java deleted file mode 100644 index 1e08ead9b3..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/array/ByteArrayIdTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id.array; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.hibernate.query.Query; -import org.hibernate.Session; -import org.hibernate.dialect.MySQL5Dialect; -import org.hibernate.dialect.Oracle9iDialect; - -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author Piotr Krauzowicz - * @author Gail Badner - */ -@SkipForDialect(value = MySQL5Dialect.class, comment = "BLOB/TEXT column 'id' used in key specification without a key length") -@SkipForDialect(value = Oracle9iDialect.class, comment = "ORA-02329: column of datatype LOB cannot be unique or a primary key") -public class ByteArrayIdTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { DemoEntity.class }; - } - - @Before - public void prepare() { - Session s = openSession(); - s.getTransaction().begin(); - for ( int i = 0; i < 3; i++ ) { - DemoEntity entity = new DemoEntity(); - entity.id = new Byte[] { - (byte) ( i + 1 ), - (byte) ( i + 2 ), - (byte) ( i + 3 ), - (byte) ( i + 4 ) - }; - entity.name = "Simple name " + i; - s.persist( entity ); - } - s.getTransaction().commit(); - s.close(); - } - - @After - public void cleanup() { - Session s = openSession(); - s.getTransaction().begin(); - s.createQuery( "delete from ByteArrayIdTest$DemoEntity" ).executeUpdate(); - s.getTransaction().commit(); - s.close(); - } - - /** - * Removes two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleDeletions() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); - List results = query.list(); - s.delete( results.get( 0 ) ); - s.delete( results.get( 1 ) ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); - assertEquals( 1, query.list().size() ); - s.getTransaction().commit(); - s.close(); - } - - /** - * Updates two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleUpdates() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); - List results = (List) query.list(); - results.get( 0 ).name = "Different 0"; - results.get( 1 ).name = "Different 1"; - final String lastResultName = results.get( 0 ).name; - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM ByteArrayIdTest$DemoEntity s" ); - results = (List) query.list(); - final Set names = new HashSet( ); - for ( DemoEntity entity : results ) { - names.add( entity.name ); - } - assertTrue( names.contains( "Different 0" ) ); - assertTrue( names.contains( "Different 1" ) ); - assertTrue( names.contains( lastResultName ) ); - s.getTransaction().commit(); - s.close(); - } - - - @Entity - @Table(name="DemoEntity") - public static class DemoEntity { - @Id - public Byte[] id; - public String name; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java deleted file mode 100644 index 9403933f03..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/array/CharacterArrayIdTest.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id.array; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.hibernate.query.Query; -import org.hibernate.Session; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author Piotr Krauzowicz - * @author Gail Badner - */ -public class CharacterArrayIdTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { DemoEntity.class }; - } - - @Before - public void prepare() { - Session s = openSession(); - s.getTransaction().begin(); - for ( int i = 0; i < 3; i++ ) { - DemoEntity entity = new DemoEntity(); - entity.id = new Character[] { - (char) ( i + 'a' ), - (char) ( i + 'b' ), - (char) ( i + 'c' ), - (char) ( i + 'd' ) - }; - entity.name = "Simple name " + i; - s.persist( entity ); - } - s.getTransaction().commit(); - s.close(); - } - - @After - public void cleanup() { - Session s = openSession(); - s.getTransaction().begin(); - s.createQuery( "delete from CharacterArrayIdTest$DemoEntity" ).executeUpdate(); - s.getTransaction().commit(); - s.close(); - } - - /** - * Removes two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleDeletions() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); - List results = query.list(); - s.delete( results.get( 0 ) ); - s.delete( results.get( 1 ) ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); - assertEquals( 1, query.list().size() ); - s.getTransaction().commit(); - s.close(); - } - - /** - * Updates two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleUpdates() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); - List results = (List) query.list(); - results.get( 0 ).name = "Different 0"; - results.get( 1 ).name = "Different 1"; - final String lastResultName = results.get( 0 ).name; - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM CharacterArrayIdTest$DemoEntity s" ); - results = (List) query.list(); - final Set names = new HashSet( ); - for ( DemoEntity entity : results ) { - names.add( entity.name ); - } - assertTrue( names.contains( "Different 0" ) ); - assertTrue( names.contains( "Different 1" ) ); - assertTrue( names.contains( lastResultName ) ); - s.getTransaction().commit(); - s.close(); - } - - - @Entity - @Table(name="DemoEntity") - public static class DemoEntity { - @Id - public Character[] id; - public String name; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java deleted file mode 100644 index 805581120c..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveByteArrayIdTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id.array; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.hibernate.query.Query; -import org.hibernate.Session; -import org.hibernate.dialect.MySQL5Dialect; -import org.hibernate.dialect.Oracle9iDialect; - -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author Piotr Krauzowicz - * @author Gail Badner - */ -@SkipForDialect(value = MySQL5Dialect.class, comment = "BLOB/TEXT column 'id' used in key specification without a key length") -@SkipForDialect(value = Oracle9iDialect.class, comment = "ORA-02329: column of datatype LOB cannot be unique or a primary key") -public class PrimitiveByteArrayIdTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { DemoEntity.class }; - } - - @Before - public void prepare() { - Session s = openSession(); - s.getTransaction().begin(); - for ( int i = 0; i < 3; i++ ) { - DemoEntity entity = new DemoEntity(); - entity.id = new byte[] { - (byte) ( i + 1 ), - (byte) ( i + 2 ), - (byte) ( i + 3 ), - (byte) ( i + 4 ) - }; - entity.name = "Simple name " + i; - s.persist( entity ); - } - s.getTransaction().commit(); - s.close(); - } - - @After - public void cleanup() { - Session s = openSession(); - s.getTransaction().begin(); - s.createQuery( "delete from PrimitiveByteArrayIdTest$DemoEntity" ).executeUpdate(); - s.getTransaction().commit(); - s.close(); - } - - /** - * Removes two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleDeletions() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); - List results = query.list(); - s.delete( results.get( 0 ) ); - s.delete( results.get( 1 ) ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); - assertEquals( 1, query.list().size() ); - s.getTransaction().commit(); - s.close(); - } - - /** - * Updates two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleUpdates() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); - List results = (List) query.list(); - results.get( 0 ).name = "Different 0"; - results.get( 1 ).name = "Different 1"; - final String lastResultName = results.get( 0 ).name; - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM PrimitiveByteArrayIdTest$DemoEntity s" ); - results = (List) query.list(); - final Set names = new HashSet( ); - for ( DemoEntity entity : results ) { - names.add( entity.name ); - } - assertTrue( names.contains( "Different 0" ) ); - assertTrue( names.contains( "Different 1" ) ); - assertTrue( names.contains( lastResultName ) ); - s.getTransaction().commit(); - s.close(); - } - - - @Entity - @Table(name="DemoEntity") - public static class DemoEntity { - @Id - public byte[] id; - public String name; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java deleted file mode 100644 index 33fc1bceff..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/array/PrimitiveCharacterArrayIdTest.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id.array; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.Table; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.hibernate.query.Query; -import org.hibernate.Session; -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author Piotr Krauzowicz - * @author Gail Badner - */ -public class PrimitiveCharacterArrayIdTest extends BaseCoreFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { DemoEntity.class }; - } - - @Before - public void prepare() { - Session s = openSession(); - s.getTransaction().begin(); - for ( int i = 0; i < 3; i++ ) { - DemoEntity entity = new DemoEntity(); - entity.id = new char[] { - (char) ( i + 'a' ), - (char) ( i + 'b' ), - (char) ( i + 'c' ), - (char) ( i + 'd' ) - }; - entity.name = "Simple name " + i; - s.persist( entity ); - } - s.getTransaction().commit(); - s.close(); - } - - @After - public void cleanup() { - Session s = openSession(); - s.getTransaction().begin(); - s.createQuery( "delete from PrimitiveCharacterArrayIdTest$DemoEntity" ).executeUpdate(); - s.getTransaction().commit(); - s.close(); - } - - /** - * Removes two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleDeletions() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); - List results = query.list(); - s.delete( results.get( 0 ) ); - s.delete( results.get( 1 ) ); - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); - assertEquals( 1, query.list().size() ); - s.getTransaction().commit(); - s.close(); - } - - /** - * Updates two records from database. - */ - @Test - @TestForIssue(jiraKey = "HHH-8999") - public void testMultipleUpdates() { - Session s = openSession(); - s.getTransaction().begin(); - Query query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); - List results = (List) query.list(); - results.get( 0 ).name = "Different 0"; - results.get( 1 ).name = "Different 1"; - final String lastResultName = results.get( 0 ).name; - s.getTransaction().commit(); - s.close(); - - s = openSession(); - s.getTransaction().begin(); - query = s.createQuery( "SELECT s FROM PrimitiveCharacterArrayIdTest$DemoEntity s" ); - results = (List) query.list(); - final Set names = new HashSet( ); - for ( DemoEntity entity : results ) { - names.add( entity.name ); - } - assertTrue( names.contains( "Different 0" ) ); - assertTrue( names.contains( "Different 1" ) ); - assertTrue( names.contains( lastResultName ) ); - s.getTransaction().commit(); - s.close(); - } - - - @Entity - @Table(name="DemoEntity") - public static class DemoEntity { - @Id - public char[] id; - public String name; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/OptimizerTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/sequence/OptimizerTest.java deleted file mode 100644 index 2486b0486d..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/id/sequence/OptimizerTest.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.id.sequence; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.SequenceGenerator; -import javax.persistence.Table; - -import org.hibernate.Session; - -import org.hibernate.testing.TestForIssue; -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - -/** - * @author Steve Ebersole - */ -public class OptimizerTest extends BaseNonConfigCoreFunctionalTestCase { - @Test - @TestForIssue( jiraKey = "HHH-10166" ) - public void testGenerationPastBound() { - Session session = openSession(); - session.getTransaction().begin(); - for (int i = 0; i < 100; i++) { - TheEntity entity = new TheEntity( Integer.toString( i ) ); - session.save( entity ); - } - session.getTransaction().commit(); - session.close(); - - session = openSession(); - session.getTransaction().begin(); - TheEntity number100 = session.get( TheEntity.class, 100 ); - assertThat( number100, notNullValue() ); - session.createQuery( "delete TheEntity" ).executeUpdate(); - session.getTransaction().commit(); - session.close(); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { TheEntity.class }; - } - - @Entity( name = "TheEntity" ) - @Table( name = "TheEntity" ) - public static class TheEntity { - @Id - @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "seq1" ) - @SequenceGenerator( name = "seq1", sequenceName = "the_sequence" ) - public Integer id; - public String someString; - - public TheEntity() { - } - - public TheEntity(String someString) { - this.someString = someString; - } - } -}