diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java index 1b97bd76e5..4a1dfdde5c 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/PropertyBinder.java @@ -574,22 +574,27 @@ public class PropertyBinder { private static void checkVersionGenerationAlways(XProperty property, Generator generator) { if ( property.isAnnotationPresent(Version.class) ) { - final GenerationTiming timing = generator.getGenerationTiming(); - if ( !timing.isAlways() ) { + if ( !generator.generatedOnInsert() ) { throw new AnnotationException("Property '" + property.getName() - + "' is annotated '@Version' but has a value generator with timing " + timing.name() - + " (the value generation timing must be ALWAYS)" + + "' is annotated '@Version' but has a 'Generator' which does not generate on inserts" + ); + } + if ( !generator.generatedOnUpdate() ) { + throw new AnnotationException("Property '" + property.getName() + + "' is annotated '@Version' but has a 'Generator' which does not generate on updates" ); } } } private static void checkIdGeneratorTiming(Class extends Annotation> annotationType, Generator generator) { - GenerationTiming timing = generator.getGenerationTiming(); - if ( timing != INSERT ) { + if ( !generator.generatedOnInsert() ) { throw new MappingException( "Annotation '" + annotationType - + "' is annotated 'IdGeneratorType' but the given 'InMemoryGenerator' has timing " + timing - + " (an id generator must having timing INSERT)"); + + "' is annotated 'IdGeneratorType' but the given 'Generator' does not generate on inserts"); + } + if ( generator.generatedOnUpdate() ) { + throw new MappingException( "Annotation '" + annotationType + + "' is annotated 'IdGeneratorType' but the given 'Generator' generates on updates (it must generate only on inserts)"); } } } diff --git a/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java b/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java index a58a355af8..dcc95b43bc 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java @@ -129,7 +129,7 @@ public final class Nullability { } private static boolean generated(Generator generator) { - return generator != null && generator.getGenerationTiming().isNotNever(); + return generator != null && generator.isNotNever(); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java index 98ca5dfeaf..88677d5fd8 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java @@ -19,8 +19,6 @@ import org.hibernate.tuple.GenerationTiming; import org.hibernate.tuple.InMemoryGenerator; import org.hibernate.type.Type; -import static org.hibernate.tuple.GenerationTiming.INSERT; - /** * A classic extension point from the very earliest days of Hibernate, * this interface is no longer the only way to generate identifiers. Any @@ -145,11 +143,19 @@ public interface IdentifierGenerator extends InMemoryGenerator, ExportableProduc } /** - * @return {@link GenerationTiming#INSERT} + * @return {@code true} */ @Override - default GenerationTiming getGenerationTiming() { - return INSERT; + default boolean generatedOnInsert() { + return true; + } + + /** + * @return {@code false} + */ + @Override + default boolean generatedOnUpdate() { + return false; } /** diff --git a/hibernate-core/src/main/java/org/hibernate/id/PostInsertIdentifierGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/PostInsertIdentifierGenerator.java index e7b6165e1d..ef9112a5fe 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/PostInsertIdentifierGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/PostInsertIdentifierGenerator.java @@ -13,8 +13,6 @@ import org.hibernate.type.Type; import java.util.Properties; -import static org.hibernate.tuple.GenerationTiming.INSERT; - /** * The counterpart of {@link IdentifierGenerator} for values generated by the database. * This interface is no longer the only way to handle database-generate identifiers. @@ -28,11 +26,19 @@ import static org.hibernate.tuple.GenerationTiming.INSERT; public interface PostInsertIdentifierGenerator extends InDatabaseGenerator, Configurable { /** - * @return {@link GenerationTiming#INSERT} + * @return {@code true} */ @Override - default GenerationTiming getGenerationTiming() { - return INSERT; + default boolean generatedOnInsert() { + return true; + } + + /** + * @return {@code false} + */ + @Override + default boolean generatedOnUpdate() { + return false; } /** diff --git a/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java b/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java index 270b10bb35..0e658fa1fe 100644 --- a/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java +++ b/hibernate-core/src/main/java/org/hibernate/id/uuid/UuidGenerator.java @@ -12,7 +12,6 @@ import java.util.UUID; import org.hibernate.HibernateException; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext; -import org.hibernate.tuple.GenerationTiming; import org.hibernate.tuple.InMemoryGenerator; import org.hibernate.type.descriptor.java.UUIDJavaType; import org.hibernate.type.descriptor.java.UUIDJavaType.ValueTransformer; @@ -60,9 +59,20 @@ public class UuidGenerator implements InMemoryGenerator { } } + /** + * @return {@code true} + */ @Override - public GenerationTiming getGenerationTiming() { - return GenerationTiming.INSERT; + public boolean generatedOnInsert() { + return true; + } + + /** + * @return {@code false} + */ + @Override + public boolean generatedOnUpdate() { + return false; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/GeneratedValueResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/GeneratedValueResolver.java index 575d07445a..81df94f539 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/GeneratedValueResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/GeneratedValueResolver.java @@ -20,22 +20,22 @@ import org.hibernate.tuple.InMemoryGenerator; */ @Incubating public interface GeneratedValueResolver { - static GeneratedValueResolver from( - Generator generator, - GenerationTiming requestedTiming, - int dbSelectionPosition) { - assert requestedTiming.isNotNever(); +// static GeneratedValueResolver from( +// Generator generator, +// GenerationTiming requestedTiming, +// int dbSelectionPosition) { +// assert requestedTiming.isNotNever(); +// +// if ( generator == null || !generator.getGenerationTiming().includes( requestedTiming ) ) { +// return NoGeneratedValueResolver.INSTANCE; +// } +// else { +// return generator.generatedByDatabase() +// ? new InDatabaseGeneratedValueResolver( requestedTiming, dbSelectionPosition ) // in-db generation (column-default, function, etc) +// : new InMemoryGeneratedValueResolver( (InMemoryGenerator) generator, requestedTiming ); +// } +// } - if ( generator == null || !generator.getGenerationTiming().includes( requestedTiming ) ) { - return NoGeneratedValueResolver.INSTANCE; - } - else { - return generator.generatedByDatabase() - ? new InDatabaseGeneratedValueResolver( requestedTiming, dbSelectionPosition ) // in-db generation (column-default, function, etc) - : new InMemoryGeneratedValueResolver( (InMemoryGenerator) generator, requestedTiming ); - } - } - - GenerationTiming getGenerationTiming(); +// GenerationTiming getGenerationTiming(); Object resolveGeneratedValue(Object[] row, Object entity, SharedSessionContractImplementor session, Object currentValue); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InDatabaseGeneratedValueResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InDatabaseGeneratedValueResolver.java index e31ff2361e..c338414389 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InDatabaseGeneratedValueResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InDatabaseGeneratedValueResolver.java @@ -18,18 +18,18 @@ import org.hibernate.tuple.GenerationTiming; */ @Internal public class InDatabaseGeneratedValueResolver implements GeneratedValueResolver { - private final GenerationTiming timing; +// private final GenerationTiming timing; private final int resultPosition; public InDatabaseGeneratedValueResolver(GenerationTiming timing, int resultPosition) { - this.timing = timing; +// this.timing = timing; this.resultPosition = resultPosition; } - @Override - public GenerationTiming getGenerationTiming() { - return timing; - } +// @Override +// public GenerationTiming getGenerationTiming() { +// return timing; +// } @Override public Object resolveGeneratedValue(Object[] row, Object entity, SharedSessionContractImplementor session, Object currentValue) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InMemoryGeneratedValueResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InMemoryGeneratedValueResolver.java index a64e83431f..000de9345f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InMemoryGeneratedValueResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/InMemoryGeneratedValueResolver.java @@ -18,18 +18,18 @@ import org.hibernate.tuple.InMemoryGenerator; */ @Internal public class InMemoryGeneratedValueResolver implements GeneratedValueResolver { - private final GenerationTiming generationTiming; +// private final GenerationTiming generationTiming; private final InMemoryGenerator valueGenerator; public InMemoryGeneratedValueResolver(InMemoryGenerator valueGenerator, GenerationTiming generationTiming) { this.valueGenerator = valueGenerator; - this.generationTiming = generationTiming; +// this.generationTiming = generationTiming; } - @Override - public GenerationTiming getGenerationTiming() { - return generationTiming; - } +// @Override +// public GenerationTiming getGenerationTiming() { +// return generationTiming; +// } @Override public Object resolveGeneratedValue(Object[] row, Object entity, SharedSessionContractImplementor session, Object currentValue) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/GeneratedValuesProcessor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/GeneratedValuesProcessor.java index ca701a5fe0..12860dc0f9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/GeneratedValuesProcessor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/GeneratedValuesProcessor.java @@ -11,8 +11,6 @@ import java.util.List; import org.hibernate.Incubating; import org.hibernate.LockOptions; -import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; -import org.hibernate.engine.jdbc.spi.JdbcServices; import org.hibernate.engine.spi.LoadQueryInfluencers; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; @@ -25,7 +23,6 @@ import org.hibernate.metamodel.mapping.InDatabaseGeneratedValueResolver; import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryParameterBindings; import org.hibernate.sql.ast.Clause; -import org.hibernate.sql.ast.SqlAstTranslatorFactory; import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.exec.internal.JdbcParameterBindingsImpl; @@ -33,7 +30,6 @@ import org.hibernate.sql.exec.spi.Callback; import org.hibernate.sql.exec.spi.ExecutionContext; import org.hibernate.sql.exec.spi.JdbcOperationQuerySelect; import org.hibernate.sql.exec.spi.JdbcParameterBindings; -import org.hibernate.sql.results.spi.ListResultsConsumer; import org.hibernate.tuple.GenerationTiming; import org.hibernate.tuple.Generator; @@ -100,7 +96,7 @@ public class GeneratedValuesProcessor { final Generator generator = generators[ mapping.getStateArrayPosition() ]; if ( generator != null && generator.generatedByDatabase() - && generator.getGenerationTiming().isNotNever() ) { + && generator.isNotNever() ) { // this attribute is generated for the timing we are processing... valueDescriptors.add( new GeneratedValueDescriptor( new InDatabaseGeneratedValueResolver( timing, generatedValuesToSelect.size() ), diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NoGeneratedValueResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NoGeneratedValueResolver.java index 6cd1232c26..4ed62cd083 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NoGeneratedValueResolver.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NoGeneratedValueResolver.java @@ -20,10 +20,10 @@ public class NoGeneratedValueResolver implements GeneratedValueResolver { */ public static final NoGeneratedValueResolver INSTANCE = new NoGeneratedValueResolver(); - @Override - public GenerationTiming getGenerationTiming() { - return GenerationTiming.NEVER; - } +// @Override +// public GenerationTiming getGenerationTiming() { +// return GenerationTiming.NEVER; +// } @Override public Object resolveGeneratedValue(Object[] row, Object entity, SharedSessionContractImplementor session, Object currentValue) { diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index a33a221027..396da6dbd3 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -2773,7 +2773,7 @@ public abstract class AbstractEntityPersister else { final Generator generator = attributeMapping.getGenerator(); if ( generator!=null - && generator.getGenerationTiming().includesUpdate() + && generator.generatedOnUpdate() && generator.generatedByDatabase() ) { final InDatabaseGenerator databaseGenerator = (InDatabaseGenerator) generator; final Dialect dialect = getFactory().getJdbcServices().getDialect(); diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/InsertCoordinator.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/InsertCoordinator.java index 2ee719eb62..cd6c226e2c 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/InsertCoordinator.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/InsertCoordinator.java @@ -106,7 +106,7 @@ public class InsertCoordinator extends AbstractMutationCoordinator { Generator generator = generators[i]; if ( generator != null && !generator.generatedByDatabase() - && generator.getGenerationTiming().includesInsert() ) { + && generator.generatedOnInsert() ) { values[i] = ( (InMemoryGenerator) generator ).generate( session, entity, values[i] ); entityPersister().setPropertyValue( entity, i, values[i] ); } @@ -438,7 +438,7 @@ public class InsertCoordinator extends AbstractMutationCoordinator { private static boolean isValueGenerationInSql(Generator generator, Dialect dialect) { return generator != null - && generator.getGenerationTiming().includesInsert() + && generator.generatedOnInsert() && generator.generatedByDatabase() && ( (InDatabaseGenerator) generator ).referenceColumnsInSql(dialect); } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/UpdateCoordinatorStandard.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/UpdateCoordinatorStandard.java index 6868553344..2c13cad3b1 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/UpdateCoordinatorStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/UpdateCoordinatorStandard.java @@ -337,14 +337,14 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple private boolean isValueGenerationInSql(Generator generator, Dialect dialect) { return generator != null - && generator.getGenerationTiming().includesUpdate() + && generator.generatedOnUpdate() && generator.generatedByDatabase() && ((InDatabaseGenerator) generator).referenceColumnsInSql(dialect); } private boolean isValueGenerationInSqlNoWrite(Generator generator, Dialect dialect) { return generator != null - && generator.getGenerationTiming().includesUpdate() + && generator.generatedOnUpdate() && generator.generatedByDatabase() && ((InDatabaseGenerator) generator).referenceColumnsInSql(dialect) && !((InDatabaseGenerator) generator).writePropertyValue(); @@ -451,7 +451,7 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple Generator generator = generators[i]; if ( generator != null && !generator.generatedByDatabase() - && generator.getGenerationTiming().includesUpdate() ) { + && generator.generatedOnUpdate() ) { newValues[i] = ( (InMemoryGenerator) generator ).generate( session, object, newValues[i] ); entityPersister().setPropertyValue( object, i, newValues[i] ); fieldsPreUpdateNeeded[count++] = i; diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedAlwaysValueGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedAlwaysValueGeneration.java index cb3c6c43f9..e6fa92cd57 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedAlwaysValueGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedAlwaysValueGeneration.java @@ -20,8 +20,13 @@ public class GeneratedAlwaysValueGeneration public GeneratedAlwaysValueGeneration() {} @Override - public GenerationTiming getGenerationTiming() { - return GenerationTiming.ALWAYS; + public boolean generatedOnUpdate() { + return true; + } + + @Override + public boolean generatedOnInsert() { + return true; } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedValueGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedValueGeneration.java index 0e76fbbd70..d00012004d 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedValueGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/GeneratedValueGeneration.java @@ -9,8 +9,6 @@ package org.hibernate.tuple; import org.hibernate.annotations.Generated; import org.hibernate.dialect.Dialect; -import java.lang.reflect.Member; - import static org.hibernate.internal.util.StringHelper.isEmpty; /** @@ -41,8 +39,13 @@ public class GeneratedValueGeneration implements InDatabaseGenerator { } @Override - public GenerationTiming getGenerationTiming() { - return timing; + public boolean generatedOnInsert() { + return timing.includesInsert(); + } + + @Override + public boolean generatedOnUpdate() { + return timing.includesUpdate(); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/Generator.java b/hibernate-core/src/main/java/org/hibernate/tuple/Generator.java index 167dafed4a..3d2b5f1da0 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/Generator.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/Generator.java @@ -48,19 +48,6 @@ import java.io.Serializable; * @since 6.2 */ public interface Generator extends Serializable { - /** - * Specifies that the property value is generated: - *