Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package org.hibernate.boot;

import org.hibernate.Incubating;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
import org.hibernate.boot.archive.scan.spi.ScanOptions;
import org.hibernate.boot.archive.scan.spi.Scanner;
Expand Down Expand Up @@ -276,19 +275,6 @@ public interface MetadataBuilder {
*/
MetadataBuilder enableGlobalNationalizedCharacterDataSupport(boolean enabled);

/**
* Specify whether missing {@link jakarta.persistence.MapsId} annotations
* should be inferred.
*
* @param enabled {@code true} if missing {@code MapsId} should be inferred
*
* @return {@code this}, for method chaining
*
* @since 7.0
*/
@Incubating
MetadataBuilder enableMapsIdInference(boolean enabled);

/**
* Specify an additional or overridden basic type mapping.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1372,16 +1372,6 @@ public void addPropertyAnnotatedWithMapsId(ClassDetails entityType, PropertyData
.put( property.getAttributeMember().getDirectAnnotationUsage( MapsId.class ).value(), property );
}

@Override
public void addInferredMapsIdProperty(ClassDetails entityType, PropertyData property, String mapsIdValue) {
if ( propertiesAnnotatedWithMapsId == null ) {
propertiesAnnotatedWithMapsId = new HashMap<>();
}

propertiesAnnotatedWithMapsId.computeIfAbsent( entityType, k -> new HashMap<>() )
.put( mapsIdValue, property );
}

@Override
public PropertyData getPropertyAnnotatedWithIdAndToOne(ClassDetails entityType, String propertyName) {
if ( propertiesAnnotatedWithIdAndToOne == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,6 @@ public MetadataBuilder applyTempClassLoader(ClassLoader tempClassLoader) {
return this;
}

public MetadataBuilder enableMapsIdInference(boolean enabled) {
options.mapsIdInferenceEnabled = enabled;
return this;
}

public MetadataBuilder noConstraintByDefault() {
options.noConstraintByDefault = true;
return this;
Expand Down Expand Up @@ -644,7 +639,6 @@ public static class MetadataBuildingOptionsImpl
private boolean implicitDiscriminatorsForJoinedInheritanceSupported;
private boolean implicitlyForceDiscriminatorInSelect;
private boolean useNationalizedCharacterData;
private boolean mapsIdInferenceEnabled;
private boolean noConstraintByDefault;

private final String schemaCharset;
Expand Down Expand Up @@ -735,12 +729,6 @@ else if ( value instanceof AccessType accessType ) {
regionFactory == null ? null : regionFactory.getDefaultAccessType()
);

mapsIdInferenceEnabled = configService.getSetting(
"hibernate.enable_mapsid_inference",
BOOLEAN,
false
);

noConstraintByDefault = ConstraintMode.NO_CONSTRAINT.name().equalsIgnoreCase( configService.getSetting(
AvailableSettings.HBM2DDL_DEFAULT_CONSTRAINT_MODE,
String.class,
Expand Down Expand Up @@ -919,11 +907,6 @@ public boolean useNationalizedCharacterData() {
return useNationalizedCharacterData;
}

@Override
public boolean isMapsIdInferenceEnabled() {
return mapsIdInferenceEnabled;
}

@Override
public boolean isNoConstraintByDefault() {
return noConstraintByDefault;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.hibernate.annotations.OptimisticLock;
import org.hibernate.annotations.Parent;
import org.hibernate.binder.AttributeBinder;
import org.hibernate.boot.models.JpaAnnotations;
import org.hibernate.boot.spi.AccessType;
import org.hibernate.boot.spi.InFlightMetadataCollector;
import org.hibernate.boot.spi.MetadataBuildingContext;
Expand Down Expand Up @@ -67,11 +66,9 @@


import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
Expand Down Expand Up @@ -623,7 +620,6 @@ private static int addProperty(
final MemberDetails element = propertyAnnotatedElement.getAttributeMember();
if ( hasIdAnnotation( element ) ) {
inFlightPropertyDataList.add( idPropertyCounter, propertyAnnotatedElement );
handleInferredMapsIdProperty( propertyContainer, context, declaringClass, ownerType, element );
if ( hasToOneAnnotation( element ) ) {
context.getMetadataCollector()
.addToOneAndIdProperty( ownerType.determineRawClass(), propertyAnnotatedElement );
Expand Down Expand Up @@ -653,65 +649,6 @@ private static void checkIdProperty(MemberDetails property, PropertyData propert
}
}

// The following code infers a "missing" @MapsId annotation when
// an @Id Column matches a @JoinColumn of another field. No test
// fails if I simply remove this code, and, indeed, it was broken
// and doing nothing before I got here. I've now "fixed" it to do
// what it was supposed to be doing, but honestly the semantics
// aren't clear: why should it be linked to the existence of an
// explicit @Column annotation? And there's still no test for it.
//
// The real work is done by ToOneBinder#handleInferredMapsId
private static void handleInferredMapsIdProperty(
PropertyContainer propertyContainer,
MetadataBuildingContext context,
ClassDetails declaringClass,
TypeVariableScope ownerType,
MemberDetails element) {
if ( context.getBuildingOptions().isMapsIdInferenceEnabled() ) {
//TODO support true/false/default on the property instead of present / not present
final SourceModelBuildingContext sourceModelContext =
context.getMetadataCollector().getSourceModelBuildingContext();
if ( element.hasDirectAnnotationUsage( Id.class )
//TODO Explicit @Column should not be mandatory here
&& element.hasDirectAnnotationUsage( Column.class ) ) {
final String columnName = element.getDirectAnnotationUsage( Column.class ).name();
declaringClass.forEachPersistableMember( memberDetails -> {
if ( !memberDetails.hasDirectAnnotationUsage( MapsId.class )
&& isJoinColumnPresent( columnName, memberDetails, sourceModelContext ) ) {
//create a PropertyData for the specJ property holding the mapping
context.getMetadataCollector().addInferredMapsIdProperty(
ownerType.determineRawClass(),
new PropertyInferredData(
declaringClass,
ownerType,
//same dec
memberDetails,
// the actual @XToOne property
propertyContainer.getClassLevelAccessType().getType(),
//TODO we should get the right accessor but the same as id would do
context
),
element.toString()
);
}
} );
}
}
}

private static boolean isJoinColumnPresent(String columnName, MemberDetails property, SourceModelBuildingContext modelContext) {
// The detection of a configured individual JoinColumn differs
// between Annotation and XML configuration processing.
for ( JoinColumn joinColumnAnnotation :
property.getRepeatedAnnotationUsages( JpaAnnotations.JOIN_COLUMN, modelContext ) ) {
if ( joinColumnAnnotation.name().equals( columnName ) ) {
return true;
}
}
return false;
}

static boolean hasIdAnnotation(MemberDetails element) {
return element.hasDirectAnnotationUsage( Id.class )
|| element.hasDirectAnnotationUsage( EmbeddedId.class );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import static org.hibernate.boot.model.internal.BinderHelper.noConstraint;
import static org.hibernate.internal.CoreLogging.messageLogger;
import static org.hibernate.internal.util.StringHelper.isBlank;
import static org.hibernate.internal.util.StringHelper.isNotBlank;
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
import static org.hibernate.internal.util.StringHelper.qualify;

Expand Down Expand Up @@ -214,7 +213,6 @@ private static void bindManyToOne(
joinColumns.setMapsId( mapsId.value() );
}

final boolean hasInferredMapsId = handleInferredMapsId( joinColumns, inferredData, property, context );
value.setTypeName( inferredData.getClassOrElementName() );
final String propertyName = inferredData.getPropertyName();
value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName );
Expand Down Expand Up @@ -244,7 +242,7 @@ private static void bindManyToOne(
joinColumns,
optional,
inferredData,
isIdentifierMapper || hasInferredMapsId,
isIdentifierMapper,
propertyBinder,
value,
property,
Expand All @@ -257,48 +255,6 @@ static boolean isTargetAnnotatedEntity(ClassDetails targetEntity, MemberDetails
return target.hasDirectAnnotationUsage( Entity.class );
}

// The following code infers a "missing" @MapsId annotation
// when an @Id Column matches a @JoinColumn of another field.
// There's also some related code for this case over in
// PropertyBinder#handleInferredMapsIdProperty
private static boolean handleInferredMapsId(
AnnotatedJoinColumns columns,
PropertyData propertyData,
MemberDetails property,
MetadataBuildingContext context) {
if ( context.getBuildingOptions().isMapsIdInferenceEnabled() ) {
//Make sure that JPA1 key-many-to-one columns are read only too
boolean hasInferredMapsId = false;
final JoinColumn joinColumn = property.getDirectAnnotationUsage( JoinColumn.class );
if ( joinColumn != null
&& property.hasDirectAnnotationUsage( ManyToOne.class )
&& !property.hasDirectAnnotationUsage( MapsId.class ) ) {
final String joinColumnName = joinColumn.name();
if ( isNotBlank( joinColumnName ) ) {
for ( MemberDetails member : propertyData.getDeclaringClass().getFields() ) {
if ( member.hasDirectAnnotationUsage( Id.class )
//TODO Explicit @Column should not be mandatory here
&& member.hasDirectAnnotationUsage( Column.class ) ) {
final String columnName = member.getDirectAnnotationUsage( Column.class ).name();
if ( joinColumnName.equals( columnName ) ) {
hasInferredMapsId = true;
for ( AnnotatedJoinColumn column : columns.getJoinColumns() ) {
column.setInsertable( false );
column.setUpdatable( false );
}
}
}
}
}

}
return hasInferredMapsId;
}
else {
return false;
}
}

private static void processManyToOneProperty(
String cascadeStrategy,
AnnotatedJoinColumns columns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ public MetadataBuilder enableGlobalNationalizedCharacterDataSupport(boolean enab
return getThis();
}

@Override
public MetadataBuilder enableMapsIdInference(boolean enabled) {
delegate.enableMapsIdInference( enabled );
return getThis();
}

@Override
public MetadataBuilder applyBasicType(BasicType<?> type) {
delegate.applyBasicType( type );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,6 @@ public boolean useNationalizedCharacterData() {
return delegate.useNationalizedCharacterData();
}

@Override
public boolean isMapsIdInferenceEnabled() {
return delegate.isMapsIdInferenceEnabled();
}

@Override
public boolean isNoConstraintByDefault() {
return delegate.isNoConstraintByDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ void addTableNameBinding(

PropertyData getPropertyAnnotatedWithMapsId(ClassDetails persistentClassDetails, String propertyName);
void addPropertyAnnotatedWithMapsId(ClassDetails entityClassDetails, PropertyData propertyAnnotatedElement);
void addInferredMapsIdProperty(ClassDetails entityClassDetails, PropertyData specJPropertyData, String s);

void addToOneAndIdProperty(ClassDetails entityClassDetails, PropertyData propertyAnnotatedElement);
PropertyData getPropertyAnnotatedWithIdAndToOne(ClassDetails persistentClassDetails, String propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,6 @@ default CollectionSemanticsResolver getPersistentCollectionRepresentationResolve
*/
boolean useNationalizedCharacterData();

/**
* Do we attempt to infer missing {@link jakarta.persistence.MapsId}
* annotations when they are not explicitly specified?
*/
boolean isMapsIdInferenceEnabled();

/**
* Should we <em>disable</em> constraint creation when
* {@link jakarta.persistence.ConstraintMode#PROVIDER_DEFAULT}?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class CustomerInventory implements Serializable, Comparator<CustomerInven

@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "CI_CUSTOMERID", nullable = false)
@MapsId("custId")
private Customer customer;

@ManyToOne(cascade = CascadeType.MERGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataBuilder;

import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
Expand All @@ -22,12 +21,6 @@
*/
public class IdMapManyToOneSpecjTest extends BaseNonConfigCoreFunctionalTestCase {

@Override
protected void configureMetadataBuilder(MetadataBuilder metadataBuilder) {
super.configureMetadataBuilder( metadataBuilder );
metadataBuilder.enableMapsIdInference( true );
}

@Test
public void testComplexIdClass() {
Session s = openSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1491,14 +1491,6 @@ public void addPropertyAnnotatedWithMapsId(

}

@Override
public void addInferredMapsIdProperty(
ClassDetails entityClassDetails,
PropertyData specJPropertyData,
String s) {

}

@Override
public void addToOneAndIdProperty(ClassDetails entityClassDetails, PropertyData propertyAnnotatedElement) {

Expand Down
Loading