Skip to content
Closed
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
@@ -0,0 +1,11 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.generator;

public interface CompositeOnExecutionGenerator extends OnExecutionGenerator {
OnExecutionGenerator getPropertyGenerator(String propertyName);
boolean[] writePropertyValues();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public abstract class AbstractSingularAttributeMapping
extends AbstractStateArrayContributorMapping
implements SingularAttributeMapping {

private Generator generator;

public AbstractSingularAttributeMapping(
String name,
int stateArrayPosition,
Expand Down Expand Up @@ -52,7 +54,20 @@ protected AbstractSingularAttributeMapping( AbstractSingularAttributeMapping ori

@Override
public Generator getGenerator() {
return findContainingEntityMapping().getEntityPersister().getEntityMetamodel().getGenerators()[getStateArrayPosition()];
if ( generator != null ) {
return generator;
}
final int stateArrayPosition = getStateArrayPosition();
if ( stateArrayPosition < 0 ) {
return null;
}
final Generator[] generators = findContainingEntityMapping().getEntityPersister().getEntityMetamodel()
.getGenerators();
if ( generators.length == 0 ) {
return null;
}
generator = generators[stateArrayPosition];
return generator;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.CompositeOnExecutionGenerator;
import org.hibernate.generator.Generator;
import org.hibernate.generator.OnExecutionGenerator;
import org.hibernate.mapping.AggregateColumn;
import org.hibernate.mapping.Any;
import org.hibernate.mapping.BasicValue;
Expand Down Expand Up @@ -171,6 +174,7 @@ public static EmbeddableMappingTypeImpl from(
private final boolean aggregateMappingRequiresColumnWriter;
private final boolean preferSelectAggregateMapping;
private final boolean preferBindAggregateMapping;
private final CompositeOnExecutionGenerator generator;

private EmbeddableMappingTypeImpl(
Component bootDescriptor,
Expand Down Expand Up @@ -251,6 +255,7 @@ private EmbeddableMappingTypeImpl(
this.preferSelectAggregateMapping = false;
this.preferBindAggregateMapping = false;
}
this.generator = resolveOnExecutionGenerator( valueMapping );
}

private JdbcMapping resolveJdbcMapping(Component bootDescriptor, RuntimeModelCreationContext creationContext) {
Expand Down Expand Up @@ -371,6 +376,7 @@ public EmbeddableMappingTypeImpl(
attributeMappings
)
);
this.generator = resolveOnExecutionGenerator( valueMapping );
}

public EmbeddableMappingType createInverseMappingType(
Expand Down Expand Up @@ -436,13 +442,17 @@ private boolean finishInitialization(

// Reset the attribute mappings that were added in previous attempts
attributeMappings.clear();

for ( final Property bootPropertyDescriptor : bootDescriptor.getProperties() ) {

final AttributeMapping attributeMapping;

final Type subtype = subtypes[attributeIndex];
final Value value = bootPropertyDescriptor.getValue();
final String name = bootPropertyDescriptor.getName();
if ( subtype instanceof BasicType ) {
boolean isInsertable = isInsertable( generator, insertability[columnPosition], name );
boolean isUpdatable = isUpdatable( generator, updateability[columnPosition], name );

final BasicValue basicValue = (BasicValue) value;
final Selectable selectable = dependantValue != null ?
dependantValue.getColumns().get( dependantColumnIndex + columnPosition ) :
Expand Down Expand Up @@ -474,7 +484,7 @@ private boolean finishInitialization(
containingTableExpression = rootTableExpression;
columnExpression = rootTableKeyColumnNames[columnPosition];
}
final NavigableRole role = valueMapping.getNavigableRole().append( bootPropertyDescriptor.getName() );
final NavigableRole role = valueMapping.getNavigableRole().append( name );
final SelectablePath selectablePath;
final String columnDefinition;
final Long length;
Expand Down Expand Up @@ -502,10 +512,10 @@ private boolean finishInitialization(
temporalPrecision = null;
isLob = false;
nullable = bootPropertyDescriptor.isOptional();
selectablePath = new SelectablePath( determineEmbeddablePrefix() + bootPropertyDescriptor.getName() );
selectablePath = new SelectablePath( determineEmbeddablePrefix() + name );
}
attributeMapping = MappingModelCreationHelper.buildBasicAttributeMapping(
bootPropertyDescriptor.getName(),
name,
role,
attributeIndex,
attributeIndex,
Expand All @@ -525,8 +535,8 @@ private boolean finishInitialization(
temporalPrecision,
isLob,
nullable,
insertability[columnPosition],
updateability[columnPosition],
isInsertable,
isUpdatable,
representationStrategy.resolvePropertyAccess( bootPropertyDescriptor ),
compositeType.getCascadeStyle( attributeIndex ),
creationProcess
Expand Down Expand Up @@ -556,7 +566,7 @@ else if ( subtype instanceof AnyType anyType ) {
);

attributeMapping = new DiscriminatedAssociationAttributeMapping(
valueMapping.getNavigableRole().append( bootPropertyDescriptor.getName() ),
valueMapping.getNavigableRole().append( name ),
typeConfiguration.getJavaTypeRegistry().getDescriptor( Object.class ),
this,
attributeIndex,
Expand Down Expand Up @@ -585,7 +595,7 @@ else if ( subtype instanceof CompositeType subCompositeType ) {
}

attributeMapping = MappingModelCreationHelper.buildEmbeddedAttributeMapping(
bootPropertyDescriptor.getName(),
name,
attributeIndex,
attributeIndex,
bootPropertyDescriptor,
Expand All @@ -604,7 +614,7 @@ else if ( subtype instanceof CompositeType subCompositeType ) {
}
else if ( subtype instanceof CollectionType ) {
attributeMapping = MappingModelCreationHelper.buildPluralAttributeMapping(
bootPropertyDescriptor.getName(),
name,
attributeIndex,
attributeIndex,
bootPropertyDescriptor,
Expand All @@ -619,8 +629,8 @@ else if ( subtype instanceof EntityType ) {
final EntityPersister entityPersister = creationProcess.getEntityPersister( bootDescriptor.getOwner().getEntityName() );

attributeMapping = MappingModelCreationHelper.buildSingularAssociationAttributeMapping(
bootPropertyDescriptor.getName(),
valueMapping.getNavigableRole().append( bootPropertyDescriptor.getName() ),
name,
valueMapping.getNavigableRole().append( name ),
attributeIndex,
attributeIndex,
bootPropertyDescriptor,
Expand All @@ -639,7 +649,7 @@ else if ( subtype instanceof EntityType ) {
Locale.ROOT,
"Unable to determine attribute nature : %s#%s",
bootDescriptor.getOwner().getEntityName(),
bootPropertyDescriptor.getName()
name
)
);
}
Expand Down Expand Up @@ -667,6 +677,30 @@ else if ( subtype instanceof EntityType ) {
return true;
}

private boolean isInsertable(CompositeOnExecutionGenerator generator, boolean propertyInsertability, String propertyName) {
if ( propertyInsertability && generator != null ) {
final OnExecutionGenerator propertyGenerator = generator.getPropertyGenerator( propertyName );
if ( propertyGenerator != null
&& propertyGenerator.generatesOnInsert()
&& !propertyGenerator.writePropertyValue() ) {
return false;
}
}
return propertyInsertability;
}

private boolean isUpdatable(CompositeOnExecutionGenerator generator, boolean propertyUpdatability, String propertyName) {
if ( propertyUpdatability && generator != null ) {
final OnExecutionGenerator propertyGenerator = generator.getPropertyGenerator( propertyName );
if ( propertyGenerator != null
&& propertyGenerator.generatesOnUpdate()
&& !propertyGenerator.writePropertyValue() ) {
return false;
}
}
return propertyUpdatability;
}

private boolean isDefinedInClassOrSuperclass(Component bootDescriptor, String declaringClass, String subclass) {
while ( subclass != null ) {
if ( declaringClass.equals( subclass ) ) {
Expand Down Expand Up @@ -1121,4 +1155,25 @@ public boolean shouldSelectAggregateMapping() {
public boolean shouldBindAggregateMapping() {
return preferBindAggregateMapping;
}

private static CompositeOnExecutionGenerator resolveOnExecutionGenerator(EmbeddableValuedModelPart valueMapping) {
if ( valueMapping instanceof EmbeddedAttributeMapping attributeMapping ) {
if ( attributeMapping.getDeclaringType() instanceof EmbeddableMappingTypeImpl embeddableMappingType ) {
if ( embeddableMappingType.getGenerator() instanceof CompositeOnExecutionGenerator compositeOnExecutionGenerator ) {
if ( compositeOnExecutionGenerator.getPropertyGenerator( attributeMapping.getAttributeName() )
instanceof CompositeOnExecutionGenerator composite ) {
return composite;
}
}
}
else if ( attributeMapping.getGenerator() instanceof CompositeOnExecutionGenerator compositeOnExecutionGenerator ) {
return compositeOnExecutionGenerator;
}
}
return null;
}

public Generator getGenerator() {
return generator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ public boolean containsTableReference(String tableExpression) {
return producer.containsTableReference( tableExpression );
}

@Override
public boolean isEntityIdentifierMapping() {
return getAttributeMetadata() instanceof EmbeddedIdentifierMappingImpl;
}

@Override
public int compare(Object value1, Object value2) {
return embeddableMappingType.compare( value1, value2 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.spi.MergeContext;
import org.hibernate.generator.Generator;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
Expand Down Expand Up @@ -332,4 +333,9 @@ public int getNumberOfFetchables() {
public Fetchable getFetchable(int position) {
return getPartMappingType().getFetchable( position );
}

@Override
public Generator getGenerator() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.generator.Generator;
import org.hibernate.metamodel.mapping.AttributeMetadata;
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
import org.hibernate.metamodel.mapping.EmbeddableValuedModelPart;
Expand Down Expand Up @@ -97,4 +98,8 @@ public VirtualEmbeddedAttributeMapping(
);
}

@Override
public Generator getGenerator() {
return null;
}
}
Loading
Loading