code cleanups to Binders

This commit is contained in:
Gavin King 2024-11-11 16:54:04 +01:00
parent f1760ce6a2
commit f7a8144e3c
8 changed files with 210 additions and 185 deletions

View File

@ -254,6 +254,14 @@ public abstract class CollectionBinder {
this.buildingContext = buildingContext;
}
private String getRole() {
return collection.getRole();
}
private InFlightMetadataCollector getMetadataCollector() {
return buildingContext.getMetadataCollector();
}
/**
* The first pass at binding a collection.
*/
@ -859,7 +867,9 @@ public abstract class CollectionBinder {
MetadataBuildingContext buildingContext) {
final CollectionBinder binder;
final CollectionType typeAnnotation = property.getAnnotationUsage( CollectionType.class, buildingContext.getMetadataCollector().getSourceModelBuildingContext() );
final CollectionType typeAnnotation =
property.getAnnotationUsage( CollectionType.class,
buildingContext.getMetadataCollector().getSourceModelBuildingContext() );
if ( typeAnnotation != null ) {
binder = createBinderFromCustomTypeAnnotation( property, typeAnnotation, buildingContext );
// todo (6.0) - technically, these should no longer be needed
@ -1174,8 +1184,8 @@ public abstract class CollectionBinder {
}
public void setTargetEntity(Class<?> targetEntity) {
final SourceModelBuildingContext sourceModelContext = buildingContext.getMetadataCollector().getSourceModelBuildingContext();
final ClassDetailsRegistry classDetailsRegistry = sourceModelContext.getClassDetailsRegistry();
final ClassDetailsRegistry classDetailsRegistry =
getMetadataCollector().getSourceModelBuildingContext().getClassDetailsRegistry();
setTargetEntity( classDetailsRegistry.resolveClassDetails( targetEntity.getName() ) );
}
@ -1225,7 +1235,7 @@ public abstract class CollectionBinder {
//TODO reduce tableBinder != null and oneToMany
scheduleSecondPass( isUnowned );
buildingContext.getMetadataCollector().addCollectionBinding( collection );
getMetadataCollector().addCollectionBinding( collection );
bindProperty();
}
@ -1245,7 +1255,7 @@ public abstract class CollectionBinder {
}
private void scheduleSecondPass(boolean isMappedBy) {
final InFlightMetadataCollector metadataCollector = buildingContext.getMetadataCollector();
final InFlightMetadataCollector metadataCollector = getMetadataCollector();
//many to many may need some second pass information
if ( !oneToMany && isMappedBy ) {
metadataCollector.addMappedBy( getElementType().getName(), mappedBy, propertyName );
@ -1274,7 +1284,7 @@ public abstract class CollectionBinder {
private void bindExplicitTypes() {
// set explicit type information
final InFlightMetadataCollector metadataCollector = buildingContext.getMetadataCollector();
final InFlightMetadataCollector metadataCollector = getMetadataCollector();
if ( explicitType != null ) {
final TypeDefinition typeDef = metadataCollector.getTypeDefinition( explicitType );
if ( typeDef == null ) {
@ -1402,7 +1412,7 @@ public abstract class CollectionBinder {
final SQLSelect sqlSelect = property.getDirectAnnotationUsage( SQLSelect.class );
if ( sqlSelect != null ) {
final String loaderName = collection.getRole() + "$SQLSelect";
final String loaderName = getRole() + "$SQLSelect";
collection.setLoaderName( loaderName );
// TODO: pass in the collection element type here
QueryBinder.bindNativeQuery( loaderName, sqlSelect, null, buildingContext );
@ -1410,7 +1420,7 @@ public abstract class CollectionBinder {
final HQLSelect hqlSelect = property.getDirectAnnotationUsage( HQLSelect.class );
if ( hqlSelect != null ) {
final String loaderName = collection.getRole() + "$HQLSelect";
final String loaderName = getRole() + "$HQLSelect";
collection.setLoaderName( loaderName );
QueryBinder.bindQuery( loaderName, hqlSelect, buildingContext );
}
@ -1517,12 +1527,12 @@ public abstract class CollectionBinder {
}
private SourceModelBuildingContext sourceModelContext() {
return buildingContext.getMetadataCollector().getSourceModelBuildingContext();
return getMetadataCollector().getSourceModelBuildingContext();
}
private void handleFetchProfileOverrides() {
property.forEachAnnotationUsage( FetchProfileOverride.class, sourceModelContext(), (usage) -> {
buildingContext.getMetadataCollector().addSecondPass( new FetchSecondPass(
getMetadataCollector().addSecondPass( new FetchSecondPass(
usage,
propertyHolder,
propertyName,
@ -1702,7 +1712,7 @@ public abstract class CollectionBinder {
}
oneToMany.setAssociatedClass( associatedClass );
final Map<String, Join> joins = buildingContext.getMetadataCollector().getJoins( referencedEntityName );
final Map<String, Join> joins = getMetadataCollector().getJoins( referencedEntityName );
foreignJoinColumns.setPropertyHolder( buildPropertyHolder(
associatedClass,
joins,
@ -1717,7 +1727,7 @@ public abstract class CollectionBinder {
collection.setCollectionTable( foreignJoinColumns.getTable() );
}
if ( LOG.isDebugEnabled() ) {
LOG.debugf( "Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName() );
LOG.debugf( "Mapping collection: %s -> %s", getRole(), collection.getCollectionTable().getName() );
}
bindSynchronize();
@ -1733,7 +1743,7 @@ public abstract class CollectionBinder {
}
private void createOneToManyBackref(org.hibernate.mapping.OneToMany oneToMany) {
final InFlightMetadataCollector collector = buildingContext.getMetadataCollector();
final InFlightMetadataCollector collector = getMetadataCollector();
// for non-inverse one-to-many, with a not-null fk, add a backref!
final String entityName = oneToMany.getReferencedEntityName();
final PersistentClass referenced = collector.getEntityBinding( entityName );
@ -1745,15 +1755,16 @@ public abstract class CollectionBinder {
backref.setOptional( true );
backref.setUpdateable( false);
backref.setSelectable( false );
backref.setCollectionRole( collection.getRole() );
backref.setCollectionRole( getRole() );
backref.setEntityName( collection.getOwner().getEntityName() );
backref.setValue( collection.getKey() );
referenced.addProperty( backref );
}
private void handleJpaOrderBy(Collection collection, PersistentClass associatedClass) {
if ( jpaOrderBy != null ) {
final String orderByFragment = buildOrderByClauseFromHql( jpaOrderBy.value(), associatedClass );
final String hqlOrderBy = extractHqlOrderBy( jpaOrderBy );
if ( hqlOrderBy != null ) {
final String orderByFragment = buildOrderByClauseFromHql( hqlOrderBy, associatedClass );
if ( isNotEmpty( orderByFragment ) ) {
collection.setOrderBy( orderByFragment );
}
@ -1763,22 +1774,20 @@ public abstract class CollectionBinder {
private void bindSynchronize() {
final Synchronize synchronizeAnnotation = property.getDirectAnnotationUsage( Synchronize.class );
if ( synchronizeAnnotation != null ) {
final JdbcEnvironment jdbcEnvironment = buildingContext.getMetadataCollector().getDatabase().getJdbcEnvironment();
for ( String table : synchronizeAnnotation.value() ) {
String physicalName = synchronizeAnnotation.logical()
? toPhysicalName( jdbcEnvironment, table )
: table;
final String physicalName =
synchronizeAnnotation.logical()
? toPhysicalName( table )
: table;
collection.addSynchronizedTable( physicalName );
}
}
}
private String toPhysicalName(JdbcEnvironment jdbcEnvironment, String logicalName) {
private String toPhysicalName(String logicalName) {
final JdbcEnvironment jdbcEnvironment = getMetadataCollector().getDatabase().getJdbcEnvironment();
return buildingContext.getBuildingOptions().getPhysicalNamingStrategy()
.toPhysicalTableName(
jdbcEnvironment.getIdentifierHelper().toIdentifier( logicalName ),
jdbcEnvironment
)
.toPhysicalTableName( jdbcEnvironment.getIdentifierHelper().toIdentifier( logicalName ), jdbcEnvironment )
.render( jdbcEnvironment.getDialect() );
}
@ -1863,7 +1872,8 @@ public abstract class CollectionBinder {
}
private String getWhereJoinTableClause() {
final SQLJoinTableRestriction joinTableRestriction = property.getDirectAnnotationUsage( SQLJoinTableRestriction.class );
final SQLJoinTableRestriction joinTableRestriction =
property.getDirectAnnotationUsage( SQLJoinTableRestriction.class );
return joinTableRestriction != null ? joinTableRestriction.value() : null;
}
@ -1878,7 +1888,8 @@ public abstract class CollectionBinder {
}
private String getWhereOnCollectionClause() {
final SQLRestriction restrictionOnCollection = getOverridableAnnotation( property, SQLRestriction.class, getBuildingContext() );
final SQLRestriction restrictionOnCollection =
getOverridableAnnotation( property, SQLRestriction.class, getBuildingContext() );
return restrictionOnCollection != null ? restrictionOnCollection.value() : null;
}
@ -1939,7 +1950,7 @@ public abstract class CollectionBinder {
}
private String getDefaultFilterCondition(String name, Annotation annotation) {
final FilterDefinition definition = buildingContext.getMetadataCollector().getFilterDefinition( name );
final FilterDefinition definition = getMetadataCollector().getFilterDefinition( name );
if ( definition == null ) {
throw new AnnotationException( "Collection '" + qualify( propertyHolder.getPath(), propertyName )
+ "' has a '@" + annotation.annotationType().getSimpleName()
@ -1981,6 +1992,7 @@ public abstract class CollectionBinder {
public void setMapKey(MapKey key) {
hasMapKeyProperty = key != null;
if ( hasMapKeyProperty ) {
// JPA says: if missing, use primary key of associated entity
mapKeyPropertyName = nullIfEmpty( key.name() );
}
}
@ -2167,7 +2179,7 @@ public abstract class CollectionBinder {
final String entityName = joinColumns.getManyToManyOwnerSideEntityName() != null
? "inverse__" + joinColumns.getManyToManyOwnerSideEntityName()
: joinColumns.getPropertyHolder().getEntityName();
final InFlightMetadataCollector collector = buildingContext.getMetadataCollector();
final InFlightMetadataCollector collector = getMetadataCollector();
final String referencedProperty = collector.getPropertyReferencedAssociation( entityName, mappedBy );
if ( referencedProperty != null ) {
collection.setReferencedPropertyName( referencedProperty );
@ -2238,14 +2250,8 @@ public abstract class CollectionBinder {
propertyHolder.startingProperty( property );
}
final CollectionPropertyHolder holder = buildPropertyHolder(
collection,
collection.getRole(),
elementClass,
property,
propertyHolder,
buildingContext
);
final CollectionPropertyHolder holder =
buildPropertyHolder( collection, getRole(), elementClass, property, propertyHolder, buildingContext );
final Class<? extends CompositeUserType<?>> compositeUserType =
resolveCompositeUserType( property, elementClass, buildingContext );
@ -2370,13 +2376,13 @@ public abstract class CollectionBinder {
// todo : force in the case of Convert annotation(s) with embedded paths (beyond key/value prefixes)?
return isEmbedded || attributeOverride
? EMBEDDABLE
: buildingContext.getMetadataCollector().getClassType( elementClass );
: getMetadataCollector().getClassType( elementClass );
}
}
protected boolean mappingDefinedAttributeOverrideOnElement(MemberDetails property) {
return property.hasDirectAnnotationUsage( AttributeOverride.class )
|| property.hasDirectAnnotationUsage( AttributeOverrides.class );
|| property.hasDirectAnnotationUsage( AttributeOverrides.class );
}
static AnnotatedColumns createElementColumnsIfNecessary(
@ -2516,7 +2522,7 @@ public abstract class CollectionBinder {
private void handleOwnedManyToMany(PersistentClass collectionEntity, boolean isCollectionOfEntities) {
//TODO: only for implicit columns?
//FIXME NamingStrategy
final InFlightMetadataCollector collector = buildingContext.getMetadataCollector();
final InFlightMetadataCollector collector = getMetadataCollector();
final PersistentClass owner = collection.getOwner();
joinColumns.setMappedBy(
owner.getEntityName(),
@ -2616,11 +2622,12 @@ public abstract class CollectionBinder {
+ "' which does not exist in the target entity '" + elementType.getName() + "'" );
}
final Value otherSidePropertyValue = otherSideProperty.getValue();
final Table table = otherSidePropertyValue instanceof Collection
// this is a collection on the other side
? ( (Collection) otherSidePropertyValue ).getCollectionTable()
// this is a ToOne with a @JoinTable or a regular property
: otherSidePropertyValue.getTable();
final Table table =
otherSidePropertyValue instanceof Collection collectionProperty
// this is a collection on the other side
? collectionProperty.getCollectionTable()
// this is a ToOne with a @JoinTable or a regular property
: otherSidePropertyValue.getTable();
collection.setCollectionTable( table );
processSoftDeletes();
@ -2666,7 +2673,7 @@ public abstract class CollectionBinder {
return " targets the type '" + elementType.getName() + "'" + problem;
}
private Class<? extends EmbeddableInstantiator> resolveCustomInstantiator(
private static Class<? extends EmbeddableInstantiator> resolveCustomInstantiator(
MemberDetails property,
TypeDetails propertyClass,
MetadataBuildingContext context) {
@ -2710,15 +2717,12 @@ public abstract class CollectionBinder {
return null;
}
private String extractHqlOrderBy(OrderBy jpaOrderBy) {
if ( jpaOrderBy != null ) {
// Null not possible. In case of empty expression, apply default ordering.
return jpaOrderBy.value();
}
else {
// @OrderBy not found.
return null;
}
private static String extractHqlOrderBy(OrderBy jpaOrderBy) {
return jpaOrderBy != null
// Null not possible. In case of empty expression, apply default ordering.
? jpaOrderBy.value()
// @OrderBy not found.
: null;
}
private static void checkFilterConditions(Collection collection) {
@ -2859,21 +2863,22 @@ public abstract class CollectionBinder {
for ( Selectable selectable: mappedByColumns ) {
firstColumn.linkValueUsingAColumnCopy( (Column) selectable, value);
}
final String referencedPropertyName = buildingContext.getMetadataCollector()
.getPropertyReferencedAssociation( targetEntity.getEntityName(), mappedBy );
final InFlightMetadataCollector metadataCollector = getMetadataCollector();
final String referencedPropertyName =
metadataCollector.getPropertyReferencedAssociation( targetEntity.getEntityName(), mappedBy );
final ManyToOne manyToOne = (ManyToOne) value;
if ( referencedPropertyName != null ) {
//TODO always a many to one?
( (ManyToOne) value).setReferencedPropertyName( referencedPropertyName );
buildingContext.getMetadataCollector()
.addUniquePropertyReference( targetEntity.getEntityName(), referencedPropertyName );
manyToOne.setReferencedPropertyName( referencedPropertyName );
metadataCollector.addUniquePropertyReference( targetEntity.getEntityName(), referencedPropertyName );
}
( (ManyToOne) value).setReferenceToPrimaryKey( referencedPropertyName == null );
manyToOne.setReferenceToPrimaryKey( referencedPropertyName == null );
value.createForeignKey();
}
private static List<Selectable> mappedByColumns(PersistentClass referencedEntity, Property property) {
if ( property.getValue() instanceof Collection ) {
return ( (Collection) property.getValue() ).getKey().getSelectables();
if ( property.getValue() instanceof Collection collection ) {
return collection.getKey().getSelectables();
}
else {
//find the appropriate reference key, can be in a join

View File

@ -201,7 +201,11 @@ public class EntityBinder {
private CacheLayout queryCacheLayout;
private SourceModelBuildingContext getSourceModelContext() {
return context.getMetadataCollector().getSourceModelBuildingContext();
return getMetadataCollector().getSourceModelBuildingContext();
}
private InFlightMetadataCollector getMetadataCollector() {
return context.getMetadataCollector();
}
/**
@ -215,12 +219,14 @@ public class EntityBinder {
LOG.debugf( "Binding entity from annotated class: %s", clazzToProcess.getName() );
}
final InFlightMetadataCollector collector = context.getMetadataCollector();
//TODO: be more strict with secondary table allowance (not for ids, not for secondary table join columns etc)
final InheritanceState inheritanceState = inheritanceStates.get( clazzToProcess );
final PersistentClass superEntity = getSuperEntity( clazzToProcess, inheritanceStates, context, inheritanceState );
final PersistentClass persistentClass = makePersistentClass( inheritanceState, superEntity, context );
checkOverrides( clazzToProcess, superEntity, context.getMetadataCollector().getSourceModelBuildingContext() );
checkOverrides( clazzToProcess, superEntity, collector.getSourceModelBuildingContext() );
final EntityBinder entityBinder = new EntityBinder( clazzToProcess, persistentClass, context );
entityBinder.bindEntity();
@ -240,7 +246,6 @@ public class EntityBinder {
entityBinder.handleInheritance( inheritanceState, superEntity, holder );
entityBinder.handleIdentifier( holder, inheritanceStates, inheritanceState );
final InFlightMetadataCollector collector = context.getMetadataCollector();
if ( persistentClass instanceof RootClass rootClass ) {
collector.addSecondPass( new CreateKeySecondPass( rootClass ) );
bindSoftDelete( clazzToProcess, rootClass, context );
@ -327,7 +332,8 @@ public class EntityBinder {
ClassDetails classToCheck = classDetails.getSuperClass();
while ( classToCheck != null ) {
final SoftDelete fromSuper = classToCheck.getAnnotationUsage( SoftDelete.class, sourceModelContext );
if ( fromSuper != null && classToCheck.hasAnnotationUsage( jakarta.persistence.MappedSuperclass.class, sourceModelContext ) ) {
if ( fromSuper != null
&& classToCheck.hasAnnotationUsage( jakarta.persistence.MappedSuperclass.class, sourceModelContext ) ) {
return fromSuper;
}
@ -366,7 +372,8 @@ public class EntityBinder {
}
private void callTypeBinders(PersistentClass persistentClass) {
final List<? extends Annotation> metaAnnotatedList = annotatedClass.getMetaAnnotated( TypeBinderType.class, getSourceModelContext() );
final List<? extends Annotation> metaAnnotatedList =
annotatedClass.getMetaAnnotated( TypeBinderType.class, getSourceModelContext() );
for ( Annotation metaAnnotated : metaAnnotatedList ) {
applyTypeBinder( metaAnnotated, persistentClass );
}
@ -424,9 +431,8 @@ public class EntityBinder {
TableBinder.addTableOptions( table, jpaTableUsage.options() );
}
final InFlightMetadataCollector.EntityTableXref entityTableXref = context
.getMetadataCollector()
.getEntityTableXref( persistentClass.getEntityName() );
final InFlightMetadataCollector.EntityTableXref entityTableXref =
getMetadataCollector().getEntityTableXref( persistentClass.getEntityName() );
annotatedClass.forEachAnnotationUsage( jakarta.persistence.SecondaryTable.class, getSourceModelContext(), (usage) -> {
final Identifier secondaryTableLogicalName = toIdentifier( usage.name() );
@ -476,7 +482,9 @@ public class EntityBinder {
if ( classWithIdClass != null ) {
final IdClass idClassAnn = classWithIdClass.getDirectAnnotationUsage( IdClass.class );
final Class<?> idClassValue = idClassAnn.value();
final ClassDetails compositeClass = context.getMetadataCollector().getSourceModelBuildingContext().getClassDetailsRegistry().resolveClassDetails( idClassValue.getName() );
final ClassDetails compositeClass =
getMetadataCollector().getSourceModelBuildingContext().getClassDetailsRegistry()
.resolveClassDetails( idClassValue.getName() );
final TypeDetails compositeType = new ClassTypeDetailsImpl( compositeClass, TypeDetails.Kind.CLASS );
final TypeDetails classWithIdType = new ClassTypeDetailsImpl( classWithIdClass, TypeDetails.Kind.CLASS );
@ -657,7 +665,8 @@ public class EntityBinder {
}
else {
final SourceModelBuildingContext sourceModelContext = context.getMetadataCollector().getSourceModelBuildingContext();
final SourceModelBuildingContext sourceModelContext =
context.getMetadataCollector().getSourceModelBuildingContext();
final IdClass idClass = associatedClassWithIdClass.getAnnotationUsage( IdClass.class, sourceModelContext );
return compositeClass.getName().equals( idClass.value().getName() );
}
@ -731,7 +740,8 @@ public class EntityBinder {
final String table;
final String catalog;
final UniqueConstraint[] uniqueConstraints;
final jakarta.persistence.Table tableAnnotation = annotatedClass.getAnnotationUsage( jakarta.persistence.Table.class, getSourceModelContext() );
final jakarta.persistence.Table tableAnnotation =
annotatedClass.getAnnotationUsage( jakarta.persistence.Table.class, getSourceModelContext() );
if ( tableAnnotation != null ) {
table = tableAnnotation.name();
schema = tableAnnotation.schema();
@ -778,7 +788,7 @@ public class EntityBinder {
rowId == null ? null : rowId.value(),
view == null ? null : view.query(),
inheritanceState.hasDenormalizedTable()
? context.getMetadataCollector().getEntityTableXref( superEntity.getEntityName() )
? getMetadataCollector().getEntityTableXref( superEntity.getEntityName() )
: null
);
}
@ -838,10 +848,9 @@ public class EntityBinder {
final OnDelete onDelete = annotatedClass.getAnnotationUsage( OnDelete.class, getSourceModelContext() );
key.setOnDeleteAction( onDelete == null ? null : onDelete.action() );
//we are never in a second pass at that stage, so queue it
context.getMetadataCollector()
.addSecondPass( new JoinedSubclassFkSecondPass( jsc, joinColumns, key, context) );
context.getMetadataCollector()
.addSecondPass( new CreateKeySecondPass( jsc ) );
final InFlightMetadataCollector metadataCollector = getMetadataCollector();
metadataCollector.addSecondPass( new JoinedSubclassFkSecondPass( jsc, joinColumns, key, context) );
metadataCollector.addSecondPass( new CreateKeySecondPass( jsc ) );
}
final AnnotatedDiscriminatorColumn discriminatorColumn = processJoinedDiscriminatorProperties( state );
@ -936,8 +945,7 @@ public class EntityBinder {
rootClass.setPolymorphic( true );
final String rootEntityName = rootClass.getEntityName();
LOG.tracev( "Setting discriminator for entity {0}", rootEntityName);
context.getMetadataCollector()
.addSecondPass( new NullableDiscriminatorColumnSecondPass( rootEntityName ) );
getMetadataCollector().addSecondPass( new NullableDiscriminatorColumnSecondPass( rootEntityName ) );
}
}
@ -945,10 +953,13 @@ public class EntityBinder {
* Process all discriminator-related metadata per rules for "single table" inheritance
*/
private AnnotatedDiscriminatorColumn processSingleTableDiscriminatorProperties(InheritanceState inheritanceState) {
final DiscriminatorColumn discriminatorColumn = annotatedClass.getAnnotationUsage( DiscriminatorColumn.class, getSourceModelContext() );
final DiscriminatorFormula discriminatorFormula = getOverridableAnnotation( annotatedClass, DiscriminatorFormula.class, context );
final DiscriminatorColumn discriminatorColumn =
annotatedClass.getAnnotationUsage( DiscriminatorColumn.class, getSourceModelContext() );
final DiscriminatorFormula discriminatorFormula =
getOverridableAnnotation( annotatedClass, DiscriminatorFormula.class, context );
if ( !inheritanceState.hasParents() || annotatedClass.hasAnnotationUsage( Inheritance.class, getSourceModelContext() ) ) {
if ( !inheritanceState.hasParents()
|| annotatedClass.hasAnnotationUsage( Inheritance.class, getSourceModelContext() ) ) {
return buildDiscriminatorColumn(
discriminatorColumn,
discriminatorFormula,
@ -982,8 +993,10 @@ public class EntityBinder {
+ "' has 'JOINED' inheritance and is annotated '@DiscriminatorFormula'" );
}
final DiscriminatorColumn discriminatorColumn = annotatedClass.getAnnotationUsage( DiscriminatorColumn.class, getSourceModelContext() );
if ( !inheritanceState.hasParents() || annotatedClass.hasAnnotationUsage( Inheritance.class, getSourceModelContext() ) ) {
final DiscriminatorColumn discriminatorColumn =
annotatedClass.getAnnotationUsage( DiscriminatorColumn.class, getSourceModelContext() );
if ( !inheritanceState.hasParents()
|| annotatedClass.hasAnnotationUsage( Inheritance.class, getSourceModelContext() ) ) {
return useDiscriminatorColumnForJoined( discriminatorColumn )
? buildDiscriminatorColumn( discriminatorColumn, null, null, DEFAULT_DISCRIMINATOR_COLUMN_NAME, context )
: null;
@ -1009,14 +1022,14 @@ public class EntityBinder {
*/
private boolean useDiscriminatorColumnForJoined(DiscriminatorColumn discriminatorColumn) {
if ( discriminatorColumn != null ) {
boolean ignore = context.getBuildingOptions().ignoreExplicitDiscriminatorsForJoinedInheritance();
final boolean ignore = context.getBuildingOptions().ignoreExplicitDiscriminatorsForJoinedInheritance();
if ( ignore ) {
LOG.debugf( "Ignoring explicit @DiscriminatorColumn annotation on: %s", annotatedClass.getName() );
}
return !ignore;
}
else {
boolean createImplicit = context.getBuildingOptions().createImplicitDiscriminatorsForJoinedInheritance();
final boolean createImplicit = context.getBuildingOptions().createImplicitDiscriminatorsForJoinedInheritance();
if ( createImplicit ) {
LOG.debugf( "Inferring implicit @DiscriminatorColumn using defaults for: %s", annotatedClass.getName() );
}
@ -1121,9 +1134,11 @@ public class EntityBinder {
final AnnotatedJoinColumns joinColumns = new AnnotatedJoinColumns();
joinColumns.setBuildingContext( context );
final SourceModelBuildingContext sourceModelContext = context.getMetadataCollector().getSourceModelBuildingContext();
final SourceModelBuildingContext sourceModelContext =
context.getMetadataCollector().getSourceModelBuildingContext();
final PrimaryKeyJoinColumns primaryKeyJoinColumns = clazzToProcess.getAnnotationUsage( PrimaryKeyJoinColumns.class, sourceModelContext );
final PrimaryKeyJoinColumns primaryKeyJoinColumns =
clazzToProcess.getAnnotationUsage( PrimaryKeyJoinColumns.class, sourceModelContext );
if ( primaryKeyJoinColumns != null ) {
final PrimaryKeyJoinColumn[] columns = primaryKeyJoinColumns.value();
if ( !ArrayHelper.isEmpty( columns ) ) {
@ -1138,7 +1153,8 @@ public class EntityBinder {
}
}
else {
final PrimaryKeyJoinColumn columnAnnotation = clazzToProcess.getAnnotationUsage( PrimaryKeyJoinColumn.class, sourceModelContext );
final PrimaryKeyJoinColumn columnAnnotation =
clazzToProcess.getAnnotationUsage( PrimaryKeyJoinColumn.class, sourceModelContext );
buildInheritanceJoinColumn(
columnAnnotation,
null,
@ -1171,8 +1187,9 @@ public class EntityBinder {
return null;
}
else {
final PersistentClass superEntity = context.getMetadataCollector()
.getEntityBinding( superState.getClassDetails().getName() );
final PersistentClass superEntity =
context.getMetadataCollector()
.getEntityBinding( superState.getClassDetails().getName() );
//check if superclass is not a potential persistent class
if ( superEntity == null && inheritanceState.hasParents() ) {
throw new AssertionFailure( "Subclass has to be bound after its parent class: "
@ -1185,7 +1202,8 @@ public class EntityBinder {
/**
* See {@link JpaEventListener} for a better (?) alternative
*/
private static void bindCallbacks(ClassDetails entityClass, PersistentClass persistentClass, MetadataBuildingContext context) {
private static void bindCallbacks(
ClassDetails entityClass, PersistentClass persistentClass, MetadataBuildingContext context) {
for ( CallbackType callbackType : CallbackType.values() ) {
persistentClass.addCallbackDefinitions( CallbackDefinitionResolver.resolveEntityCallbacks(
context,
@ -1249,22 +1267,27 @@ public class EntityBinder {
}
private void bindRowManagement() {
final DynamicInsert dynamicInsertAnn = annotatedClass.getAnnotationUsage( DynamicInsert.class, getSourceModelContext() );
final DynamicInsert dynamicInsertAnn =
annotatedClass.getAnnotationUsage( DynamicInsert.class, getSourceModelContext() );
persistentClass.setDynamicInsert( dynamicInsertAnn != null );
final DynamicUpdate dynamicUpdateAnn = annotatedClass.getAnnotationUsage( DynamicUpdate.class, getSourceModelContext() );
final DynamicUpdate dynamicUpdateAnn =
annotatedClass.getAnnotationUsage( DynamicUpdate.class, getSourceModelContext() );
persistentClass.setDynamicUpdate( dynamicUpdateAnn != null );
if ( persistentClass.useDynamicInsert() && annotatedClass.hasAnnotationUsage( SQLInsert.class, getSourceModelContext() ) ) {
if ( persistentClass.useDynamicInsert()
&& annotatedClass.hasAnnotationUsage( SQLInsert.class, getSourceModelContext() ) ) {
throw new AnnotationException( "Entity '" + name + "' is annotated both '@DynamicInsert' and '@SQLInsert'" );
}
if ( persistentClass.useDynamicUpdate() && annotatedClass.hasAnnotationUsage( SQLUpdate.class, getSourceModelContext() ) ) {
if ( persistentClass.useDynamicUpdate()
&& annotatedClass.hasAnnotationUsage( SQLUpdate.class, getSourceModelContext() ) ) {
throw new AnnotationException( "Entity '" + name + "' is annotated both '@DynamicUpdate' and '@SQLUpdate'" );
}
}
private void bindOptimisticLocking() {
final OptimisticLocking optimisticLockingAnn = annotatedClass.getAnnotationUsage( OptimisticLocking.class, getSourceModelContext() );
final OptimisticLocking optimisticLockingAnn =
annotatedClass.getAnnotationUsage( OptimisticLocking.class, getSourceModelContext() );
persistentClass.setOptimisticLockStyle( fromLockType( optimisticLockingAnn == null
? OptimisticLockType.VERSION
: optimisticLockingAnn.type() ) );
@ -1347,10 +1370,11 @@ public class EntityBinder {
private void registerImportName() {
LOG.debugf( "Import with entity name %s", name );
try {
context.getMetadataCollector().addImport( name, persistentClass.getEntityName() );
final InFlightMetadataCollector metadataCollector = getMetadataCollector();
metadataCollector.addImport( name, persistentClass.getEntityName() );
final String entityName = persistentClass.getEntityName();
if ( !entityName.equals( name ) ) {
context.getMetadataCollector().addImport( entityName, entityName );
metadataCollector.addImport( entityName, entityName );
}
}
catch (MappingException me) {
@ -1461,12 +1485,10 @@ public class EntityBinder {
// - if so, we return the matched override
// - if not, we return the normal SQLInsert (if one)
final Class<Annotation> overrideAnnotation = getOverrideAnnotation( annotationType );
final Annotation[] dialectOverrides = annotatedClass.getRepeatedAnnotationUsages(
overrideAnnotation,
getSourceModelContext()
);
final Annotation[] dialectOverrides =
annotatedClass.getRepeatedAnnotationUsages( overrideAnnotation, getSourceModelContext() );
if ( isNotEmpty( dialectOverrides ) ) {
final Dialect dialect = context.getMetadataCollector().getDatabase().getDialect();
final Dialect dialect = getMetadataCollector().getDatabase().getDialect();
for ( int i = 0; i < dialectOverrides.length; i++ ) {
//noinspection unchecked
final DialectOverrider<A> dialectOverride = (DialectOverrider<A>) dialectOverrides[i];
@ -1507,7 +1529,7 @@ public class EntityBinder {
}
private String getDefaultFilterCondition(String filterName) {
final FilterDefinition definition = context.getMetadataCollector().getFilterDefinition( filterName );
final FilterDefinition definition = getMetadataCollector().getFilterDefinition( filterName );
if ( definition == null ) {
throw new AnnotationException( "Entity '" + name
+ "' has a '@Filter' for an undefined filter named '" + filterName + "'" );
@ -1523,16 +1545,14 @@ public class EntityBinder {
private void bindSynchronize() {
final Synchronize synchronize = annotatedClass.getAnnotationUsage( Synchronize.class, getSourceModelContext() );
if ( synchronize == null ) {
return;
}
final JdbcEnvironment jdbcEnvironment = context.getMetadataCollector().getDatabase().getJdbcEnvironment();
final boolean logical = synchronize.logical();
final String[] tableNames = synchronize.value();
for ( String tableName : tableNames ) {
String physicalName = logical ? toPhysicalName( jdbcEnvironment, tableName ) : tableName;
persistentClass.addSynchronizedTable( physicalName );
if ( synchronize != null ) {
final JdbcEnvironment jdbcEnvironment = getMetadataCollector().getDatabase().getJdbcEnvironment();
final boolean logical = synchronize.logical();
final String[] tableNames = synchronize.value();
for ( String tableName : tableNames ) {
final String physicalName = logical ? toPhysicalName( jdbcEnvironment, tableName ) : tableName;
persistentClass.addSynchronizedTable( physicalName );
}
}
}
@ -1553,16 +1573,16 @@ public class EntityBinder {
}
private void processNamedEntityGraph(NamedEntityGraph annotation) {
if ( annotation == null ) {
return;
if ( annotation != null ) {
getMetadataCollector()
.addNamedEntityGraph( new NamedEntityGraphDefinition( annotation, name,
persistentClass.getEntityName() ) );
}
context.getMetadataCollector().addNamedEntityGraph(
new NamedEntityGraphDefinition( annotation, name, persistentClass.getEntityName() )
);
}
public void bindDiscriminatorValue() {
final DiscriminatorValue discriminatorValueAnn = annotatedClass.getAnnotationUsage( DiscriminatorValue.class, getSourceModelContext() );
final DiscriminatorValue discriminatorValueAnn =
annotatedClass.getAnnotationUsage( DiscriminatorValue.class, getSourceModelContext() );
final String discriminatorValue = discriminatorValueAnn != null
? discriminatorValueAnn.value()
: null;
@ -1594,7 +1614,8 @@ public class EntityBinder {
}
public void bindConcreteProxy() {
final ConcreteProxy annotationUsage = annotatedClass.getAnnotationUsage( ConcreteProxy.class, getSourceModelContext() );
final ConcreteProxy annotationUsage =
annotatedClass.getAnnotationUsage( ConcreteProxy.class, getSourceModelContext() );
if ( annotationUsage != null ) {
if ( persistentClass.getSuperclass() != null ) {
throw new AnnotationException( "Entity class '" + persistentClass.getClassName()
@ -1617,7 +1638,8 @@ public class EntityBinder {
private void bindNaturalIdCache() {
naturalIdCacheRegion = null;
final NaturalIdCache naturalIdCacheAnn = annotatedClass.getAnnotationUsage( NaturalIdCache.class, getSourceModelContext() );
final NaturalIdCache naturalIdCacheAnn =
annotatedClass.getAnnotationUsage( NaturalIdCache.class, getSourceModelContext() );
if ( naturalIdCacheAnn == null ) {
return;
}
@ -1686,7 +1708,8 @@ public class EntityBinder {
cacheRegion = effectiveCache.region();
cacheLazyProperty = isCacheLazy( effectiveCache, annotatedClass );
final QueryCacheLayout queryCache = annotatedClass.getAnnotationUsage( QueryCacheLayout.class, getSourceModelContext() );
final QueryCacheLayout queryCache =
annotatedClass.getAnnotationUsage( QueryCacheLayout.class, getSourceModelContext() );
queryCacheLayout = queryCache == null ? null : queryCache.layout();
}
@ -1722,7 +1745,8 @@ public class EntityBinder {
}
private static Cache buildCacheMock(ClassDetails classDetails, MetadataBuildingContext context) {
final CacheAnnotation cacheUsage = HibernateAnnotations.CACHE.createUsage( context.getMetadataCollector().getSourceModelBuildingContext() );
final CacheAnnotation cacheUsage =
HibernateAnnotations.CACHE.createUsage( context.getMetadataCollector().getSourceModelBuildingContext() );
cacheUsage.region( classDetails.getName() );
cacheUsage.usage( determineCacheConcurrencyStrategy( context ) );
return cacheUsage;
@ -1802,7 +1826,7 @@ public class EntityBinder {
);
}
final InFlightMetadataCollector collector = context.getMetadataCollector();
final InFlightMetadataCollector collector = getMetadataCollector();
final InFlightMetadataCollector.EntityTableXref superTableXref =
collector.getEntityTableXref( entityName );
final Table primaryTable = superTableXref.getPrimaryTable();
@ -1851,12 +1875,11 @@ public class EntityBinder {
// table.setComment( comment.value() );
// }
context.getMetadataCollector()
.addEntityTableXref( entityName, logicalName, table, denormalizedSuperTableXref );
getMetadataCollector().addEntityTableXref( entityName, logicalName, table, denormalizedSuperTableXref );
if ( persistentClass instanceof TableOwner ) {
if ( persistentClass instanceof TableOwner tableOwner ) {
LOG.debugf( "Bind entity %s on table %s", entityName, table.getName() );
( (TableOwner) persistentClass ).setTable( table );
tableOwner.setTable( table );
}
else {
throw new AssertionFailure( "binding a table for a subclass" );
@ -2109,11 +2132,8 @@ public class EntityBinder {
return new QualifiedTableName(
toIdentifier( catalog ),
toIdentifier( schema ),
context.getMetadataCollector()
.getDatabase()
.getJdbcEnvironment()
.getIdentifierHelper()
.toIdentifier( name )
getMetadataCollector().getDatabase().getJdbcEnvironment()
.getIdentifierHelper().toIdentifier( name )
);
}
@ -2129,7 +2149,7 @@ public class EntityBinder {
final String entityName = persistentClass.getEntityName();
final InFlightMetadataCollector.EntityTableXref tableXref
= context.getMetadataCollector().getEntityTableXref( entityName );
= getMetadataCollector().getEntityTableXref( entityName );
assert tableXref != null : "Could not locate EntityTableXref for entity [" + entityName + "]";
tableXref.addSecondaryTable( logicalName, join );
@ -2278,7 +2298,7 @@ public class EntityBinder {
ClassDetails classToProcess = annotatedClass.getSuperClass();
while ( classToProcess != null ) {
final AnnotatedClassType classType = context.getMetadataCollector().getClassType( classToProcess );
final AnnotatedClassType classType = getMetadataCollector().getClassType( classToProcess );
if ( classType == MAPPED_SUPERCLASS ) {
bindFilters( classToProcess );
}

View File

@ -21,6 +21,7 @@ import org.hibernate.mapping.SimpleValue;
import org.hibernate.resource.beans.spi.ManagedBean;
import org.hibernate.usertype.UserCollectionType;
import static org.hibernate.boot.model.internal.PropertyHolderBuilder.buildPropertyHolder;
import static org.hibernate.internal.util.StringHelper.qualify;
/**
@ -65,14 +66,8 @@ public class ListBinder extends CollectionBinder {
}
private void bindIndex() {
final PropertyHolder valueHolder = PropertyHolderBuilder.buildPropertyHolder(
collection,
qualify( collection.getRole(), "key" ),
null,
null,
propertyHolder,
getBuildingContext()
);
final PropertyHolder valueHolder =
buildPropertyHolder( collection, getPath(), null, null, propertyHolder, buildingContext );
if ( !collection.isOneToMany() ) {
indexColumn.forceNotNull();
@ -94,6 +89,10 @@ public class ListBinder extends CollectionBinder {
createBackref();
}
private String getPath() {
return qualify( collection.getRole(), "key" );
}
private void createBackref() {
if ( collection.isOneToMany()
&& !collection.getKey().isNullable()

View File

@ -291,14 +291,8 @@ public class MapBinder extends CollectionBinder {
private CollectionPropertyHolder buildCollectionPropertyHolder(
MemberDetails property,
ClassDetails keyClass) {
final CollectionPropertyHolder holder = buildPropertyHolder(
collection,
qualify( collection.getRole(), "mapkey" ),
keyClass,
property,
propertyHolder,
buildingContext
);
final CollectionPropertyHolder holder =
buildPropertyHolder( collection, getPath(), keyClass, property, propertyHolder, buildingContext );
// 'propertyHolder' is the PropertyHolder for the owner of the collection
// 'holder' is the CollectionPropertyHolder.
// 'property' is the collection XProperty
@ -307,6 +301,10 @@ public class MapBinder extends CollectionBinder {
return holder;
}
private String getPath() {
return qualify( collection.getRole(), "mapkey" );
}
private void handleForeignKey(MemberDetails property, ManyToOne element) {
final ForeignKey foreignKey = getMapKeyForeignKey( property );
if ( foreignKey != null ) {
@ -472,8 +470,8 @@ public class MapBinder extends CollectionBinder {
Collection collection,
PersistentClass associatedClass,
PersistentClass targetPropertyPersistentClass) {
if ( value instanceof Component ) {
return createIndexComponent( collection, associatedClass, (Component) value );
if ( value instanceof Component component ) {
return createIndexComponent( collection, associatedClass, component );
}
else {
// HHH-11005 - only if we are @OneToMany and location of map key property is
@ -481,11 +479,11 @@ public class MapBinder extends CollectionBinder {
final Table mapKeyTable = !associatedClass.equals( targetPropertyPersistentClass )
? targetPropertyPersistentClass.getTable()
: associatedClass.getTable();
if ( value instanceof BasicValue ) {
return createDependantBasicValue( mapKeyTable, (BasicValue) value );
if ( value instanceof BasicValue basicValue ) {
return createDependantBasicValue( mapKeyTable, basicValue );
}
else if ( value instanceof SimpleValue ) {
return createTargetValue( mapKeyTable, (SimpleValue) value );
else if ( value instanceof SimpleValue simpleValue ) {
return createTargetValue( mapKeyTable, simpleValue );
}
else {
throw new AssertionFailure( "Unknown type encountered for map key: " + value.getClass() );
@ -526,11 +524,11 @@ public class MapBinder extends CollectionBinder {
}
private static void addSelectable(SimpleValue targetValue, Selectable selectable) {
if ( selectable instanceof Column ) {
targetValue.addColumn( ( (Column) selectable).clone(), false, false );
if ( selectable instanceof Column column ) {
targetValue.addColumn( column.clone(), false, false );
}
else if ( selectable instanceof Formula ) {
targetValue.addFormula( new Formula( ( (Formula) selectable).getFormula() ) );
else if ( selectable instanceof Formula formula ) {
targetValue.addFormula( new Formula( formula.getFormula() ) );
}
else {
throw new AssertionFailure( "Unknown element in column iterator: " + selectable.getClass() );

View File

@ -60,8 +60,8 @@ class NaturalIdBinder {
final Property property = columns.resolveProperty();
if ( property.isComposite() ) {
for ( Selectable selectable : property.getValue().getSelectables() ) {
if ( selectable instanceof org.hibernate.mapping.Column) {
uniqueKey.addColumn( tableColumn( (org.hibernate.mapping.Column) selectable, table, collector ) );
if ( selectable instanceof org.hibernate.mapping.Column column) {
uniqueKey.addColumn( tableColumn( column, table, collector ) );
}
}
}

View File

@ -442,14 +442,14 @@ public class PropertyBinder {
}
private void handleLob(Property property) {
if ( this.memberDetails != null ) {
if ( memberDetails != null ) {
// HHH-4635 -- needed for dialect-specific property ordering
property.setLob( this.memberDetails.hasDirectAnnotationUsage( Lob.class ) );
property.setLob( memberDetails.hasDirectAnnotationUsage( Lob.class ) );
}
}
private void handleMutability(Property property) {
if ( this.memberDetails != null && this.memberDetails.hasDirectAnnotationUsage( Immutable.class ) ) {
if ( memberDetails != null && memberDetails.hasDirectAnnotationUsage( Immutable.class ) ) {
updatable = false;
}
property.setInsertable( insertable );
@ -457,8 +457,8 @@ public class PropertyBinder {
}
private void handleOptional(Property property) {
if ( this.memberDetails != null ) {
property.setOptional( !isId && isOptional( this.memberDetails, this.holder ) );
if ( memberDetails != null ) {
property.setOptional( !isId && isOptional( memberDetails, holder ) );
if ( property.isOptional() ) {
final OptionalDeterminationSecondPass secondPass = persistentClasses -> {
// Defer determining whether a property and its columns are nullable,
@ -487,8 +487,8 @@ public class PropertyBinder {
}
private void handleNaturalId(Property property) {
if ( this.memberDetails != null && entityBinder != null ) {
final NaturalId naturalId = this.memberDetails.getDirectAnnotationUsage( NaturalId.class );
if ( memberDetails != null && entityBinder != null ) {
final NaturalId naturalId = memberDetails.getDirectAnnotationUsage( NaturalId.class );
if ( naturalId != null ) {
if ( !entityBinder.isRootEntity() ) {
throw new AnnotationException( "Property '" + qualify( holder.getPath(), name )
@ -505,11 +505,11 @@ public class PropertyBinder {
private void inferOptimisticLocking(Property property) {
// this is already handled for collections in CollectionBinder...
if ( value instanceof Collection ) {
property.setOptimisticLocked( ((Collection) value).isOptimisticLocked() );
if ( value instanceof Collection collection ) {
property.setOptimisticLocked( collection.isOptimisticLocked() );
}
else if ( this.memberDetails != null && this.memberDetails.hasDirectAnnotationUsage( OptimisticLock.class ) ) {
final OptimisticLock optimisticLock = this.memberDetails.getDirectAnnotationUsage( OptimisticLock.class );
else if ( memberDetails != null && memberDetails.hasDirectAnnotationUsage( OptimisticLock.class ) ) {
final OptimisticLock optimisticLock = memberDetails.getDirectAnnotationUsage( OptimisticLock.class );
final boolean excluded = optimisticLock.excluded();
validateOptimisticLock( excluded );
property.setOptimisticLocked( !excluded );

View File

@ -533,7 +533,7 @@ public abstract class Collection implements Fetchable, Value, Filterable, SoftDe
@Override
public boolean isSame(Value other) {
return this == other
|| other instanceof Collection && isSame( (Collection) other );
|| other instanceof Collection collection && isSame( collection );
}
protected static boolean isSame(Value v1, Value v2) {
@ -770,8 +770,8 @@ public abstract class Collection implements Fetchable, Value, Filterable, SoftDe
@SuppressWarnings("rawtypes")
public void setTypeParameters(java.util.Map typeParameters) {
if ( typeParameters instanceof Properties ) {
this.typeParameters = (Properties) typeParameters;
if ( typeParameters instanceof Properties properties ) {
this.typeParameters = properties;
}
else {
this.typeParameters = new Properties();

View File

@ -40,9 +40,11 @@ public abstract class IndexedCollection extends Collection {
public Value getIndex() {
return index;
}
public void setIndex(Value index) {
this.index = index;
}
public final boolean isIndexed() {
return true;
}
@ -53,18 +55,18 @@ public abstract class IndexedCollection extends Collection {
@Override
public boolean isSame(Collection other) {
return other instanceof IndexedCollection
&& isSame( (IndexedCollection) other );
return other instanceof IndexedCollection indexedCollection
&& isSame( indexedCollection );
}
public boolean isSame(IndexedCollection other) {
return super.isSame( other )
&& isSame( index, other.index );
&& isSame( index, other.index );
}
void createPrimaryKey() {
if ( !isOneToMany() ) {
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
final PrimaryKey pk = new PrimaryKey( getCollectionTable() );
pk.addColumns( getKey() );
// index should be last column listed
@ -94,6 +96,7 @@ public abstract class IndexedCollection extends Collection {
// }
}
@Deprecated
public void validate(Mapping mapping) throws MappingException {
validate( (MappingContext) mapping);
}