1616import org .hibernate .boot .model .relational .ExportableProducer ;
1717import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
1818import org .hibernate .engine .spi .SharedSessionContractImplementor ;
19+ import org .hibernate .generator .BeforeExecutionGenerator ;
1920import org .hibernate .id .factory .spi .StandardGenerator ;
2021import org .hibernate .property .access .spi .Setter ;
2122import 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 }
0 commit comments