20
20
import org .hibernate .boot .model .naming .ImplicitBasicColumnNameSource ;
21
21
import org .hibernate .boot .model .naming .ImplicitNamingStrategy ;
22
22
import org .hibernate .boot .model .naming .ObjectNameNormalizer ;
23
+ import org .hibernate .boot .model .naming .PhysicalNamingStrategy ;
23
24
import org .hibernate .boot .model .relational .Database ;
24
25
import org .hibernate .boot .model .source .spi .AttributePath ;
25
26
import org .hibernate .boot .spi .InFlightMetadataCollector ;
@@ -342,19 +343,15 @@ public boolean isNameDeferred() {
342
343
}
343
344
344
345
public void redefineColumnName (String columnName , String propertyName , boolean applyNamingStrategy ) {
345
- if ( StringHelper . isEmpty ( columnName ) && StringHelper . isEmpty ( propertyName ) ) {
346
- // nothing to do
347
- return ;
346
+ if ( ! isEmpty ( columnName ) || ! isEmpty ( propertyName ) ) {
347
+ final String logicalColumnName = resolveLogicalColumnName ( columnName , propertyName );
348
+ mappingColumn . setName ( processColumnName ( logicalColumnName , applyNamingStrategy ) ) ;
348
349
}
349
- final String logicalColumnName = resolveLogicalColumnName ( columnName , propertyName );
350
- mappingColumn .setName ( processColumnName ( logicalColumnName , applyNamingStrategy ) );
350
+ // else nothing to do
351
351
}
352
352
353
353
private String resolveLogicalColumnName (String columnName , String propertyName ) {
354
- final String baseColumnName = StringHelper .isNotEmpty ( columnName )
355
- ? columnName
356
- : inferColumnName ( propertyName );
357
-
354
+ final String baseColumnName = isNotEmpty ( columnName ) ? columnName : inferColumnName ( propertyName );
358
355
if ( parent .getPropertyHolder () != null && parent .getPropertyHolder ().isComponent () ) {
359
356
// see if we need to apply one-or-more @EmbeddedColumnNaming patterns
360
357
return applyEmbeddedColumnNaming ( baseColumnName , (ComponentPropertyHolder ) parent .getPropertyHolder () );
@@ -370,7 +367,7 @@ private String applyEmbeddedColumnNaming(String inferredColumnName, ComponentPro
370
367
boolean appliedAnyPatterns = false ;
371
368
372
369
final String columnNamingPattern = propertyHolder .getComponent ().getColumnNamingPattern ();
373
- if ( StringHelper . isNotEmpty ( columnNamingPattern ) ) {
370
+ if ( isNotEmpty ( columnNamingPattern ) ) {
374
371
// zip_code
375
372
result = String .format ( columnNamingPattern , result );
376
373
appliedAnyPatterns = true ;
@@ -380,7 +377,7 @@ private String applyEmbeddedColumnNaming(String inferredColumnName, ComponentPro
380
377
while ( tester .parent .isComponent () ) {
381
378
final ComponentPropertyHolder parentHolder = (ComponentPropertyHolder ) tester .parent ;
382
379
final String parentColumnNamingPattern = parentHolder .getComponent ().getColumnNamingPattern ();
383
- if ( StringHelper . isNotEmpty ( parentColumnNamingPattern ) ) {
380
+ if ( isNotEmpty ( parentColumnNamingPattern ) ) {
384
381
// home_zip_code
385
382
result = String .format ( parentColumnNamingPattern , result );
386
383
appliedAnyPatterns = true ;
@@ -398,24 +395,19 @@ private String applyEmbeddedColumnNaming(String inferredColumnName, ComponentPro
398
395
399
396
protected String processColumnName (String columnName , boolean applyNamingStrategy ) {
400
397
if ( applyNamingStrategy ) {
401
- final Database database = getBuildingContext (). getMetadataCollector (). getDatabase ();
402
- return getBuildingContext (). getBuildingOptions (). getPhysicalNamingStrategy ()
398
+ final Database database = getDatabase ();
399
+ return getPhysicalNamingStrategy ()
403
400
.toPhysicalColumnName ( database .toIdentifier ( columnName ), database .getJdbcEnvironment () )
404
401
.render ( database .getDialect () );
405
402
}
406
403
else {
407
- return getBuildingContext (). getObjectNameNormalizer ().toDatabaseIdentifierText ( columnName );
404
+ return getObjectNameNormalizer ().toDatabaseIdentifierText ( columnName );
408
405
}
409
-
410
406
}
411
407
412
408
protected String inferColumnName (String propertyName ) {
413
- final Database database = getBuildingContext ().getMetadataCollector ().getDatabase ();
414
- final ObjectNameNormalizer normalizer = getBuildingContext ().getObjectNameNormalizer ();
415
- final ImplicitNamingStrategy implicitNamingStrategy = getBuildingContext ().getBuildingOptions ().getImplicitNamingStrategy ();
416
-
417
- Identifier implicitName = normalizer .normalizeIdentifierQuoting (
418
- implicitNamingStrategy .determineBasicColumnName (
409
+ Identifier implicitName = getObjectNameNormalizer ().normalizeIdentifierQuoting (
410
+ getImplicitNamingStrategy ().determineBasicColumnName (
419
411
new ImplicitBasicColumnNameSource () {
420
412
final AttributePath attributePath = AttributePath .parse ( propertyName );
421
413
@@ -448,11 +440,28 @@ public MetadataBuildingContext getBuildingContext() {
448
440
);
449
441
}
450
442
451
- return getBuildingContext ().getBuildingOptions ().getPhysicalNamingStrategy ()
443
+ final Database database = getDatabase ();
444
+ return getPhysicalNamingStrategy ()
452
445
.toPhysicalColumnName ( implicitName , database .getJdbcEnvironment () )
453
446
.render ( database .getDialect () );
454
447
}
455
448
449
+ private ObjectNameNormalizer getObjectNameNormalizer () {
450
+ return getBuildingContext ().getObjectNameNormalizer ();
451
+ }
452
+
453
+ private Database getDatabase () {
454
+ return getBuildingContext ().getMetadataCollector ().getDatabase ();
455
+ }
456
+
457
+ private PhysicalNamingStrategy getPhysicalNamingStrategy () {
458
+ return getBuildingContext ().getBuildingOptions ().getPhysicalNamingStrategy ();
459
+ }
460
+
461
+ private ImplicitNamingStrategy getImplicitNamingStrategy () {
462
+ return getBuildingContext ().getBuildingOptions ().getImplicitNamingStrategy ();
463
+ }
464
+
456
465
public String getName () {
457
466
return mappingColumn .getName ();
458
467
}
@@ -503,14 +512,12 @@ public void linkWithValue(SimpleValue value) {
503
512
504
513
protected void addColumnBinding (SimpleValue value ) {
505
514
final String logicalColumnName ;
506
- final MetadataBuildingContext context = getBuildingContext ();
507
- final InFlightMetadataCollector collector = context .getMetadataCollector ();
508
515
if ( isNotEmpty ( this .logicalColumnName ) ) {
509
516
logicalColumnName = this .logicalColumnName ;
510
517
}
511
518
else {
512
- final Identifier implicitName = context . getObjectNameNormalizer ().normalizeIdentifierQuoting (
513
- context . getBuildingOptions (). getImplicitNamingStrategy ().determineBasicColumnName (
519
+ final Identifier implicitName = getObjectNameNormalizer ().normalizeIdentifierQuoting (
520
+ getImplicitNamingStrategy ().determineBasicColumnName (
514
521
new ImplicitBasicColumnNameSource () {
515
522
@ Override
516
523
public AttributePath getAttributePath () {
@@ -524,14 +531,15 @@ public boolean isCollectionElement() {
524
531
525
532
@ Override
526
533
public MetadataBuildingContext getBuildingContext () {
527
- return context ;
534
+ return AnnotatedColumn . this . getBuildingContext () ;
528
535
}
529
536
}
530
537
)
531
538
);
532
- logicalColumnName = implicitName .render ( collector . getDatabase ().getDialect () );
539
+ logicalColumnName = implicitName .render ( getDatabase ().getDialect () );
533
540
}
534
- collector .addColumnNameBinding ( value .getTable (), logicalColumnName , getMappingColumn () );
541
+ getBuildingContext ().getMetadataCollector ()
542
+ .addColumnNameBinding ( value .getTable (), logicalColumnName , getMappingColumn () );
535
543
}
536
544
537
545
public void forceNotNull () {
0 commit comments