Skip to content

Commit 5d22930

Browse files
committed
HHH-19320 Pass current entity identifier value to generate method
1 parent 8ccb21a commit 5d22930

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

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

+2-1
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 ) : 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
}

Diff for: hibernate-core/src/main/java/org/hibernate/id/CompositeNestedGeneratedValueGenerator.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
import org.hibernate.boot.model.relational.ExportableProducer;
1515
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
1616
import org.hibernate.engine.spi.SharedSessionContractImplementor;
17+
import org.hibernate.generator.BeforeExecutionGenerator;
1718
import org.hibernate.property.access.spi.Setter;
1819
import org.hibernate.type.CompositeType;
1920

21+
import static org.hibernate.generator.EventType.INSERT;
22+
2023
/**
2124
* For composite identifiers, defines a number of "nested" generations that
2225
* need to happen to "fill" the identifier property(s).
@@ -71,7 +74,6 @@ public interface GenerationContextLocator {
7174
* determined {@linkplain GenerationContextLocator#locateGenerationContext context}
7275
*/
7376
public interface GenerationPlan extends ExportableProducer {
74-
7577
/**
7678
* Initializes this instance, in particular pre-generates SQL as necessary.
7779
* <p>
@@ -82,12 +84,9 @@ public interface GenerationPlan extends ExportableProducer {
8284
void initialize(SqlStringGenerationContext context);
8385

8486
/**
85-
* Execute the value generation.
86-
*
87-
* @param session The current session
88-
* @param incomingObject The entity for which we are generating id
87+
* Retrieve the generator for this generation plan
8988
*/
90-
Object execute(SharedSessionContractImplementor session, Object incomingObject);
89+
BeforeExecutionGenerator getGenerator();
9190

9291
/**
9392
* Returns the {@link Setter injector} for the generated property.
@@ -129,7 +128,17 @@ public Object generate(SharedSessionContractImplementor session, Object object)
129128
null :
130129
new ArrayList<>( generationPlans.size() );
131130
for ( GenerationPlan generationPlan : generationPlans ) {
132-
final Object generated = generationPlan.execute( session, object );
131+
final BeforeExecutionGenerator generator = generationPlan.getGenerator();
132+
final Object generated;
133+
if ( generator.generatedBeforeExecution( object, session ) ) {
134+
final Object currentValue = generator.allowAssignedIdentifiers() ?
135+
compositeType.getPropertyValue( context, generationPlan.getPropertyIndex(), session ) :
136+
null;
137+
generated = generator.generate( session, object, currentValue, INSERT );
138+
}
139+
else {
140+
throw new IdentifierGenerationException( "Identity generation isn't supported for composite ids" );
141+
}
133142
if ( generatedValues != null ) {
134143
generatedValues.add( generated );
135144
}

Diff for: hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public Object insert(String entityName, Object entity) {
191191
if ( !generator.generatesOnInsert() ) {
192192
throw new IdentifierGenerationException( "Identifier generator must generate on insert" );
193193
}
194-
id = ( (BeforeExecutionGenerator) generator ).generate( this, entity, null, INSERT );
194+
final Object currentValue = generator.allowAssignedIdentifiers() ? persister.getIdentifier( entity ) : null;
195+
id = ( (BeforeExecutionGenerator) generator ).generate( this, entity, currentValue, INSERT );
195196
persister.setIdentifier( entity, id, this );
196197
if ( firePreInsert(entity, id, state, persister) ) {
197198
return id;

Diff for: hibernate-core/src/main/java/org/hibernate/mapping/Component.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555

5656
import static java.util.Collections.unmodifiableList;
5757
import static java.util.stream.Collectors.toList;
58-
import static org.hibernate.generator.EventType.INSERT;
5958
import static org.hibernate.internal.util.StringHelper.qualify;
6059
import static org.hibernate.mapping.MappingHelper.checkPropertyColumnDuplication;
6160
import static org.hibernate.mapping.MappingHelper.classForName;
@@ -790,13 +789,8 @@ public int getPropertyIndex() {
790789
}
791790

792791
@Override
793-
public Object execute(SharedSessionContractImplementor session, Object incomingObject) {
794-
if ( generator.generatedBeforeExecution( incomingObject, session ) ) {
795-
return generator.generate( session, incomingObject, null, INSERT );
796-
}
797-
else {
798-
throw new IdentifierGenerationException( "Identity generation isn't supported for composite ids" );
799-
}
792+
public BeforeExecutionGenerator getGenerator() {
793+
return generator;
800794
}
801795

802796
@Override

0 commit comments

Comments
 (0)