getEventTypes() {
+ return INSERT_AND_UPDATE;
+ }
+
+ @Override
+ public Object generate(SharedSessionContractImplementor session, Object owner, Object current, EventType eventType) {
+ return eventType == INSERT
+ ? seed( versionMapping, session )
+ : increment( current, versionMapping, session );
+ }
+}
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 829f7bf97b..08adc1521d 100644
--- a/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java
+++ b/hibernate-core/src/main/java/org/hibernate/id/IdentifierGenerator.java
@@ -142,7 +142,7 @@ public interface IdentifierGenerator extends InMemoryGenerator, ExportableProduc
* The {@code currentValue} is usually null for id generation.
*/
@Override
- default Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue) {
+ default Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
return generate( session, owner );
}
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 8c462ab914..bd16f3a561 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
@@ -72,7 +72,7 @@ public class UuidGenerator implements InMemoryGenerator {
}
@Override
- public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue) {
+ public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
return valueTransformer.transform( generator.generateUuid( session ) );
}
}
diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java
index 775e01e72e..ff6bf0f620 100644
--- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java
+++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java
@@ -20,7 +20,6 @@ import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.StatefulPersistenceContext;
-import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.PersistenceContext;
@@ -41,6 +40,10 @@ import jakarta.transaction.SystemException;
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
+import static org.hibernate.engine.internal.Versioning.incrementVersion;
+import static org.hibernate.engine.internal.Versioning.seedVersion;
+import static org.hibernate.engine.internal.Versioning.setVersion;
+import static org.hibernate.generator.EventType.INSERT;
/**
* Concrete implementation of the {@link StatelessSession} API.
@@ -97,17 +100,11 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
final EntityPersister persister = getEntityPersister( entityName, entity );
final Object id;
final Object[] state = persister.getValues( entity );
- Generator generator = persister.getGenerator();
+ final Generator generator = persister.getGenerator();
if ( !generator.generatedByDatabase() ) {
- id = ( (InMemoryGenerator) generator).generate( this, entity, null );
+ id = ( (InMemoryGenerator) generator).generate( this, entity, null, INSERT );
if ( persister.isVersioned() ) {
- boolean substitute = Versioning.seedVersion(
- state,
- persister.getVersionProperty(),
- persister.getVersionMapping(),
- this
- );
- if ( substitute ) {
+ if ( seedVersion( entity, state, persister, this ) ) {
persister.setValues( entity, state );
}
}
@@ -156,8 +153,8 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
Object oldVersion;
if ( persister.isVersioned() ) {
oldVersion = persister.getVersion( entity );
- Object newVersion = Versioning.increment( oldVersion, persister.getVersionMapping(), this );
- Versioning.setVersion( state, newVersion, persister );
+ Object newVersion = incrementVersion( entity, oldVersion, persister, this );
+ setVersion( state, newVersion, persister );
persister.setValues( entity, state );
}
else {
diff --git a/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java b/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java
index 985f3843f5..c84da15a13 100644
--- a/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java
+++ b/hibernate-core/src/main/java/org/hibernate/loader/entity/CacheEntityLoaderHelper.java
@@ -18,7 +18,6 @@ import org.hibernate.cache.spi.entry.StandardCacheEntryImpl;
import org.hibernate.engine.internal.CacheHelper;
import org.hibernate.engine.internal.StatefulPersistenceContext;
import org.hibernate.engine.internal.TwoPhaseLoad;
-import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
@@ -45,6 +44,7 @@ import org.hibernate.type.TypeHelper;
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isManagedEntity;
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
+import static org.hibernate.engine.internal.Versioning.getVersion;
import static org.hibernate.loader.ast.internal.LoaderHelper.upgradeLock;
/**
@@ -424,7 +424,7 @@ public class CacheEntityLoaderHelper {
source
);
}
- version = Versioning.getVersion( values, subclassPersister );
+ version = getVersion( values, subclassPersister );
LOG.tracef( "Cached Version : %s", version );
final Object proxy = persistenceContext.getProxy( entityKey );
diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Component.java b/hibernate-core/src/main/java/org/hibernate/mapping/Component.java
index 07d9e5367d..aa72e40fe1 100644
--- a/hibernate-core/src/main/java/org/hibernate/mapping/Component.java
+++ b/hibernate-core/src/main/java/org/hibernate/mapping/Component.java
@@ -28,7 +28,6 @@ import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.CompositeNestedGeneratedValueGenerator;
import org.hibernate.id.IdentifierGenerator;
-import org.hibernate.id.IdentifierGeneratorHelper;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
@@ -41,6 +40,9 @@ import org.hibernate.type.ComponentType;
import org.hibernate.type.EmbeddedComponentType;
import org.hibernate.type.Type;
+import static org.hibernate.generator.EventType.INSERT;
+import static org.hibernate.id.IdentifierGeneratorHelper.POST_INSERT_INDICATOR;
+
/**
* A mapping model object that represents an {@linkplain jakarta.persistence.Embeddable embeddable class}.
*
@@ -508,38 +510,36 @@ public class Component extends SimpleValue implements MetaAttributable, Sortable
}
public static class ValueGenerationPlan implements CompositeNestedGeneratedValueGenerator.GenerationPlan {
- private final Generator subGenerator;
+ private final Generator subgenerator;
private final Setter injector;
- public ValueGenerationPlan(
- Generator subGenerator,
- Setter injector) {
- this.subGenerator = subGenerator;
+ public ValueGenerationPlan(Generator subgenerator, Setter injector) {
+ this.subgenerator = subgenerator;
this.injector = injector;
}
@Override
public void execute(SharedSessionContractImplementor session, Object incomingObject, Object injectionContext) {
- if ( !subGenerator.generatedByDatabase() ) {
- Object generatedId = ( (InMemoryGenerator) subGenerator ).generate( session, incomingObject, null );
+ if ( !subgenerator.generatedByDatabase() ) {
+ Object generatedId = ( (InMemoryGenerator) subgenerator).generate( session, incomingObject, null, INSERT );
injector.set( injectionContext, generatedId );
}
else {
- injector.set( injectionContext, IdentifierGeneratorHelper.POST_INSERT_INDICATOR );
+ injector.set( injectionContext, POST_INSERT_INDICATOR );
}
}
@Override
public void registerExportables(Database database) {
- if ( subGenerator instanceof ExportableProducer ) {
- ( (ExportableProducer) subGenerator ).registerExportables( database );
+ if ( subgenerator instanceof ExportableProducer ) {
+ ( (ExportableProducer) subgenerator).registerExportables( database );
}
}
@Override
public void initialize(SqlStringGenerationContext context) {
- if ( subGenerator instanceof IdentifierGenerator ) {
- ( (IdentifierGenerator) subGenerator ).initialize( context );
+ if ( subgenerator instanceof IdentifierGenerator ) {
+ ( (IdentifierGenerator) subgenerator).initialize( context );
}
}
}
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 c338414389..c6fc60298c 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
@@ -8,7 +8,7 @@ package org.hibernate.metamodel.mapping;
import org.hibernate.Internal;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
-import org.hibernate.tuple.GenerationTiming;
+import org.hibernate.generator.EventType;
/**
* GeneratedValueResolver impl for in-db generation. It extracts the generated value
@@ -18,11 +18,11 @@ import org.hibernate.tuple.GenerationTiming;
*/
@Internal
public class InDatabaseGeneratedValueResolver implements GeneratedValueResolver {
-// private final GenerationTiming timing;
+ private final EventType eventType;
private final int resultPosition;
- public InDatabaseGeneratedValueResolver(GenerationTiming timing, int resultPosition) {
-// this.timing = timing;
+ public InDatabaseGeneratedValueResolver(EventType eventType, int resultPosition) {
+ this.eventType = eventType;
this.resultPosition = resultPosition;
}
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 8b031879c3..637bb8d779 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
@@ -8,7 +8,7 @@ package org.hibernate.metamodel.mapping;
import org.hibernate.Internal;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
-import org.hibernate.tuple.GenerationTiming;
+import org.hibernate.generator.EventType;
import org.hibernate.generator.InMemoryGenerator;
/**
@@ -18,12 +18,12 @@ import org.hibernate.generator.InMemoryGenerator;
*/
@Internal
public class InMemoryGeneratedValueResolver implements GeneratedValueResolver {
-// private final GenerationTiming generationTiming;
- private final InMemoryGenerator valueGenerator;
+ private final EventType eventType;
+ private final InMemoryGenerator generator;
- public InMemoryGeneratedValueResolver(InMemoryGenerator valueGenerator, GenerationTiming generationTiming) {
- this.valueGenerator = valueGenerator;
-// this.generationTiming = generationTiming;
+ public InMemoryGeneratedValueResolver(InMemoryGenerator generator, EventType eventType) {
+ this.generator = generator;
+ this.eventType = eventType;
}
// @Override
@@ -33,6 +33,6 @@ public class InMemoryGeneratedValueResolver implements GeneratedValueResolver {
@Override
public Object resolveGeneratedValue(Object[] row, Object entity, SharedSessionContractImplementor session, Object currentValue) {
- return valueGenerator.generate( session, entity, currentValue );
+ return generator.generate( session, entity, currentValue, eventType );
}
}
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 a77a899939..5ddff0c3ad 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
@@ -14,6 +14,7 @@ import org.hibernate.LockOptions;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
+import org.hibernate.generator.EventType;
import org.hibernate.generator.InDatabaseGenerator;
import org.hibernate.loader.ast.internal.LoaderSelectBuilder;
import org.hibernate.metamodel.UnsupportedMappingException;
@@ -31,7 +32,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.tuple.GenerationTiming;
import org.hibernate.generator.Generator;
import static org.hibernate.sql.results.spi.ListResultsConsumer.UniqueSemantic.FILTER;
@@ -59,7 +59,7 @@ public class GeneratedValuesProcessor {
public GeneratedValuesProcessor(
EntityMappingType entityDescriptor,
- GenerationTiming timing,
+ EventType timing,
SessionFactoryImplementor sessionFactory) {
this.entityDescriptor = entityDescriptor;
this.sessionFactory = sessionFactory;
@@ -88,7 +88,7 @@ public class GeneratedValuesProcessor {
* populate the list of {@link GeneratedValueDescriptor}s by side effect, and
* return a list of {@link AttributeMapping}s.
*/
- private List getGeneratedAttributes(EntityMappingType entityDescriptor, GenerationTiming timing) {
+ private List getGeneratedAttributes(EntityMappingType entityDescriptor, EventType timing) {
// todo (6.0): For now, we rely on the entity metamodel as composite attributes report
// GenerationTiming.NEVER even if they have attributes that would need generation
final Generator[] generators = entityDescriptor.getEntityPersister().getEntityMetamodel().getGenerators();
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 2fac9f7f8f..64c6017c3d 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
@@ -93,6 +93,9 @@ import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.LoadEvent;
+import org.hibernate.generator.EventType;
+import org.hibernate.generator.InMemoryGenerator;
+import org.hibernate.generator.internal.VersionGeneration;
import org.hibernate.id.Assigned;
import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
import org.hibernate.id.IdentifierGenerator;
@@ -259,7 +262,6 @@ import org.hibernate.sql.results.internal.SqlSelectionImpl;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.generator.Generator;
import org.hibernate.generator.InDatabaseGenerator;
-import org.hibernate.tuple.GenerationTiming;
import org.hibernate.tuple.NonIdentifierAttribute;
import org.hibernate.tuple.entity.EntityBasedAssociationAttribute;
import org.hibernate.tuple.entity.EntityMetamodel;
@@ -278,6 +280,8 @@ import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttrib
import static org.hibernate.engine.internal.ManagedTypeHelper.processIfPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.processIfSelfDirtinessTracker;
import static org.hibernate.engine.internal.Versioning.isVersionIncrementRequired;
+import static org.hibernate.generator.EventType.INSERT;
+import static org.hibernate.generator.EventType.UPDATE;
import static org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey;
/**
@@ -414,7 +418,6 @@ public abstract class AbstractEntityPersister
private final Map subclassPropertyAliases = new HashMap<>();
private final Map subclassPropertyColumnNames = new HashMap<>();
-
private final JavaType> javaType;
private final EntityRepresentationStrategy representationStrategy;
@@ -431,6 +434,8 @@ public abstract class AbstractEntityPersister
protected Map declaredAttributeMappings = new LinkedHashMap<>();
protected List staticFetchableList;
+ private InMemoryGenerator versionGenerator;
+
protected ReflectionOptimizer.AccessOptimizer accessOptimizer;
/**
@@ -490,10 +495,10 @@ public abstract class AbstractEntityPersister
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- this.representationStrategy = creationContext.getBootstrapContext().getRepresentationStrategySelector()
+ representationStrategy = creationContext.getBootstrapContext().getRepresentationStrategySelector()
.resolveStrategy( bootDescriptor, this, creationContext );
- this.javaType = representationStrategy.getLoadJavaType();
+ javaType = representationStrategy.getLoadJavaType();
assert javaType != null;
final JdbcServices jdbcServices = factory.getServiceRegistry().getService( JdbcServices.class );
@@ -1933,7 +1938,7 @@ public abstract class AbstractEntityPersister
return select.addCondition( rootTableKeyColumnNames, "=?" ).toStatementString();
}
- private GeneratedValuesProcessor createGeneratedValuesProcessor(GenerationTiming timing) {
+ private GeneratedValuesProcessor createGeneratedValuesProcessor(EventType timing) {
return new GeneratedValuesProcessor( this, timing, getFactory() );
}
@@ -3992,6 +3997,11 @@ public abstract class AbstractEntityPersister
return entityMetamodel.getIdentifierProperty().getGenerator();
}
+ @Override
+ public InMemoryGenerator getVersionGenerator() {
+ return versionGenerator;
+ }
+
@Override
public String getRootEntityName() {
return entityMetamodel.getRootName();
@@ -4771,11 +4781,11 @@ public abstract class AbstractEntityPersister
if ( superMappingType != null ) {
( (InFlightEntityMappingType) superMappingType ).prepareMappingModel( creationProcess );
if ( shouldProcessSuperMapping() ) {
- this.discriminatorMapping = superMappingType.getDiscriminatorMapping();
- this.identifierMapping = superMappingType.getIdentifierMapping();
- this.naturalIdMapping = superMappingType.getNaturalIdMapping();
- this.versionMapping = superMappingType.getVersionMapping();
- this.rowIdMapping = superMappingType.getRowIdMapping();
+ discriminatorMapping = superMappingType.getDiscriminatorMapping();
+ identifierMapping = superMappingType.getIdentifierMapping();
+ naturalIdMapping = superMappingType.getNaturalIdMapping();
+ versionMapping = superMappingType.getVersionMapping();
+ rowIdMapping = superMappingType.getRowIdMapping();
}
else {
prepareMappingModel( creationProcess, bootEntityDescriptor );
@@ -4787,10 +4797,17 @@ public abstract class AbstractEntityPersister
// rootEntityDescriptor = this;
}
- final EntityMetamodel currentEntityMetamodel = this.getEntityMetamodel();
+ final EntityMetamodel currentEntityMetamodel = getEntityMetamodel();
+
+ if ( currentEntityMetamodel.isVersioned() ) {
+ final InMemoryGenerator generator = currentEntityMetamodel.getVersionGenerator();
+ // need to do this here because EntityMetamodel doesn't have the EntityVersionMapping :-(
+ versionGenerator = generator == null ? new VersionGeneration( versionMapping ) : generator;
+ }
+
int stateArrayPosition = getStateArrayInitialPosition( creationProcess );
- NonIdentifierAttribute[] properties = currentEntityMetamodel.getProperties();
+ final NonIdentifierAttribute[] properties = currentEntityMetamodel.getProperties();
for ( int i = 0; i < currentEntityMetamodel.getPropertySpan(); i++ ) {
final NonIdentifierAttribute runtimeAttrDefinition = properties[i];
final Property bootProperty = bootEntityDescriptor.getProperty( runtimeAttrDefinition.getName() );
@@ -4826,7 +4843,6 @@ public abstract class AbstractEntityPersister
}
-
// register a callback for after all `#prepareMappingModel` calls have finished. here we want to delay the
// generation of `staticFetchableList` because we need to wait until after all sub-classes have had their
// `#prepareMappingModel` called (and their declared attribute mappings resolved)
@@ -4834,10 +4850,10 @@ public abstract class AbstractEntityPersister
"Entity(" + getEntityName() + ") `staticFetchableList` generator",
() -> {
if ( hasInsertGeneratedProperties() ) {
- insertGeneratedValuesProcessor = createGeneratedValuesProcessor( GenerationTiming.INSERT );
+ insertGeneratedValuesProcessor = createGeneratedValuesProcessor( INSERT );
}
if ( hasUpdateGeneratedProperties() ) {
- updateGeneratedValuesProcessor = createGeneratedValuesProcessor( GenerationTiming.UPDATE );
+ updateGeneratedValuesProcessor = createGeneratedValuesProcessor( UPDATE );
}
staticFetchableList = new ArrayList<>( attributeMappings.size() );
visitSubTypeAttributeMappings( attributeMapping -> staticFetchableList.add( attributeMapping ) );
@@ -4921,8 +4937,7 @@ public abstract class AbstractEntityPersister
identifierMapping = creationProcess.processSubPart(
EntityIdentifierMapping.ROLE_LOCAL_NAME,
- (role, process) ->
- generateIdentifierMapping( templateInstanceCreator, bootEntityDescriptor, process )
+ (role, process) -> generateIdentifierMapping( templateInstanceCreator, bootEntityDescriptor, process )
);
versionMapping = generateVersionMapping( templateInstanceCreator, bootEntityDescriptor, creationProcess );
diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java
index 38f003312e..3af2ade378 100644
--- a/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java
+++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/EntityPersister.java
@@ -27,6 +27,8 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.spi.EventSource;
+import org.hibernate.generator.InMemoryGenerator;
+import org.hibernate.generator.internal.VersionGeneration;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;
import org.hibernate.internal.FilterAliasGenerator;
@@ -464,6 +466,10 @@ public interface EntityPersister
return getIdentifierGenerator();
}
+ default InMemoryGenerator getVersionGenerator() {
+ return new VersionGeneration( getVersionMapping() );
+ }
+
@Override
default AttributeMapping getAttributeMapping(int position) {
return getAttributeMappings().get( position );
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 ca2dedb471..c309bf1138 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
@@ -19,6 +19,7 @@ import org.hibernate.engine.jdbc.mutation.TableInclusionChecker;
import org.hibernate.engine.jdbc.mutation.spi.MutationExecutorService;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
+import org.hibernate.generator.EventType;
import org.hibernate.id.insert.InsertGeneratedIdentifierDelegate;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.BasicEntityIdentifierMapping;
@@ -103,11 +104,11 @@ public class InsertCoordinator extends AbstractMutationCoordinator {
if ( entityMetamodel.hasPreInsertGeneratedValues() ) {
final Generator[] generators = entityMetamodel.getGenerators();
for ( int i = 0; i < generators.length; i++ ) {
- Generator generator = generators[i];
+ final Generator generator = generators[i];
if ( generator != null
&& !generator.generatedByDatabase()
&& generator.generatesOnInsert() ) {
- values[i] = ( (InMemoryGenerator) generator ).generate( session, entity, values[i] );
+ values[i] = ( (InMemoryGenerator) generator ).generate( session, entity, values[i], EventType.INSERT );
entityPersister().setPropertyValue( entity, i, values[i] );
}
}
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 33da84885e..065d2281f3 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
@@ -60,6 +60,7 @@ import static org.hibernate.engine.OptimisticLockStyle.DIRTY;
import static org.hibernate.engine.OptimisticLockStyle.NONE;
import static org.hibernate.engine.OptimisticLockStyle.VERSION;
import static org.hibernate.engine.internal.Versioning.isVersionIncrementRequired;
+import static org.hibernate.generator.EventType.UPDATE;
import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_INT_ARRAY;
/**
@@ -452,7 +453,7 @@ public class UpdateCoordinatorStandard extends AbstractMutationCoordinator imple
if ( generator != null
&& !generator.generatedByDatabase()
&& generator.generatesOnUpdate() ) {
- newValues[i] = ( (InMemoryGenerator) generator ).generate( session, object, newValues[i] );
+ newValues[i] = ( (InMemoryGenerator) generator ).generate( session, object, newValues[i], UPDATE );
entityPersister().setPropertyValue( object, i, newValues[i] );
fieldsPreUpdateNeeded[count++] = i;
}
diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/temptable/InsertExecutionDelegate.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/temptable/InsertExecutionDelegate.java
index 6542a00448..e1d310acf8 100644
--- a/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/temptable/InsertExecutionDelegate.java
+++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/mutation/internal/temptable/InsertExecutionDelegate.java
@@ -22,6 +22,7 @@ import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
+import org.hibernate.generator.EventType;
import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
import org.hibernate.id.OptimizableGenerator;
import org.hibernate.id.enhanced.Optimizer;
@@ -508,7 +509,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
rootIdentity,
new JdbcParameterBindingImpl(
identifierMapping.getJdbcMapping(),
- ( (InMemoryGenerator) generator ).generate( executionContext.getSession(), null, null )
+ ( (InMemoryGenerator) generator ).generate( executionContext.getSession(), null, null, EventType.INSERT )
)
);
jdbcServices.getJdbcMutationExecutor().execute(
diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java
index d860577a70..2278b1a9d7 100644
--- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java
+++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java
@@ -404,6 +404,7 @@ import jakarta.persistence.TemporalType;
import jakarta.persistence.metamodel.SingularAttribute;
import jakarta.persistence.metamodel.Type;
+import static org.hibernate.generator.EventType.INSERT;
import static org.hibernate.internal.util.NullnessHelper.coalesceSuppliedValues;
import static org.hibernate.query.sqm.BinaryArithmeticOperator.ADD;
import static org.hibernate.query.sqm.BinaryArithmeticOperator.MULTIPLY;
@@ -1464,7 +1465,7 @@ public abstract class BaseSqmToSqlAstConverter extends Base
ExecutionContext executionContext) throws SQLException {
getJdbcMapping().getJdbcValueBinder().bind(
statement,
- generator.generate( executionContext.getSession(), null, null ),
+ generator.generate( executionContext.getSession(), null, null, INSERT ),
startPosition,
executionContext.getSession()
);
diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/ValueGeneration.java b/hibernate-core/src/main/java/org/hibernate/tuple/ValueGeneration.java
index 3a7d12f31c..92921c46c9 100644
--- a/hibernate-core/src/main/java/org/hibernate/tuple/ValueGeneration.java
+++ b/hibernate-core/src/main/java/org/hibernate/tuple/ValueGeneration.java
@@ -55,7 +55,7 @@ public interface ValueGeneration extends InMemoryGenerator, InDatabaseGenerator
ValueGenerator> getValueGenerator();
@Override
- default Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue) {
+ default Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
return getValueGenerator().generateValue( (Session) session, owner, currentValue );
}
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 65aa40a086..ac2c1b3f6b 100644
--- a/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java
+++ b/hibernate-core/src/main/java/org/hibernate/tuple/VmValueGeneration.java
@@ -51,7 +51,7 @@ public class VmValueGeneration implements InMemoryGenerator {
}
@Override
- public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue) {
+ public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
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 70e374cbd4..769f30f2a3 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
@@ -139,6 +139,8 @@ public class EntityMetamodel implements Serializable {
private final Set subclassEntityNames;
private final Map,String> entityNameByInheritanceClassMap;
+ private final InMemoryGenerator versionGenerator;
+
private final BytecodeEnhancementMetadata bytecodeEnhancementMetadata;
@Deprecated(since = "6.0")
@@ -217,7 +219,7 @@ public class EntityMetamodel implements Serializable {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// generated value strategies ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- this.generators = new Generator[propertySpan];
+ generators = new Generator[propertySpan];
boolean foundPreInsertGeneratedValues = false;
boolean foundPreUpdateGeneratedValues = false;
@@ -233,6 +235,7 @@ public class EntityMetamodel implements Serializable {
BitSet mutableIndexes = new BitSet();
boolean foundNonIdentifierPropertyNamedId = false;
boolean foundUpdateableNaturalIdProperty = false;
+ InMemoryGenerator tempVersionGenerator = null;
List props = persistentClass.getPropertyClosure();
for ( int i=0; i 1 );
+ || ( getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()
+ && getBytecodeEnhancementMetadata().getLazyAttributesMetadata().getFetchGroupNames().size() > 1 );
dynamicInsert = persistentClass.useDynamicInsert();
polymorphic = persistentClass.isPolymorphic();
@@ -477,6 +492,10 @@ public class EntityMetamodel implements Serializable {
return generators;
}
+ public InMemoryGenerator getVersionGenerator() {
+ return versionGenerator;
+ }
+
public static class ValueGenerationStrategyException extends HibernateException {
public ValueGenerationStrategyException(String message) {
super( message );
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 5e0010c36a..4ba81ef721 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
@@ -98,7 +98,7 @@ public class GeneratedUuidTests {
}
@Override
- public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue) {
+ public Object generate(SharedSessionContractImplementor session, Object owner, Object currentValue, EventType eventType) {
return UUID.randomUUID();
}
}