diff --git a/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/common/impl/HibernateOrmUtils.java b/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/common/impl/HibernateOrmUtils.java index a2e3346c6b3..52c8aae551b 100644 --- a/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/common/impl/HibernateOrmUtils.java +++ b/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/common/impl/HibernateOrmUtils.java @@ -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; @@ -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; + } } diff --git a/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/search/loading/impl/HibernateOrmEntityIdEntityLoader.java b/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/search/loading/impl/HibernateOrmEntityIdEntityLoader.java index ba8a72aac3e..c1beffbcf7e 100644 --- a/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/search/loading/impl/HibernateOrmEntityIdEntityLoader.java +++ b/mapper/orm/src/main/java/org/hibernate/search/mapper/orm/search/loading/impl/HibernateOrmEntityIdEntityLoader.java @@ -32,7 +32,7 @@ public class HibernateOrmEntityIdEntityLoader 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; @@ -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; @@ -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; - } } }