minor cleanups following from HR review

This commit is contained in:
Gavin 2023-04-13 18:38:35 +02:00 committed by Gavin King
parent a4d7b0df67
commit 2fd6f548e4
5 changed files with 57 additions and 50 deletions

View File

@ -16,9 +16,10 @@ import org.hibernate.event.spi.PostCollectionUpdateEventListener;
import org.hibernate.event.spi.PreCollectionUpdateEvent;
import org.hibernate.event.spi.PreCollectionUpdateEventListener;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.stat.spi.StatisticsImplementor;
import static org.hibernate.pretty.MessageHelper.collectionInfoString;
/**
* The action for updating a collection
*/
@ -71,7 +72,7 @@ public final class CollectionUpdateAction extends CollectionAction {
else if ( collection.needsRecreate( persister ) ) {
if ( affectedByFilters ) {
throw new HibernateException( "cannot recreate collection while filter is enabled: "
+ MessageHelper.collectionInfoString( persister, collection, id, session )
+ collectionInfoString( persister, collection, id, session )
);
}
if ( !emptySnapshot ) {

View File

@ -26,17 +26,16 @@ import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.type.AssociationType;
import org.hibernate.type.CollectionType;
import org.hibernate.type.ComponentType;
import org.hibernate.type.CompositeType;
import org.hibernate.type.EntityType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy;
import static org.hibernate.pretty.MessageHelper.infoString;
import static org.hibernate.type.ForeignKeyDirection.TO_PARENT;
/**
* Delegate responsible for, in conjunction with the various
@ -319,7 +318,7 @@ public final class Cascade {
// orphaned if the association was nulled (child == null) or receives a new value while the
// entity is managed (without first nulling and manually flushing).
if ( child == null || ( loadedValue != null && child != loadedValue ) ) {
if ( child == null || loadedValue != null && child != loadedValue ) {
EntityEntry valueEntry = persistenceContext.getEntry( loadedValue );
if ( valueEntry == null && isHibernateProxy( loadedValue ) ) {
@ -339,16 +338,17 @@ public final class Cascade {
}
if ( valueEntry != null ) {
final String entityName = valueEntry.getPersister().getEntityName();
final EntityPersister persister = valueEntry.getPersister();
final String entityName = persister.getEntityName();
if ( LOG.isTraceEnabled() ) {
final Object id = valueEntry.getPersister().getIdentifier( loadedValue, eventSource );
final String description = MessageHelper.infoString( entityName, id );
LOG.tracev( "Deleting orphaned entity instance: {0}", description );
LOG.tracev(
"Deleting orphaned entity instance: {0}",
infoString( entityName, persister.getIdentifier( loadedValue, eventSource ) )
);
}
if ( type.isAssociationType() && ( (AssociationType) type ).getForeignKeyDirection().equals(
ForeignKeyDirection.TO_PARENT
) ) {
if ( type.isAssociationType()
&& ( (AssociationType) type ).getForeignKeyDirection().equals(TO_PARENT) ) {
// If FK direction is to-parent, we must remove the orphan *before* the queued update(s)
// occur. Otherwise, replacing the association on a managed entity, without manually
// nulling and flushing, causes FK constraint violations.
@ -397,8 +397,9 @@ public final class Cascade {
for ( int i = 0; i < types.length; i++ ) {
final CascadeStyle componentPropertyStyle = componentType.getCascadeStyle( i );
final String subPropertyName = propertyNames[i];
if ( componentPropertyStyle.doCascade( action ) || componentPropertyStyle.hasOrphanDelete() && action.deleteOrphans() ) {
if (children == null) {
if ( componentPropertyStyle.doCascade( action )
|| componentPropertyStyle.hasOrphanDelete() && action.deleteOrphans() ) {
if ( children == null ) {
// Get children on demand.
children = componentType.getPropertyValues( child, eventSource );
}
@ -461,10 +462,9 @@ public final class Cascade {
final CascadeStyle style,
final T anything,
final CollectionType type) {
final CollectionPersister persister = eventSource.getFactory()
.getRuntimeMetamodels()
.getMappingMetamodel()
.getCollectionDescriptor( type.getRole());
final CollectionPersister persister =
eventSource.getFactory().getMappingMetamodel()
.getCollectionDescriptor( type.getRole() );
final Type elemType = persister.getElementType();
CascadePoint elementsCascadePoint = cascadePoint;

View File

@ -22,6 +22,7 @@ import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy;
import static org.hibernate.engine.internal.ManagedTypeHelper.processIfSelfDirtinessTracker;
/**
* Algorithms related to foreign key constraint transparency
@ -117,10 +118,10 @@ public final class ForeignKeys {
returnedValue = isNullifiable( null, value ) ? null : value;
}
else if ( type.isComponentType() ) {
final CompositeType actype = (CompositeType) type;
final Object[] subvalues = actype.getPropertyValues( value, session );
final Type[] subtypes = actype.getSubtypes();
final String[] subPropertyNames = actype.getPropertyNames();
final CompositeType compositeType = (CompositeType) type;
final Object[] subvalues = compositeType.getPropertyValues( value, session );
final Type[] subtypes = compositeType.getSubtypes();
final String[] subPropertyNames = compositeType.getPropertyNames();
boolean substitute = false;
for ( int i = 0; i < subvalues.length; i++ ) {
final Object replacement = nullifyTransientReferences(
@ -135,7 +136,7 @@ public final class ForeignKeys {
}
if ( substitute ) {
// todo : need to account for entity mode on the CompositeType interface :(
actype.setPropertyValues( value, subvalues );
compositeType.setPropertyValues( value, subvalues );
}
returnedValue = value;
}
@ -148,19 +149,16 @@ public final class ForeignKeys {
// When bytecode-enhancement is used for dirty-checking, the change should
// only be tracked when returnedValue was nullified (1)).
if ( value != returnedValue && returnedValue == null ) {
ManagedTypeHelper.processIfSelfDirtinessTracker( self, SelfDirtinessTracker::$$_hibernate_trackChange, propertyName );
processIfSelfDirtinessTracker( self, SelfDirtinessTracker::$$_hibernate_trackChange, propertyName );
}
return returnedValue;
}
private Object initializeIfNecessary(
final Object value,
final String propertyName,
final Type type) {
if ( isDelete &&
value == LazyPropertyInitializer.UNFETCHED_PROPERTY &&
type.isEntityType() &&
!session.getPersistenceContextInternal().isNullifiableEntityKeysEmpty() ) {
private Object initializeIfNecessary(final Object value, final String propertyName, final Type type) {
if ( isDelete
&& value == LazyPropertyInitializer.UNFETCHED_PROPERTY
&& type.isEntityType()
&& !session.getPersistenceContextInternal().isNullifiableEntityKeysEmpty() ) {
// IMPLEMENTATION NOTE: If cascade-remove was mapped for the attribute,
// then value should have been initialized previously, when the remove operation was
// cascaded to the property (because CascadingAction.DELETE.performOnLazyProperty()
@ -209,10 +207,11 @@ public final class ForeignKeys {
// as nullifiable
Object entity = lazyInitializer.getImplementation( session );
if ( entity == null ) {
// an unloaded proxy might be scheduled for deletion
return persistenceContext.containsDeletedUnloadedEntityKey(
session.generateEntityKey(
lazyInitializer.getIdentifier(),
session.getFactory().getRuntimeMetamodels().getMappingMetamodel()
session.getFactory().getMappingMetamodel()
.getEntityDescriptor( lazyInitializer.getEntityName() )
)
);
@ -420,12 +419,12 @@ public final class ForeignKeys {
}
}
else if ( type.isComponentType() ) {
final CompositeType actype = (CompositeType) type;
final boolean[] subValueNullability = actype.getPropertyNullability();
final CompositeType compositeType = (CompositeType) type;
final boolean[] subValueNullability = compositeType.getPropertyNullability();
if ( subValueNullability != null ) {
final String[] subPropertyNames = actype.getPropertyNames();
final Object[] subvalues = actype.getPropertyValues( value, session );
final Type[] subtypes = actype.getSubtypes();
final String[] subPropertyNames = compositeType.getPropertyNames();
final Object[] subvalues = compositeType.getPropertyValues( value, session );
final Type[] subtypes = compositeType.getSubtypes();
for ( int j = 0; j < subvalues.length; j++ ) {
collectNonNullableTransientEntities(
nullifier,

View File

@ -360,13 +360,13 @@ public class CascadingActions {
Type propertyType,
int propertyIndex) {
if ( propertyType.isEntityType() ) {
Object child = persister.getValue( parent, propertyIndex );
final Object child = persister.getValue( parent, propertyIndex );
if ( child != null
&& !isInManagedState( child, session )
&& !( isHibernateProxy( child ) ) ) { //a proxy cannot be transient and it breaks ForeignKeys.isTransient
&& !isHibernateProxy( child ) ) { //a proxy cannot be transient and it breaks ForeignKeys.isTransient
final String childEntityName =
((EntityType) propertyType).getAssociatedEntityName( session.getFactory() );
if ( ForeignKeys.isTransient(childEntityName, child, null, session) ) {
if ( ForeignKeys.isTransient( childEntityName, child, null, session ) ) {
String parentEntityName = persister.getEntityName();
String propertyName = persister.getPropertyNames()[propertyIndex];
throw new TransientPropertyValueException(
@ -387,12 +387,19 @@ public class CascadingActions {
private boolean isInManagedState(Object child, EventSource session) {
EntityEntry entry = session.getPersistenceContextInternal().getEntry( child );
return entry != null &&
(
entry.getStatus() == Status.MANAGED ||
entry.getStatus() == Status.READ_ONLY ||
entry.getStatus() == Status.SAVING
);
if ( entry == null ) {
return false;
}
else {
switch ( entry.getStatus() ) {
case MANAGED:
case READ_ONLY:
case SAVING:
return true;
default:
return false;
}
}
}
@Override

View File

@ -255,7 +255,7 @@ public class StandardIdentifierGeneratorFactory
}
}
private Class<? extends Generator> generatorClassForName(String strategy) {
protected Class<? extends Generator> generatorClassForName(String strategy) {
try {
Class<? extends Generator> clazz =
serviceRegistry.getService( ClassLoaderService.class )
@ -268,7 +268,7 @@ public class StandardIdentifierGeneratorFactory
return clazz;
}
catch ( ClassLoadingException e ) {
throw new MappingException( String.format( "Could not interpret id generator strategy [%s]", strategy) );
throw new MappingException( String.format( "Could not interpret id generator strategy [%s]", strategy ) );
}
}
}