HHH-15982 Check property type when setting bidirectionalAttributeName

This commit is contained in:
Marco Belladelli 2023-01-09 15:10:56 +01:00 committed by Christian Beikov
parent e26f4d25ab
commit 436527b4ba
5 changed files with 20 additions and 13 deletions

View File

@ -147,6 +147,10 @@ public class ToOneAttributeMapping
private final Set<String> targetKeyPropertyNames;
private final Cardinality cardinality;
/*
Capture the other side's name of a possibly bidirectional association to allow resolving circular fetches.
It may be null if the referenced property is a non-entity.
*/
private final String bidirectionalAttributeName;
private final TableGroupProducer declaringTableGroupProducer;
@ -222,10 +226,10 @@ public class ToOneAttributeMapping
else {
cardinality = Cardinality.MANY_TO_ONE;
}
final PersistentClass entityBinding = manyToOne.getMetadata()
.getEntityBinding( manyToOne.getReferencedEntityName() );
if ( referencedPropertyName == null ) {
String bidirectionalAttributeName = null;
final PersistentClass entityBinding = manyToOne.getMetadata()
.getEntityBinding( manyToOne.getReferencedEntityName() );
if ( cardinality == Cardinality.LOGICAL_ONE_TO_ONE ) {
// Handle join table cases
for ( Join join : entityBinding.getJoinClosure() ) {
@ -271,7 +275,11 @@ public class ToOneAttributeMapping
this.bidirectionalAttributeName = bidirectionalAttributeName;
}
else {
this.bidirectionalAttributeName = referencedPropertyName;
// Only set the bidirectional attribute name if the referenced property can actually be circular i.e. an entity type
final Property property = entityBinding.getProperty( referencedPropertyName );
this.bidirectionalAttributeName = property != null && property.getValue() instanceof EntityType
? referencedPropertyName
: null;
}
if ( bootValue.isNullable() ) {
isKeyTableNullable = true;
@ -991,8 +999,7 @@ public class ToOneAttributeMapping
}
return false;
}
return parentNavigablePath.getLocalName().equals( bidirectionalAttributeName ) ||
parentNavigablePath.getLocalName().equals( identifyingColumnsTableExpression );
return parentNavigablePath.getLocalName().equals( bidirectionalAttributeName );
}
public String getBidirectionalAttributeName(){

View File

@ -142,7 +142,7 @@ public class BidirectionalOneToOneWithConverterEagerTest {
}
@Entity(name = "FooEntity")
@Table(name = "foo")
@Table(name = "foo_table")
public static class FooEntity {
@Id
@GeneratedValue
@ -192,7 +192,7 @@ public class BidirectionalOneToOneWithConverterEagerTest {
}
@Entity(name = "BarEntity")
@Table(name = "bar")
@Table(name = "bar_table")
public static class BarEntity {
@Id
@GeneratedValue

View File

@ -140,7 +140,7 @@ public class BidirectionalOneToOneWithConverterLazyTest {
}
@Entity(name = "FooEntity")
@Table(name = "foo")
@Table(name = "foo_table")
public static class FooEntity {
@Id
@GeneratedValue
@ -190,7 +190,7 @@ public class BidirectionalOneToOneWithConverterLazyTest {
}
@Entity(name = "BarEntity")
@Table(name = "bar")
@Table(name = "bar_table")
public static class BarEntity {
@Id
@GeneratedValue

View File

@ -104,7 +104,7 @@ public class BidirectionalOneToOneEagerFKTest {
}
@Entity(name = "FooEntity")
@Table(name = "foo")
@Table(name = "foo_table")
public static class FooEntity {
@Id
@GeneratedValue
@ -153,7 +153,7 @@ public class BidirectionalOneToOneEagerFKTest {
}
@Entity(name = "BarEntity")
@Table(name = "bar")
@Table(name = "bar_table")
public static class BarEntity {
@Id
@GeneratedValue

View File

@ -102,7 +102,7 @@ public class BidirectionalOneToOneLazyFKTest {
}
@Entity(name = "FooEntity")
@Table(name = "foo")
@Table(name = "foo_table")
public static class FooEntity {
@Id
@GeneratedValue
@ -151,7 +151,7 @@ public class BidirectionalOneToOneLazyFKTest {
}
@Entity(name = "BarEntity")
@Table(name = "bar")
@Table(name = "bar_table")
public static class BarEntity {
@Id
@GeneratedValue