9
9
import org .hibernate .HibernateException ;
10
10
import org .hibernate .boot .model .source .spi .AttributePath ;
11
11
import org .hibernate .boot .spi .MetadataBuildingContext ;
12
+ import org .hibernate .engine .jdbc .env .spi .IdentifierHelper ;
12
13
13
14
import static org .hibernate .boot .model .naming .ImplicitJoinColumnNameSource .Nature .ELEMENT_COLLECTION ;
14
15
import static org .hibernate .internal .util .StringHelper .isNotEmpty ;
@@ -92,17 +93,19 @@ public Identifier determineIdentifierColumnName(ImplicitIdentifierColumnNameSour
92
93
93
94
@ Override
94
95
public Identifier determineDiscriminatorColumnName (ImplicitDiscriminatorColumnNameSource source ) {
96
+ final MetadataBuildingContext context = source .getBuildingContext ();
95
97
return toIdentifier (
96
- source . getBuildingContext () .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
97
- source . getBuildingContext ()
98
+ context .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
99
+ context
98
100
);
99
101
}
100
102
101
103
@ Override
102
104
public Identifier determineTenantIdColumnName (ImplicitTenantIdColumnNameSource source ) {
105
+ final MetadataBuildingContext context = source .getBuildingContext ();
103
106
return toIdentifier (
104
- source . getBuildingContext () .getEffectiveDefaults ().getDefaultTenantIdColumnName (),
105
- source . getBuildingContext ()
107
+ context .getEffectiveDefaults ().getDefaultTenantIdColumnName (),
108
+ context
106
109
);
107
110
}
108
111
@@ -113,7 +116,10 @@ public Identifier determineTenantIdColumnName(ImplicitTenantIdColumnNameSource s
113
116
*/
114
117
@ Override
115
118
public Identifier determineBasicColumnName (ImplicitBasicColumnNameSource source ) {
116
- return toIdentifier ( transformAttributePath ( source .getAttributePath () ), source .getBuildingContext () );
119
+ return toIdentifier (
120
+ transformAttributePath ( source .getAttributePath () ),
121
+ source .getBuildingContext ()
122
+ );
117
123
}
118
124
119
125
/**
@@ -152,25 +158,24 @@ public Identifier determinePrimaryKeyJoinColumnName(ImplicitPrimaryKeyJoinColumn
152
158
153
159
@ Override
154
160
public Identifier determineAnyDiscriminatorColumnName (ImplicitAnyDiscriminatorColumnNameSource source ) {
155
- final MetadataBuildingContext buildingContext = source .getBuildingContext ();
161
+ final MetadataBuildingContext context = source .getBuildingContext ();
156
162
return toIdentifier (
157
163
transformAttributePath ( source .getAttributePath () )
158
- + "_" + buildingContext .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
159
- buildingContext
164
+ + "_" + context .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
165
+ context
160
166
);
161
167
}
162
168
163
169
@ Override
164
170
public Identifier determineAnyKeyColumnName (ImplicitAnyKeyColumnNameSource source ) {
165
- final MetadataBuildingContext buildingContext = source .getBuildingContext ();
171
+ final MetadataBuildingContext context = source .getBuildingContext ();
166
172
return toIdentifier (
167
173
transformAttributePath ( source .getAttributePath () )
168
- + "_" + buildingContext .getEffectiveDefaults ().getDefaultIdColumnName (),
169
- buildingContext
174
+ + "_" + context .getEffectiveDefaults ().getDefaultIdColumnName (),
175
+ context
170
176
);
171
177
}
172
178
173
-
174
179
@ Override
175
180
public Identifier determineMapKeyColumnName (ImplicitMapKeyColumnNameSource source ) {
176
181
return toIdentifier (
@@ -190,35 +195,25 @@ public Identifier determineListIndexColumnName(ImplicitIndexColumnNameSource sou
190
195
@ Override
191
196
public Identifier determineForeignKeyName (ImplicitForeignKeyNameSource source ) {
192
197
final Identifier userProvidedIdentifier = source .getUserProvidedIdentifier ();
193
- final MetadataBuildingContext buildingContext = source .getBuildingContext ();
194
- return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier (
195
- NamingHelper .withCharset ( buildingContext .getBuildingOptions ().getSchemaCharset () )
196
- .generateHashedFkName ( "FK" , source .getTableName (),
197
- source .getReferencedTableName (), source .getColumnNames () ),
198
- buildingContext
199
- );
198
+ return userProvidedIdentifier == null
199
+ ? generateConstraintName ( source )
200
+ : userProvidedIdentifier ;
200
201
}
201
202
202
203
@ Override
203
204
public Identifier determineUniqueKeyName (ImplicitUniqueKeyNameSource source ) {
204
205
final Identifier userProvidedIdentifier = source .getUserProvidedIdentifier ();
205
- final MetadataBuildingContext buildingContext = source .getBuildingContext ();
206
- return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier (
207
- NamingHelper .withCharset ( buildingContext .getBuildingOptions ().getSchemaCharset () )
208
- .generateHashedConstraintName ( "UK" , source .getTableName (), source .getColumnNames () ),
209
- buildingContext
210
- );
206
+ return userProvidedIdentifier == null
207
+ ? generateConstraintName ( source )
208
+ : userProvidedIdentifier ;
211
209
}
212
210
213
211
@ Override
214
212
public Identifier determineIndexName (ImplicitIndexNameSource source ) {
215
213
final Identifier userProvidedIdentifier = source .getUserProvidedIdentifier ();
216
- final MetadataBuildingContext buildingContext = source .getBuildingContext ();
217
- return userProvidedIdentifier != null ? userProvidedIdentifier : toIdentifier (
218
- NamingHelper .withCharset ( buildingContext .getBuildingOptions ().getSchemaCharset () )
219
- .generateHashedConstraintName ( "IDX" , source .getTableName (), source .getColumnNames () ),
220
- buildingContext
221
- );
214
+ return userProvidedIdentifier == null
215
+ ? generateConstraintName ( source )
216
+ : userProvidedIdentifier ;
222
217
}
223
218
224
219
/**
@@ -235,18 +230,85 @@ protected String transformAttributePath(AttributePath attributePath) {
235
230
}
236
231
237
232
/**
238
- * Easy hook to build an Identifier using the keyword safe IdentifierHelper.
233
+ * Easy hook to build an {@link Identifier} using the keyword safe
234
+ * {@link org.hibernate.engine.jdbc.env.spi.IdentifierHelper}.
239
235
*
240
236
* @param stringForm The String form of the name
241
- * @param buildingContext Access to the IdentifierHelper
237
+ * @param buildingContext Access to the {@code IdentifierHelper}
242
238
*
243
239
* @return The identifier
244
240
*/
245
241
protected Identifier toIdentifier (String stringForm , MetadataBuildingContext buildingContext ) {
246
- return buildingContext .getMetadataCollector ()
247
- .getDatabase ()
248
- .getJdbcEnvironment ()
249
- .getIdentifierHelper ()
250
- .toIdentifier ( stringForm );
242
+ return toIdentifier ( stringForm ,
243
+ buildingContext .getMetadataCollector ()
244
+ .getDatabase ()
245
+ .getJdbcEnvironment ()
246
+ .getIdentifierHelper () );
247
+ }
248
+
249
+ /**
250
+ * Easy hook to build an {@link Identifier} using the keyword safe
251
+ * {@link org.hibernate.engine.jdbc.env.spi.IdentifierHelper}.
252
+ *
253
+ * @param stringForm The String form of the name
254
+ * @param identifierHelper The {@code IdentifierHelper}
255
+ *
256
+ * @return The identifier
257
+ */
258
+ protected Identifier toIdentifier (String stringForm , IdentifierHelper identifierHelper ) {
259
+ return identifierHelper .toIdentifier ( stringForm );
260
+ }
261
+
262
+ /**
263
+ * Generate a name for the given constraint.
264
+ *
265
+ * @return The identifier
266
+ */
267
+ protected Identifier generateConstraintName (ImplicitConstraintNameSource source ) {
268
+ return toIdentifier ( generateConstraintNameString ( source ), source .getBuildingContext () );
269
+ }
270
+
271
+ /**
272
+ * Generate a name for the given constraint.
273
+ *
274
+ * @return The name as a string
275
+ */
276
+ protected String generateConstraintNameString (ImplicitConstraintNameSource source ) {
277
+ final NamingHelper namingHelper = namingHelper ( source .getBuildingContext () );
278
+ final String prefix = constraintNamePrefix ( source .kind () );
279
+ return source instanceof ImplicitForeignKeyNameSource foreignKeySource
280
+ ? namingHelper .generateHashedFkName (
281
+ prefix ,
282
+ source .getTableName (),
283
+ // include the referenced table in the hash
284
+ foreignKeySource .getReferencedTableName (),
285
+ source .getColumnNames ()
286
+ )
287
+ : namingHelper .generateHashedConstraintName (
288
+ prefix ,
289
+ source .getTableName (),
290
+ source .getColumnNames ()
291
+ );
292
+ }
293
+
294
+ /**
295
+ * Obtain a {@link NamingHelper} for use in constraint name generation.
296
+ */
297
+ protected NamingHelper namingHelper (MetadataBuildingContext context ) {
298
+ return NamingHelper .withCharset ( context .getBuildingOptions ().getSchemaCharset () );
299
+ }
300
+
301
+ /**
302
+ * The prefix for a generated constraint name of the given
303
+ * {@linkplain ImplicitConstraintNameSource.Kind kind}.
304
+ *
305
+ * @return The prefix as a string
306
+ */
307
+ protected String constraintNamePrefix (ImplicitConstraintNameSource .Kind kind ) {
308
+ return switch ( kind ) {
309
+ case INDEX -> "IDX" ;
310
+ case UNIQUE_KEY -> "UK" ;
311
+ case FOREIGN_KEY -> "FK" ;
312
+ };
251
313
}
252
314
}
0 commit comments