Implement support for detecting bidirectional attribute name for many-to-one

This commit is contained in:
Christian Beikov 2021-09-10 19:08:00 +02:00
parent 3cf0d9f037
commit 582e7f84aa
1 changed files with 21 additions and 1 deletions

View File

@ -8,6 +8,7 @@ package org.hibernate.metamodel.mapping.internal;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
import org.hibernate.LockMode; import org.hibernate.LockMode;
@ -15,11 +16,15 @@ import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming; import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.IndexedConsumer; import org.hibernate.mapping.IndexedConsumer;
import org.hibernate.mapping.ManyToOne; import org.hibernate.mapping.ManyToOne;
import org.hibernate.mapping.OneToOne; import org.hibernate.mapping.OneToOne;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.ToOne; import org.hibernate.mapping.ToOne;
import org.hibernate.metamodel.mapping.AssociationKey; import org.hibernate.metamodel.mapping.AssociationKey;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.CollectionPart; import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.EntityAssociationMapping; import org.hibernate.metamodel.mapping.EntityAssociationMapping;
import org.hibernate.metamodel.mapping.EntityIdentifierMapping; import org.hibernate.metamodel.mapping.EntityIdentifierMapping;
@ -172,7 +177,22 @@ public class ToOneAttributeMapping
else { else {
cardinality = Cardinality.MANY_TO_ONE; cardinality = Cardinality.MANY_TO_ONE;
} }
this.bidirectionalAttributeName = manyToOne.getReferencedPropertyName(); if ( referencedPropertyName == null ) {
String bidirectionalAttributeName = null;
final PersistentClass entityBinding = manyToOne.getMetadata().getEntityBinding( manyToOne.getReferencedEntityName() );
final Iterator<Property> propertyClosureIterator = entityBinding.getPropertyClosureIterator();
while (propertyClosureIterator.hasNext()) {
final Property property = propertyClosureIterator.next();
if ( property.getValue() instanceof Collection && name.equals( ( (Collection) property.getValue() ).getMappedByProperty() ) ) {
bidirectionalAttributeName = property.getName();
break;
}
}
this.bidirectionalAttributeName = bidirectionalAttributeName;
}
else {
this.bidirectionalAttributeName = referencedPropertyName;
}
} }
else { else {
assert bootValue instanceof OneToOne; assert bootValue instanceof OneToOne;