Skip to content

Commit

Permalink
HSEARCH-5029 Simplify entity removal in ReusableOrmSetupHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta authored and yrodiere committed Dec 4, 2023
1 parent a3e744e commit d8eab28
Showing 1 changed file with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import jakarta.persistence.metamodel.EntityType;
import jakarta.persistence.metamodel.ManagedType;
import jakarta.persistence.metamodel.Metamodel;
import jakarta.persistence.metamodel.PluralAttribute;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand Down Expand Up @@ -179,12 +180,7 @@ private static void clearEntityInstances(SessionFactoryImplementor sessionFactor
// only instances of subclasses, and those are handled separately.
return;
}
if (
// Workaround until https://hibernate.atlassian.net/browse/HHH-5529 gets implemented
hasPotentiallyJoinTable( sessionFactory, entityType )
// Workaround until https://hibernate.atlassian.net/browse/HHH-14814 gets fixed
|| hasEntitySubclass( sessionFactory, entityType )
) {
if ( hasSelfAssociation( entityType.getJavaType(), sessionFactory, entityType ) ) {
if ( mapping != null ) {
mapping.listenerEnabled( false );
}
Expand Down Expand Up @@ -281,24 +277,26 @@ private static boolean hasEntitySubclass(SessionFactory sessionFactory, EntityTy
return false;
}

private static boolean hasPotentiallyJoinTable(SessionFactoryImplementor sessionFactory,
private static boolean hasSelfAssociation(Class<?> entityJavaType, SessionFactoryImplementor sessionFactory,
ManagedType<?> managedType) {
for ( Attribute<?, ?> attribute : managedType.getAttributes() ) {
switch ( attribute.getPersistentAttributeType() ) {
case MANY_TO_ONE:
case ONE_TO_ONE:
case BASIC:
break;
case MANY_TO_MANY:
case ONE_TO_MANY:
case ELEMENT_COLLECTION:
if ( attribute.isAssociation() ) {
Class<?> type;
if ( attribute.isCollection() ) {
type = ( (PluralAttribute<?, ?, ?>) attribute ).getElementType().getJavaType();
}
else {
type = attribute.getJavaType();
}
if ( entityJavaType.isAssignableFrom( type ) ) {
return true;
case EMBEDDED:
EmbeddableType<?> embeddable = sessionFactory.getJpaMetamodel().embeddable( attribute.getJavaType() );
if ( hasPotentiallyJoinTable( sessionFactory, embeddable ) ) {
return true;
}
break;
}
}
if ( Attribute.PersistentAttributeType.EMBEDDED.equals( attribute.getPersistentAttributeType() ) ) {
EmbeddableType<?> embeddable = sessionFactory.getJpaMetamodel().embeddable( attribute.getJavaType() );
if ( hasSelfAssociation( entityJavaType, sessionFactory, embeddable ) ) {
return true;
}
}
}
return false;
Expand Down

0 comments on commit d8eab28

Please sign in to comment.