Skip to content

Commit

Permalink
HSEARCH-3925 Move a few metamodel-related methods out of HibernateOrm…
Browse files Browse the repository at this point in the history
…EntityIdEntityLoader

Just to make the class simpler, as we're going to change it.
  • Loading branch information
yrodiere committed May 25, 2020
1 parent 9bc1fea commit f4fb0c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
Expand Up @@ -10,8 +10,10 @@
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.hibernate.AssertionFailure;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.metamodel.spi.MetamodelImplementor;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.search.mapper.orm.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
Expand Down Expand Up @@ -44,4 +46,42 @@ public static SessionImplementor toSessionImplementor(EntityManager entityManage
public static boolean isSuperTypeOf(EntityPersister type1, EntityPersister type2) {
return type1.isSubclassEntityName( type2.getEntityName() );
}

public static EntityPersister toRootEntityType(SessionFactoryImplementor sessionFactory,
EntityPersister entityType) {
/*
* We need to rely on Hibernate ORM's SPIs: this is complex stuff.
* For example there may be class hierarchies such as A > B > C
* where A and C are entity types and B is a mapped superclass.
* So we need to exclude non-entity types, and for that we need the Hibernate ORM metamodel.
*/
MetamodelImplementor metamodel = sessionFactory.getMetamodel();
String rootEntityName = metamodel.entityPersister( entityType.getEntityName() ).getRootEntityName();
return metamodel.entityPersister( rootEntityName ).getEntityPersister();
}

public static EntityPersister toMostSpecificCommonEntitySuperType(MetamodelImplementor metamodel,
EntityPersister type1, EntityPersister type2) {
/*
* We need to rely on Hibernate ORM's SPIs: this is complex stuff.
* For example there may be class hierarchies such as A > B > C
* where A and C are entity types and B is a mapped superclass.
* So even if we know the two types have a common superclass,
* we need to skip non-entity superclasses, and for that we need the Hibernate ORM metamodel.
*/
EntityPersister superTypeCandidate = type1;
while ( superTypeCandidate != null && !isSuperTypeOf( superTypeCandidate, type2 ) ) {
String superSuperTypeEntityName = superTypeCandidate.getEntityMetamodel().getSuperclass();
superTypeCandidate = superSuperTypeEntityName == null ? null
: metamodel.entityPersister( superSuperTypeEntityName ).getEntityPersister();
}
if ( superTypeCandidate == null ) {
throw new AssertionFailure(
"Cannot find a common entity supertype for " + type1.getEntityName()
+ " and " + type2.getEntityName() + "."
+ " There is a bug in Hibernate Search, please report it."
);
}
return superTypeCandidate;
}
}
Expand Up @@ -32,7 +32,7 @@ public class HibernateOrmEntityIdEntityLoader<E> implements HibernateOrmComposab

public static EntityLoaderFactory factory(SessionFactoryImplementor sessionFactory,
EntityPersister entityType) {
return new Factory( toRootEntityType( sessionFactory, entityType ) );
return new Factory( HibernateOrmUtils.toRootEntityType( sessionFactory, entityType ) );
}

private final Session session;
Expand Down Expand Up @@ -174,19 +174,6 @@ private static boolean hasExpectedType(EntityReference reference, Object loadedE
return reference.type().isInstance( loadedEntity );
}

private static EntityPersister toRootEntityType(
SessionFactoryImplementor sessionFactory, EntityPersister entityType) {
/*
* We need to rely on Hibernate ORM's SPIs: this is complex stuff.
* For example there may be class hierarchies such as A > B > C
* where A and C are entity types and B is a mapped superclass.
* So we need to exclude non-entity types, and for that we need the Hibernate ORM metamodel.
*/
MetamodelImplementor metamodel = sessionFactory.getMetamodel();
String rootEntityName = metamodel.entityPersister( entityType.getEntityName() ).getRootEntityName();
return metamodel.entityPersister( rootEntityName ).getEntityPersister();
}

private static class Factory implements EntityLoaderFactory {

private final EntityPersister rootEntityType;
Expand Down Expand Up @@ -310,35 +297,11 @@ private static EntityPersister toMostSpecificCommonEntitySuperType(SessionImplem
result = type;
}
else {
result = toMostSpecificCommonEntitySuperType( metamodel, result, type );
result = HibernateOrmUtils.toMostSpecificCommonEntitySuperType( metamodel, result, type );
}
}
return result;
}

private static EntityPersister toMostSpecificCommonEntitySuperType(MetamodelImplementor metamodel,
EntityPersister type1, EntityPersister type2) {
/*
* We need to rely on Hibernate ORM's SPIs: this is complex stuff.
* For example there may be class hierarchies such as A > B > C
* where A and C are entity types and B is a mapped superclass.
* So even if we know the two types have a common superclass,
* we need to skip non-entity superclasses, and for that we need the Hibernate ORM metamodel.
*/
EntityPersister superTypeCandidate = type1;
while ( superTypeCandidate != null && !HibernateOrmUtils.isSuperTypeOf( superTypeCandidate, type2 ) ) {
String superSuperTypeEntityName = superTypeCandidate.getEntityMetamodel().getSuperclass();
superTypeCandidate = superSuperTypeEntityName == null ? null
: metamodel.entityPersister( superSuperTypeEntityName ).getEntityPersister();
}
if ( superTypeCandidate == null ) {
throw new AssertionFailure(
"Cannot find a common entity supertype for " + type1.getEntityName()
+ " and " + type2.getEntityName() + "."
+ " There is a bug in Hibernate Search, please report it."
);
}
return superTypeCandidate;
}
}
}

0 comments on commit f4fb0c4

Please sign in to comment.