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