16
16
import org .hibernate .boot .model .relational .ExportableProducer ;
17
17
import org .hibernate .boot .model .relational .SqlStringGenerationContext ;
18
18
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
19
+ import org .hibernate .generator .BeforeExecutionGenerator ;
19
20
import org .hibernate .id .factory .spi .StandardGenerator ;
20
21
import org .hibernate .property .access .spi .Setter ;
21
22
import org .hibernate .type .CompositeType ;
22
23
24
+ import static org .hibernate .generator .EventType .INSERT ;
25
+
23
26
/**
24
27
* For composite identifiers, defines a number of "nested" generations that
25
28
* need to happen to "fill" the identifier property(s).
@@ -74,7 +77,6 @@ public interface GenerationContextLocator {
74
77
* determined {@linkplain GenerationContextLocator#locateGenerationContext context}
75
78
*/
76
79
public interface GenerationPlan extends ExportableProducer {
77
-
78
80
/**
79
81
* Initializes this instance, in particular pre-generates SQL as necessary.
80
82
* <p>
@@ -85,12 +87,9 @@ public interface GenerationPlan extends ExportableProducer {
85
87
void initialize (SqlStringGenerationContext context );
86
88
87
89
/**
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
92
91
*/
93
- Object execute ( SharedSessionContractImplementor session , Object incomingObject );
92
+ BeforeExecutionGenerator getGenerator ( );
94
93
95
94
/**
96
95
* Returns the {@link Setter injector} for the generated property.
@@ -132,7 +131,17 @@ public Object generate(SharedSessionContractImplementor session, Object object)
132
131
null :
133
132
new ArrayList <>( generationPlans .size () );
134
133
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
+ }
136
145
if ( generatedValues != null ) {
137
146
generatedValues .add ( generated );
138
147
}
0 commit comments