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 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: - * - * - * @return The {@link GenerationTiming} specifying when the value is generated. - */ - GenerationTiming getGenerationTiming(); - /** * Determines if the property value is generated in Java, or by the database. * @@ -68,4 +55,12 @@ public interface Generator extends Serializable { * generated in Java using a {@link ValueGenerator}. */ boolean generatedByDatabase(); + + boolean generatedOnInsert(); + + boolean generatedOnUpdate(); + + default boolean isNotNever() { + return generatedOnInsert() || generatedOnUpdate(); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/SourceGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/SourceGeneration.java index 2e4869bd2d..e01bae252c 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/SourceGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/SourceGeneration.java @@ -24,7 +24,6 @@ import java.sql.SQLException; import java.sql.Timestamp; import static java.sql.Types.TIMESTAMP; -import static org.hibernate.tuple.GenerationTiming.ALWAYS; /** * Value generation strategy using the query {@link Dialect#getCurrentTimestampSelectString()}. @@ -68,9 +67,20 @@ public class SourceGeneration implements InMemoryGenerator, ValueGenerator + *
  • {@linkplain GenerationTiming#INSERT when the entity is inserted}, + *
  • {@linkplain GenerationTiming#UPDATE when the entity is updated}, + *
  • {@linkplain GenerationTiming#ALWAYS whenever the entity is inserted or updated}, or + *
  • {@linkplain GenerationTiming#NEVER never}. + * + * + * @return The {@link GenerationTiming} specifying when the value is generated. + */ + GenerationTiming getGenerationTiming(); + + @Override + default boolean generatedOnInsert() { + return getGenerationTiming().includesInsert(); + } + + @Override + default boolean generatedOnUpdate() { + return getGenerationTiming().includesUpdate(); + } + /** * Obtain the {@linkplain ValueGenerator Java value generator}, if the value is generated in * Java, or return {@code null} if the value is generated by the database. diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java index 3ef580336e..86ce1efbca 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java @@ -44,11 +44,23 @@ public class VmValueGeneration implements InMemoryGenerator { : annotation.timing(); } + /** + * @return {@code true} + */ @Override - public GenerationTiming getGenerationTiming() { - return generationTiming; + public boolean generatedOnInsert() { + return generationTiming.includesInsert(); } + /** + * @return {@code false} + */ + @Override + public boolean generatedOnUpdate() { + return generationTiming.includesUpdate(); + } + + @Override public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue) { return generator.generateValue( (Session) session, owner, currentValue ); diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java b/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java index a9cc3f1ae1..95b87e5af3 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/entity/EntityMetamodel.java @@ -42,7 +42,6 @@ import org.hibernate.metamodel.mapping.EntityMappingType; import org.hibernate.metamodel.spi.RuntimeModelCreationContext; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.spi.PersisterCreationContext; -import org.hibernate.tuple.GenerationTiming; import org.hibernate.tuple.Generator; import org.hibernate.tuple.IdentifierProperty; import org.hibernate.tuple.InDatabaseGenerator; @@ -310,34 +309,21 @@ public class EntityMetamodel implements Serializable { propertyInsertability[i] = false; propertyUpdateability[i] = false; } - // we have some level of generation indicated - switch ( generator.getGenerationTiming() ) { - case INSERT: - if ( generator.generatedByDatabase() ) { - foundPostInsertGeneratedValues = true; - } - else { - foundPreInsertGeneratedValues = true; - } - break; - case UPDATE: - if ( generator.generatedByDatabase() ) { - foundPostUpdateGeneratedValues = true; - } - else { - foundPreUpdateGeneratedValues = true; - } - break; - case ALWAYS: - if ( generator.generatedByDatabase() ) { - foundPostInsertGeneratedValues = true; - foundPostUpdateGeneratedValues = true; - } - else { - foundPreInsertGeneratedValues = true; - foundPreUpdateGeneratedValues = true; - } - break; + if ( generator.generatedOnInsert() ) { + if ( generator.generatedByDatabase() ) { + foundPostInsertGeneratedValues = true; + } + else { + foundPreInsertGeneratedValues = true; + } + } + if ( generator.generatedOnUpdate() ) { + if ( generator.generatedByDatabase() ) { + foundPostUpdateGeneratedValues = true; + } + else { + foundPreUpdateGeneratedValues = true; + } } } @@ -468,7 +454,7 @@ public class EntityMetamodel implements Serializable { final GeneratorCreator generatorCreator = mappingProperty.getValueGeneratorCreator(); if ( generatorCreator != null ) { final Generator generator = mappingProperty.createGenerator( context ); - if ( generator.getGenerationTiming().isNotNever() ) { + if ( generator.isNotNever() ) { return generator; } } @@ -524,7 +510,7 @@ public class EntityMetamodel implements Serializable { } private void add(InMemoryGenerator inMemoryStrategy) { - if ( inMemoryStrategy.getGenerationTiming().isNotNever() ) { + if ( inMemoryStrategy.isNotNever() ) { hadInMemoryGeneration = true; } } @@ -535,7 +521,7 @@ public class EntityMetamodel implements Serializable { } inDatabaseStrategies.add( inDatabaseStrategy ); - if ( inDatabaseStrategy.getGenerationTiming().isNotNever() ) { + if ( inDatabaseStrategy.isNotNever() ) { hadInDatabaseGeneration = true; } } @@ -563,7 +549,8 @@ public class EntityMetamodel implements Serializable { } // the base-line values for the aggregated InDatabaseValueGenerationStrategy we will build here. - GenerationTiming timing = GenerationTiming.NEVER; + boolean generatedOnInsert = false; + boolean generatedOnUpdate = false; boolean referenceColumns = false; String[] columnValues = new String[ composite.getColumnSpan() ]; @@ -573,29 +560,8 @@ public class EntityMetamodel implements Serializable { for ( Property property : composite.getProperties() ) { propertyIndex++; final InDatabaseGenerator subStrategy = inDatabaseStrategies.get( propertyIndex ); - switch ( subStrategy.getGenerationTiming() ) { - case INSERT: - switch ( timing ) { - case UPDATE: - timing = GenerationTiming.ALWAYS; - break; - case NEVER: - timing = GenerationTiming.INSERT; - break; - } - break; - case UPDATE: - switch ( timing ) { - case INSERT: - timing = GenerationTiming.ALWAYS; - break; - case NEVER: - timing = GenerationTiming.UPDATE; - } - break; - case ALWAYS: - timing = GenerationTiming.ALWAYS; - } + generatedOnUpdate = generatedOnUpdate || subStrategy.generatedOnUpdate(); + generatedOnInsert = generatedOnInsert || subStrategy.generatedOnInsert(); if ( subStrategy.referenceColumnsInSql(dialect) ) { // override base-line value referenceColumns = true; @@ -619,13 +585,17 @@ public class EntityMetamodel implements Serializable { } // then use the aggregated values to build the InDatabaseValueGenerationStrategy - return new InDatabaseGeneratorImpl( timing, referenceColumns, columnValues ); + return new InDatabaseGeneratorImpl( generatedOnUpdate, generatedOnInsert, referenceColumns, columnValues ); } else { return new Generator() { @Override - public GenerationTiming getGenerationTiming() { - return GenerationTiming.NEVER; + public boolean generatedOnInsert() { + return false; + } + @Override + public boolean generatedOnUpdate() { + return false; } @Override public boolean generatedByDatabase() { @@ -637,22 +607,30 @@ public class EntityMetamodel implements Serializable { } private static class InDatabaseGeneratorImpl implements InDatabaseGenerator { - private final GenerationTiming timing; + private final boolean generatedOnUpdate; + private final boolean generatedOnInsert; private final boolean referenceColumnInSql; private final String[] referencedColumnValues; private InDatabaseGeneratorImpl( - GenerationTiming timing, + boolean generatedOnUpdate, + boolean generatedOnInsert, boolean referenceColumnInSql, String[] referencedColumnValues) { - this.timing = timing; + this.generatedOnUpdate = generatedOnUpdate; + this.generatedOnInsert = generatedOnInsert; this.referenceColumnInSql = referenceColumnInSql; this.referencedColumnValues = referencedColumnValues; } @Override - public GenerationTiming getGenerationTiming() { - return timing; + public boolean generatedOnInsert() { + return generatedOnInsert; + } + + @Override + public boolean generatedOnUpdate() { + return generatedOnUpdate; } @Override @@ -694,17 +672,17 @@ public class EntityMetamodel implements Serializable { // * That code checks that there is a natural identifier before making this call, so we assume the same here // * That code assumes a non-composite natural-id, so we assume the same here final Generator strategy = generators[ naturalIdPropertyNumbers[0] ]; - return strategy != null && strategy.getGenerationTiming().isNotNever(); + return strategy != null && strategy.isNotNever(); } public boolean isVersionGeneratedByDatabase() { final Generator strategy = generators[ versionPropertyIndex ]; - return strategy != null && strategy.getGenerationTiming().isNotNever() && strategy.generatedByDatabase(); + return strategy != null && strategy.isNotNever() && strategy.generatedByDatabase(); } public boolean isVersionGeneratedInMemory() { final Generator strategy = generators[ versionPropertyIndex ]; - return strategy != null && strategy.getGenerationTiming().isNotNever() && !strategy.generatedByDatabase(); + return strategy != null && strategy.isNotNever() && !strategy.generatedByDatabase(); } public int[] getNaturalIdentifierProperties() { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/temporals/GeneratedUuidTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/temporals/GeneratedUuidTests.java index 5eecf3451f..ee0e687126 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/temporals/GeneratedUuidTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/generated/temporals/GeneratedUuidTests.java @@ -91,8 +91,13 @@ public class GeneratedUuidTests { } @Override - public GenerationTiming getGenerationTiming() { - return timing; + public boolean generatedOnInsert() { + return timing.includesInsert(); + } + + @Override + public boolean generatedOnUpdate() { + return timing.includesUpdate(); } @Override diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AuditMetadataGenerator.java b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AuditMetadataGenerator.java index 2674facb6b..e51506c94f 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AuditMetadataGenerator.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/configuration/internal/metadata/AuditMetadataGenerator.java @@ -123,7 +123,7 @@ public final class AuditMetadataGenerator extends AbstractMetadataGenerator { final GeneratorCreator generation = property.getValueGeneratorCreator(); if ( generation instanceof GeneratedValueGeneration ) { final GeneratedValueGeneration valueGeneration = (GeneratedValueGeneration) generation; - if ( valueGeneration.getGenerationTiming().includesInsert() ) { + if ( valueGeneration.generatedOnInsert() ) { return true; } }