|
61 | 61 | import org.hibernate.mapping.BasicValue; |
62 | 62 | import org.hibernate.mapping.Component; |
63 | 63 | import org.hibernate.mapping.PersistentClass; |
64 | | -import org.hibernate.mapping.SimpleValue; |
65 | 64 | import org.hibernate.mapping.Table; |
66 | 65 | import org.hibernate.models.spi.ClassDetails; |
67 | 66 | import org.hibernate.models.spi.MemberDetails; |
@@ -1279,49 +1278,7 @@ private void linkWithValue() { |
1279 | 1278 | final InFlightMetadataCollector collector = getMetadataCollector(); |
1280 | 1279 | final AnnotatedColumn firstColumn = columns.getColumns().get(0); |
1281 | 1280 | if ( !collector.isInSecondPass() && firstColumn.isNameDeferred() && referencedEntityName != null ) { |
1282 | | - final AnnotatedJoinColumns joinColumns = new AnnotatedJoinColumns(); |
1283 | | - joinColumns.setBuildingContext( buildingContext ); |
1284 | | - joinColumns.setPropertyHolder( columns.getPropertyHolder() ); |
1285 | | - joinColumns.setPropertyName( columns.getPropertyName() ); |
1286 | | - //TODO: resetting the parent here looks like a dangerous thing to do |
1287 | | - // should we be cloning them first (the legacy code did not) |
1288 | | - for ( AnnotatedColumn column : columns.getColumns() ) { |
1289 | | - column.setParent( joinColumns ); |
1290 | | - } |
1291 | | - collector.addSecondPass( |
1292 | | - new FkSecondPass() { |
1293 | | - @Override |
1294 | | - public SimpleValue getValue() { |
1295 | | - return basicValue; |
1296 | | - } |
1297 | | - |
1298 | | - @Override |
1299 | | - public String getReferencedEntityName() { |
1300 | | - return referencedEntityName; |
1301 | | - } |
1302 | | - |
1303 | | - @Override |
1304 | | - public boolean isInPrimaryKey() { |
1305 | | - // @MapsId is not itself in the primary key, |
1306 | | - // so it's safe to simply process it after all the primary keys have been processed. |
1307 | | - return true; |
1308 | | - } |
1309 | | - |
1310 | | - @Override |
1311 | | - public void doSecondPass(Map<String, PersistentClass> persistentClasses) { |
1312 | | - final PersistentClass referencedEntity = persistentClasses.get( referencedEntityName ); |
1313 | | - if ( referencedEntity == null ) { |
1314 | | - // TODO: much better error message if this is something that can really happen! |
1315 | | - throw new AnnotationException( "Unknown entity name '" + referencedEntityName + "'" ); |
1316 | | - } |
1317 | | - linkJoinColumnWithValueOverridingNameIfImplicit( |
1318 | | - referencedEntity, |
1319 | | - referencedEntity.getKey(), |
1320 | | - joinColumns, basicValue |
1321 | | - ); |
1322 | | - } |
1323 | | - } |
1324 | | - ); |
| 1281 | + collector.addSecondPass( new OverriddenFkSecondPass( basicValue, referencedEntityName, columns ) ); |
1325 | 1282 | } |
1326 | 1283 | else if ( aggregateComponent != null ) { |
1327 | 1284 | assert columns.getColumns().size() == 1; |
@@ -1530,4 +1487,65 @@ public Map<String,String> customTypeParameters(MemberDetails attribute, SourceMo |
1530 | 1487 | return emptyMap(); |
1531 | 1488 | } |
1532 | 1489 | } |
| 1490 | + |
| 1491 | + private static AnnotatedJoinColumns convertToJoinColumns(AnnotatedColumns columns, MetadataBuildingContext context) { |
| 1492 | + final AnnotatedJoinColumns joinColumns = new AnnotatedJoinColumns(); |
| 1493 | + joinColumns.setBuildingContext( context ); |
| 1494 | + joinColumns.setPropertyHolder( columns.getPropertyHolder() ); |
| 1495 | + joinColumns.setPropertyName( columns.getPropertyName() ); |
| 1496 | + //TODO: resetting the parent here looks like a dangerous thing to do |
| 1497 | + // should we be cloning them first (the legacy code did not) |
| 1498 | + for ( AnnotatedColumn column : columns.getColumns() ) { |
| 1499 | + column.setParent( joinColumns ); |
| 1500 | + } |
| 1501 | + return joinColumns; |
| 1502 | + } |
| 1503 | + |
| 1504 | + // used for resolving FK with @MapsId and @IdClass |
| 1505 | + private static class OverriddenFkSecondPass implements FkSecondPass { |
| 1506 | + private final AnnotatedJoinColumns joinColumns; |
| 1507 | + private final BasicValue value; |
| 1508 | + private final String referencedEntityName; |
| 1509 | + |
| 1510 | + public OverriddenFkSecondPass( |
| 1511 | + BasicValue value, |
| 1512 | + String referencedEntityName, |
| 1513 | + AnnotatedColumns columns) { |
| 1514 | + this.value = value; |
| 1515 | + this.referencedEntityName = referencedEntityName; |
| 1516 | + this.joinColumns = convertToJoinColumns( columns, value.getBuildingContext() ); |
| 1517 | + } |
| 1518 | + |
| 1519 | + @Override |
| 1520 | + public BasicValue getValue() { |
| 1521 | + return value; |
| 1522 | + } |
| 1523 | + |
| 1524 | + @Override |
| 1525 | + public String getReferencedEntityName() { |
| 1526 | + return referencedEntityName; |
| 1527 | + } |
| 1528 | + |
| 1529 | + @Override |
| 1530 | + public boolean isInPrimaryKey() { |
| 1531 | + // @MapsId is not itself in the primary key, |
| 1532 | + // so it's safe to simply process it after all the primary keys have been processed. |
| 1533 | + return true; |
| 1534 | + } |
| 1535 | + |
| 1536 | + @Override |
| 1537 | + public void doSecondPass(Map<String, PersistentClass> persistentClasses) { |
| 1538 | + final PersistentClass referencedEntity = persistentClasses.get( referencedEntityName ); |
| 1539 | + if ( referencedEntity == null ) { |
| 1540 | + // TODO: much better error message if this is something that can really happen! |
| 1541 | + throw new AnnotationException( "Unknown entity name '" + referencedEntityName + "'" ); |
| 1542 | + } |
| 1543 | + linkJoinColumnWithValueOverridingNameIfImplicit( |
| 1544 | + referencedEntity, |
| 1545 | + referencedEntity.getKey(), |
| 1546 | + joinColumns, |
| 1547 | + value |
| 1548 | + ); |
| 1549 | + } |
| 1550 | + } |
1533 | 1551 | } |
0 commit comments