99import org .hibernate .HibernateException ;
1010import org .hibernate .boot .model .source .spi .AttributePath ;
1111import org .hibernate .boot .spi .MetadataBuildingContext ;
12+ import org .hibernate .engine .jdbc .env .spi .IdentifierHelper ;
1213
1314import static org .hibernate .boot .model .naming .ImplicitJoinColumnNameSource .Nature .ELEMENT_COLLECTION ;
1415import static org .hibernate .internal .util .StringHelper .isNotEmpty ;
@@ -92,17 +93,19 @@ public Identifier determineIdentifierColumnName(ImplicitIdentifierColumnNameSour
9293
9394 @ Override
9495 public Identifier determineDiscriminatorColumnName (ImplicitDiscriminatorColumnNameSource source ) {
96+ final MetadataBuildingContext context = source .getBuildingContext ();
9597 return toIdentifier (
96- source . getBuildingContext () .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
97- source . getBuildingContext ()
98+ context .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
99+ context
98100 );
99101 }
100102
101103 @ Override
102104 public Identifier determineTenantIdColumnName (ImplicitTenantIdColumnNameSource source ) {
105+ final MetadataBuildingContext context = source .getBuildingContext ();
103106 return toIdentifier (
104- source . getBuildingContext () .getEffectiveDefaults ().getDefaultTenantIdColumnName (),
105- source . getBuildingContext ()
107+ context .getEffectiveDefaults ().getDefaultTenantIdColumnName (),
108+ context
106109 );
107110 }
108111
@@ -113,7 +116,10 @@ public Identifier determineTenantIdColumnName(ImplicitTenantIdColumnNameSource s
113116 */
114117 @ Override
115118 public Identifier determineBasicColumnName (ImplicitBasicColumnNameSource source ) {
116- return toIdentifier ( transformAttributePath ( source .getAttributePath () ), source .getBuildingContext () );
119+ return toIdentifier (
120+ transformAttributePath ( source .getAttributePath () ),
121+ source .getBuildingContext ()
122+ );
117123 }
118124
119125 /**
@@ -152,25 +158,24 @@ public Identifier determinePrimaryKeyJoinColumnName(ImplicitPrimaryKeyJoinColumn
152158
153159 @ Override
154160 public Identifier determineAnyDiscriminatorColumnName (ImplicitAnyDiscriminatorColumnNameSource source ) {
155- final MetadataBuildingContext buildingContext = source .getBuildingContext ();
161+ final MetadataBuildingContext context = source .getBuildingContext ();
156162 return toIdentifier (
157163 transformAttributePath ( source .getAttributePath () )
158- + "_" + buildingContext .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
159- buildingContext
164+ + "_" + context .getEffectiveDefaults ().getDefaultDiscriminatorColumnName (),
165+ context
160166 );
161167 }
162168
163169 @ Override
164170 public Identifier determineAnyKeyColumnName (ImplicitAnyKeyColumnNameSource source ) {
165- final MetadataBuildingContext buildingContext = source .getBuildingContext ();
171+ final MetadataBuildingContext context = source .getBuildingContext ();
166172 return toIdentifier (
167173 transformAttributePath ( source .getAttributePath () )
168- + "_" + buildingContext .getEffectiveDefaults ().getDefaultIdColumnName (),
169- buildingContext
174+ + "_" + context .getEffectiveDefaults ().getDefaultIdColumnName (),
175+ context
170176 );
171177 }
172178
173-
174179 @ Override
175180 public Identifier determineMapKeyColumnName (ImplicitMapKeyColumnNameSource source ) {
176181 return toIdentifier (
@@ -190,35 +195,25 @@ public Identifier determineListIndexColumnName(ImplicitIndexColumnNameSource sou
190195 @ Override
191196 public Identifier determineForeignKeyName (ImplicitForeignKeyNameSource source ) {
192197 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 ;
200201 }
201202
202203 @ Override
203204 public Identifier determineUniqueKeyName (ImplicitUniqueKeyNameSource source ) {
204205 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 ;
211209 }
212210
213211 @ Override
214212 public Identifier determineIndexName (ImplicitIndexNameSource source ) {
215213 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 ;
222217 }
223218
224219 /**
@@ -235,18 +230,85 @@ protected String transformAttributePath(AttributePath attributePath) {
235230 }
236231
237232 /**
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}.
239235 *
240236 * @param stringForm The String form of the name
241- * @param buildingContext Access to the IdentifierHelper
237+ * @param buildingContext Access to the {@code IdentifierHelper}
242238 *
243239 * @return The identifier
244240 */
245241 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+ };
251313 }
252314}
0 commit comments