Skip to content

Commit dd64446

Browse files
committed
HHH-19320 Pass current entity identifier value to generate method
1 parent 6ee6aee commit dd64446

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

hibernate-core/src/main/java/org/hibernate/event/internal/AbstractSaveEventListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ private static Object generateId(
152152
EventSource source,
153153
BeforeExecutionGenerator generator,
154154
EntityPersister persister) {
155-
final Object id = generator.generate( source, entity, null, INSERT );
155+
final Object currentValue = generator.allowAssignedIdentifiers() ? persister.getIdentifier( entity, source ) : null;
156+
final Object id = generator.generate( source, entity, currentValue, INSERT );
156157
if ( id == null ) {
157158
throw new IdentifierGenerationException( "Null id generated for entity '" + persister.getEntityName() + "'" );
158159
}

hibernate-core/src/main/java/org/hibernate/id/CompositeNestedGeneratedValueGenerator.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
import org.hibernate.boot.model.relational.ExportableProducer;
1717
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
1818
import org.hibernate.engine.spi.SharedSessionContractImplementor;
19+
import org.hibernate.generator.BeforeExecutionGenerator;
1920
import org.hibernate.id.factory.spi.StandardGenerator;
2021
import org.hibernate.property.access.spi.Setter;
2122
import org.hibernate.type.CompositeType;
2223

24+
import static org.hibernate.generator.EventType.INSERT;
25+
2326
/**
2427
* For composite identifiers, defines a number of "nested" generations that
2528
* need to happen to "fill" the identifier property(s).
@@ -74,7 +77,6 @@ public interface GenerationContextLocator {
7477
* determined {@linkplain GenerationContextLocator#locateGenerationContext context}
7578
*/
7679
public interface GenerationPlan extends ExportableProducer {
77-
7880
/**
7981
* Initializes this instance, in particular pre-generates SQL as necessary.
8082
* <p>
@@ -85,12 +87,9 @@ public interface GenerationPlan extends ExportableProducer {
8587
void initialize(SqlStringGenerationContext context);
8688

8789
/**
88-
* Execute the value generation.
89-
*
90-
* @param session The current session
91-
* @param incomingObject The entity for which we are generating id
90+
* Retrieve the generator for this generation plan
9291
*/
93-
Object execute(SharedSessionContractImplementor session, Object incomingObject);
92+
BeforeExecutionGenerator getGenerator();
9493

9594
/**
9695
* Returns the {@link Setter injector} for the generated property.
@@ -132,7 +131,17 @@ public Object generate(SharedSessionContractImplementor session, Object object)
132131
null :
133132
new ArrayList<>( generationPlans.size() );
134133
for ( GenerationPlan generationPlan : generationPlans ) {
135-
final Object generated = generationPlan.execute( session, object );
134+
final BeforeExecutionGenerator generator = generationPlan.getGenerator();
135+
final Object generated;
136+
if ( !generator.generatedOnExecution( object, session ) ) {
137+
final Object currentValue = generator.allowAssignedIdentifiers()
138+
? compositeType.getPropertyValue( context, generationPlan.getPropertyIndex(), session )
139+
: null;
140+
generated = generator.generate( session, object, currentValue, INSERT );
141+
}
142+
else {
143+
throw new IdentifierGenerationException( "Identity generation isn't supported for composite ids" );
144+
}
136145
if ( generatedValues != null ) {
137146
generatedValues.add( generated );
138147
}

hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ public Object insert(String entityName, Object entity) {
131131
}
132132
final Generator generator = persister.getGenerator();
133133
if ( !generator.generatedOnExecution( entity, this ) ) {
134-
id = ( (BeforeExecutionGenerator) generator).generate( this, entity, null, INSERT );
134+
final Object currentValue = generator.allowAssignedIdentifiers() ? persister.getIdentifier( entity, this ) : null;
135+
id = ( (BeforeExecutionGenerator) generator ).generate( this, entity, currentValue, INSERT );
135136
if ( firePreInsert(entity, id, state, persister) ) {
136137
return id;
137138
}

hibernate-core/src/main/java/org/hibernate/mapping/Component.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262

6363
import static java.util.Collections.unmodifiableList;
6464
import static java.util.stream.Collectors.toList;
65-
import static org.hibernate.generator.EventType.INSERT;
6665
import static org.hibernate.internal.util.StringHelper.qualify;
6766
import static org.hibernate.mapping.MappingHelper.checkPropertyColumnDuplication;
6867
import static org.hibernate.metamodel.mapping.EntityDiscriminatorMapping.DISCRIMINATOR_ROLE_NAME;
@@ -798,10 +797,9 @@ public int getPropertyIndex() {
798797
}
799798

800799
@Override
801-
public Object execute(SharedSessionContractImplementor session, Object incomingObject) {
802-
if ( !subgenerator.generatedOnExecution( incomingObject, session ) ) {
803-
return ( (BeforeExecutionGenerator) subgenerator)
804-
.generate( session, incomingObject, null, INSERT );
800+
public BeforeExecutionGenerator getGenerator() {
801+
if ( subgenerator instanceof BeforeExecutionGenerator ) {
802+
return (BeforeExecutionGenerator) subgenerator;
805803
}
806804
else {
807805
throw new IdentifierGenerationException( "Identity generation isn't supported for composite ids" );

0 commit comments

Comments
 (0)