1414import org .hibernate .boot .model .relational .ExportableProducer ;
1515import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
1616import org .hibernate .engine .spi .SharedSessionContractImplementor ;
17+ import org .hibernate .generator .BeforeExecutionGenerator ;
1718import org .hibernate .property .access .spi .Setter ;
1819import 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 }
0 commit comments