Skip to content

Commit

Permalink
HHH-16117 Querying entity with collection in Embeddable causes 'A col…
Browse files Browse the repository at this point in the history
…lection with cascade=all-delete-orphan was no longer referenced by the owning entity instance'
  • Loading branch information
dreab8 authored and beikov committed Feb 6, 2023
1 parent 674866f commit 0dfba1e
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package org.hibernate.action.internal;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.ForeignKeys;
Expand All @@ -19,8 +21,10 @@
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.metamodel.mapping.AttributeMapping;
import org.hibernate.metamodel.mapping.NaturalIdMapping;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.persister.entity.EntityPersister;
Expand Down Expand Up @@ -146,23 +150,72 @@ public final void makeEntityManaged() {

protected void addCollectionsByKeyToPersistenceContext(PersistenceContext persistenceContext, Object[] objects) {
for ( int i = 0; i < objects.length; i++ ) {
if ( objects[i] instanceof PersistentCollection<?> ) {
final PersistentCollection<?> persistentCollection = (PersistentCollection<?>) objects[i];
final CollectionPersister collectionPersister = ( (PluralAttributeMapping) getPersister().getAttributeMapping( i ) ).getCollectionDescriptor();
final CollectionKey collectionKey = new CollectionKey(
collectionPersister,
( (AbstractEntityPersister) getPersister() ).getCollectionKey(
collectionPersister,
getInstance(),
persistenceContext.getEntry( getInstance() ),
getSession()
)
final AttributeMapping attributeMapping = getPersister().getAttributeMapping( i );
if ( attributeMapping.isEmbeddedAttributeMapping() ) {
visitEmbeddedAttributeMapping(
attributeMapping.asEmbeddedAttributeMapping(),
objects[i],
persistenceContext
);
}
else if ( attributeMapping.isPluralAttributeMapping() ) {
addCollectionKey(
attributeMapping.asPluralAttributeMapping(),
objects[i],
persistenceContext
);
persistenceContext.addCollectionByKey( collectionKey, persistentCollection );
}
}
}

private void visitEmbeddedAttributeMapping(
EmbeddedAttributeMapping attributeMapping,
Object object,
PersistenceContext persistenceContext) {
if ( object != null ) {
final List<AttributeMapping> attributeMappings = attributeMapping.getEmbeddableTypeDescriptor().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attribute = attributeMappings.get( i );
if ( attribute.isPluralAttributeMapping() ) {
addCollectionKey(
attribute.asPluralAttributeMapping(),
attribute.getPropertyAccess().getGetter().get( object ),
persistenceContext
);
}
else if ( attribute.isEmbeddedAttributeMapping() ) {
visitEmbeddedAttributeMapping(
attribute.asEmbeddedAttributeMapping(),
attribute.getPropertyAccess().getGetter().get( object ),
persistenceContext
);
}
}
}
}

private void addCollectionKey(
PluralAttributeMapping pluralAttributeMapping,
Object o,
PersistenceContext persistenceContext) {
if ( o instanceof PersistentCollection ) {
final CollectionPersister collectionPersister = pluralAttributeMapping.getCollectionDescriptor();
final CollectionKey collectionKey = new CollectionKey(
collectionPersister,
( (AbstractEntityPersister) getPersister() ).getCollectionKey(
collectionPersister,
getInstance(),
persistenceContext.getEntry( getInstance() ),
getSession()
)
);
persistenceContext.addCollectionByKey(
collectionKey,
(PersistentCollection<?>) o
);
}
}

/**
* Indicate that the action has executed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void execute() throws HibernateException {
if ( !veto ) {

persister.insert( id, getState(), instance, session );
PersistenceContext persistenceContext = session.getPersistenceContextInternal();
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
final EntityEntry entry = persistenceContext.getEntry( instance );
if ( entry == null ) {
throw new AssertionFailure( "possible non-threadsafe access to session" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.metamodel.mapping;

import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.sql.results.graph.DatabaseSnapshotContributor;
import org.hibernate.sql.results.graph.Fetchable;
Expand Down Expand Up @@ -86,4 +87,30 @@ default AttributeMapping asAttributeMapping() {
return this;
}

/**
* A utility method to avoid casting explicitly to PluralAttributeMapping
*
* @return PluralAttributeMapping if this is an instance of PluralAttributeMapping otherwise {@code null}
*/
default PluralAttributeMapping asPluralAttributeMapping() {
return null;
}

default boolean isPluralAttributeMapping() {
return false;
}

/**
* A utility method to avoid casting explicitly to EmbeddedAttributeMapping
*
* @return EmbeddedAttributeMapping if this is an instance of EmbeddedAttributeMapping otherwise {@code null}
*/
default EmbeddedAttributeMapping asEmbeddedAttributeMapping(){
return null;
}

default boolean isEmbeddedAttributeMapping(){
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,14 @@ default void applyWhereRestrictions(
SqlAstCreationState creationState) {
getCollectionDescriptor().applyWhereRestrictions( predicateConsumer, tableGroup, useQualifier, creationState );
}

@Override
default PluralAttributeMapping asPluralAttributeMapping() {
return this;
}

@Override
default boolean isPluralAttributeMapping() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,14 @@ private static PropertyAccess getPropertyAccess(
}
return parentInjectionAttributePropertyAccess;
}

@Override
public EmbeddedAttributeMapping asEmbeddedAttributeMapping() {
return this;
}

@Override
public boolean isEmbeddedAttributeMapping() {
return true;
}
}

0 comments on commit 0dfba1e

Please sign in to comment.