Skip to content

Commit

Permalink
HSEARCH-4465 Avoid deprecated ORM API/SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and marko-bekhta committed Sep 25, 2023
1 parent 7dc9bbc commit cb7c170
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public List<?> setupParams() {

@Test
public void metamodel_onlyUserEntities() {
assertThat( setupHolder.sessionFactory().getMetamodel().getEntities() )
assertThat( setupHolder.sessionFactory().getJpaMetamodel().getEntities() )
.extracting( EntityType::getName )
.containsOnly( IndexedEntity.NAME );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void resetFilter() {

@Test
public void metamodel_userEntitiesAndOutboxEventAndAgent() {
assertThat( setupHolder.sessionFactory().getMetamodel().getEntities() )
assertThat( setupHolder.sessionFactory().getJpaMetamodel().getEntities() )
.<Class<?>>extracting( Type::getJavaType )
.containsExactlyInAnyOrder( IndexedEntity.class, OutboxEvent.class, Agent.class );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void test() {

// Hibernate Search started successfully.
// Check that there actually is a backref:
MappingMetamodel metamodel = sessionFactory.unwrap( SessionFactoryImplementor.class ).getMetamodel();
MappingMetamodel metamodel = sessionFactory.unwrap( SessionFactoryImplementor.class ).getMappingMetamodel();
assertThat( metamodel.getEntityDescriptor( IndexedEntity.class ).getPropertyNames() )
.contains( "_containing_fk_containingidBackref" )
.contains( "_containingIndexBackref" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void test() {

// Hibernate Search started successfully.
// Check that there actually is a synthetic property:
MappingMetamodel metamodel = sessionFactory.unwrap( SessionFactoryImplementor.class ).getMetamodel();
MappingMetamodel metamodel = sessionFactory.unwrap( SessionFactoryImplementor.class ).getMappingMetamodel();
assertThat( metamodel.getEntityDescriptor( ContainedEntity.class ).getPropertyNames() )
.contains( "_" + IndexedEntity.class.getName().replace( '.', '_' ) + "_contained" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static IndexScope getIndexScope(String hql) {
public static List<EntityTypeDescriptor> createDescriptors(EntityManagerFactory entityManagerFactory, Set<Class<?>> types) {
SessionFactoryImplementor sessionFactory = entityManagerFactory.unwrap( SessionFactoryImplementor.class );
List<EntityTypeDescriptor> result = new ArrayList<>( types.size() );
MappingMetamodel metamodel = sessionFactory.getMetamodel();
MappingMetamodel metamodel = sessionFactory.getMappingMetamodel();
for ( Class<?> type : types ) {
result.add( createDescriptor( metamodel, type ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.MutationQuery;
import org.hibernate.query.Query;
import org.hibernate.search.mapper.orm.common.spi.TransactionHelper;
import org.hibernate.search.mapper.orm.coordination.common.spi.CoordinationStrategyStartContext;
Expand Down Expand Up @@ -78,7 +79,7 @@ public int reprocessAbortedEvents() {

try ( Session session = sessionFactory.openSession() ) {
return transactionHelper.inTransaction( (SharedSessionContractImplementor) session, () -> {
Query<?> query = session.createQuery( UPDATE_EVENTS_WITH_STATUS );
MutationQuery query = session.createMutationQuery( UPDATE_EVENTS_WITH_STATUS );
query.setParameter( "status", OutboxEvent.Status.ABORTED );
query.setParameter( "newStatus", OutboxEvent.Status.PENDING );
return query.executeUpdate();
Expand All @@ -92,7 +93,7 @@ public int reprocessAbortedEvents(String tenantId) {

try ( Session session = sessionFactory.withOptions().tenantIdentifier( tenantId ).openSession() ) {
return transactionHelper.inTransaction( (SharedSessionContractImplementor) session, () -> {
Query<?> query = session.createQuery( UPDATE_EVENTS_WITH_STATUS );
MutationQuery query = session.createMutationQuery( UPDATE_EVENTS_WITH_STATUS );
query.setParameter( "status", OutboxEvent.Status.ABORTED );
query.setParameter( "newStatus", OutboxEvent.Status.PENDING );
return query.executeUpdate();
Expand All @@ -106,7 +107,7 @@ public int clearAllAbortedEvents() {

try ( Session session = sessionFactory.openSession() ) {
return transactionHelper.inTransaction( (SharedSessionContractImplementor) session, () -> {
Query<?> query = session.createQuery( DELETE_EVENTS_WITH_STATUS );
MutationQuery query = session.createMutationQuery( DELETE_EVENTS_WITH_STATUS );
query.setParameter( "status", OutboxEvent.Status.ABORTED );
return query.executeUpdate();
} );
Expand All @@ -119,7 +120,7 @@ public int clearAllAbortedEvents(String tenantId) {

try ( Session session = sessionFactory.withOptions().tenantIdentifier( tenantId ).openSession() ) {
return transactionHelper.inTransaction( (SharedSessionContractImplementor) session, () -> {
Query<?> query = session.createQuery( DELETE_EVENTS_WITH_STATUS );
MutationQuery query = session.createMutationQuery( DELETE_EVENTS_WITH_STATUS );
query.setParameter( "status", OutboxEvent.Status.ABORTED );
return query.executeUpdate();
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static EntityPersister toRootEntityType(SessionFactoryImplementor session
* 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.
*/
MappingMetamodel metamodel = sessionFactory.getMetamodel();
MappingMetamodel metamodel = sessionFactory.getMappingMetamodel();
String rootEntityName = entityType.getRootEntityName();
return metamodel.getEntityDescriptor( rootEntityName );
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public static boolean targetsAllConcreteSubTypes(SessionFactoryImplementor sessi
return true;
}

MappingMetamodel metamodel = sessionFactory.getMetamodel();
MappingMetamodel metamodel = sessionFactory.getMappingMetamodel();
int concreteSubTypesCount = 0;
for ( String subClassEntityName : subClassEntityNames ) {
if ( !metamodel.getEntityDescriptor( subClassEntityName ).getEntityMetamodel().isAbstract() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private PojoSelectionEntityLoader<?> doCreate(EntityPersister entityPersister,

private static EntityPersister toMostSpecificCommonEntitySuperType(SessionImplementor session,
Iterable<? extends LoadingTypeContext<?>> targetEntityTypeContexts) {
MappingMetamodel metamodel = session.getSessionFactory().getMetamodel();
MappingMetamodel metamodel = session.getSessionFactory().getMappingMetamodel();
EntityPersister result = null;
for ( LoadingTypeContext<?> targetTypeContext : targetEntityTypeContexts ) {
EntityPersister type = targetTypeContext.entityPersister();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface TypeQueryFactory<E, I> {

static TypeQueryFactory<?, ?> create(SessionFactoryImplementor sessionFactory, EntityPersister entityPersister,
String uniquePropertyName) {
JpaMetamodel metamodel = sessionFactory.getMetamodel();
JpaMetamodel metamodel = sessionFactory.getJpaMetamodel();
EntityDomainType<?> typeOrNull = metamodel.entity( entityPersister.getEntityName() );
if ( typeOrNull != null && !( entityPersister.getMappedClass().equals( Map.class ) ) ) {
return CriteriaTypeQueryFactory.create( typeOrNull, uniquePropertyName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class AbstractHibernateOrmTypeContext<E>
AbstractHibernateOrmTypeContext(AbstractBuilder<E> builder, SessionFactoryImplementor sessionFactory) {
this.typeIdentifier = builder.typeIdentifier;
this.jpaEntityName = builder.jpaEntityName;
MappingMetamodel metamodel = sessionFactory.getMetamodel();
MappingMetamodel metamodel = sessionFactory.getMappingMetamodel();
this.entityPersister = metamodel.getEntityDescriptor( builder.hibernateOrmEntityName );
this.ascendingSuperTypes = builder.ascendingSuperTypes;
if ( builder.documentIdSourcePropertyName != null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ public void configure(MappingBuildContext buildContext, PojoMappingConfiguration
}

// Sort the properties before processing for deterministic iteration
@SuppressWarnings("unchecked") // Hibernate ORM gives us raw types, we must make do.
List<Property> properties =
HibernateOrmUtils.sortedNonSyntheticProperties( persistentClass.getPropertyIterator() );
HibernateOrmUtils.sortedNonSyntheticProperties( persistentClass.getProperties().iterator() );

Property identifierProperty = persistentClass.getIdentifierProperty();
Optional<Property> identifierPropertyOptional = Optional.ofNullable( identifierProperty );
Expand Down Expand Up @@ -125,9 +124,8 @@ private void contributeEmbeddableTypeMetadata(
*/
if ( processedEmbeddableTypes.add( componentTypeModel ) ) {
// Sort the properties before processing for deterministic iteration
@SuppressWarnings("unchecked") // Hibernate ORM gives us raw types, we must make do.
List<Property> properties =
HibernateOrmUtils.sortedNonSyntheticProperties( componentValue.getPropertyIterator() );
HibernateOrmUtils.sortedNonSyntheticProperties( componentValue.getProperties().iterator() );
configurationCollector.collectContributor( componentTypeModel,
new ErrorCollectingPojoTypeMetadataContributor()
// Ensure Hibernate ORM metadata about properties is translated into Hibernate Search metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hibernate.mapping.OneToMany;
import org.hibernate.mapping.OneToOne;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.ToOne;
import org.hibernate.mapping.Value;
Expand Down Expand Up @@ -98,16 +97,12 @@ else if ( value instanceof SimpleValue ) {
}

private void collectScale(PojoAdditionalMetadataCollectorPropertyNode collector, Value value) {
Iterator<Selectable> columnIterator = value.getColumnIterator();
Iterator<Column> columnIterator = value.getColumns().iterator();
Dialect dialect = basicTypeMetadataProvider.getDialect();
Metadata metadata = basicTypeMetadataProvider.getMetadata();

while ( columnIterator.hasNext() ) {
Selectable mappedColumn = columnIterator.next();
if ( !(mappedColumn instanceof Column) ) {
continue;
}
Column column = (Column) mappedColumn;
Column column = columnIterator.next();
Size size = column.getColumnSize( dialect, metadata );
Integer scale = size.getScale();
if ( scale == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void collectPersistentClass(Builder metadataProviderBuilder, Pers

collectClassType(
metadataProviderBuilder, javaClass,
persistentClass.getIdentifierProperty(), persistentClass.getPropertyIterator()
persistentClass.getIdentifierProperty(), persistentClass.getProperties().iterator()
);

metadataProviderBuilder.typeIdentifierResolverBuilder.addClassEntityType(
Expand All @@ -67,7 +67,7 @@ private static void collectPersistentClass(Builder metadataProviderBuilder, Pers
collectDynamicMapType(
metadataProviderBuilder, hibernateOrmEntityName,
persistentClass.getSuperclass(),
persistentClass.getIdentifierProperty(), persistentClass.getPropertyIterator()
persistentClass.getIdentifierProperty(), persistentClass.getProperties().iterator()
);

metadataProviderBuilder.typeIdentifierResolverBuilder.addDynamicMapEntityType(
Expand Down Expand Up @@ -204,7 +204,7 @@ private static HibernateOrmTypeModelFactory<?> collectEmbedded(Builder metadataP
collectDynamicMapType(
metadataProviderBuilder, name,
null, /* No supertype */
null /* No ID */, component.getPropertyIterator()
null /* No ID */, component.getProperties().iterator()
);
}
return HibernateOrmTypeModelFactory.dynamicMap( name );
Expand All @@ -215,7 +215,7 @@ private static HibernateOrmTypeModelFactory<?> collectEmbedded(Builder metadataP
if ( !metadataProviderBuilder.classTypeMetadata.containsKey( javaClass ) ) {
collectClassType(
metadataProviderBuilder, javaClass,
null /* No ID */, component.getPropertyIterator()
null /* No ID */, component.getProperties().iterator()
);
}
return HibernateOrmTypeModelFactory.rawType( javaClass );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.hibernate.search.mapper.orm.model.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.mapping.PersistentClass;
Expand Down Expand Up @@ -38,8 +37,7 @@ public HibernateOrmPathDefinitionProvider(PojoRawTypeModel<?> typeModel, Persist
this.typeModel = typeModel;
this.persistentClass = persistentClass;
this.propertyStringRepresentationByOrdinal = new ArrayList<>();
for ( Iterator<Property> iterator = persistentClass.getPropertyClosureIterator(); iterator.hasNext(); ) {
Property property = iterator.next();
for ( Property property : persistentClass.getPropertyClosure() ) {
propertyStringRepresentationByOrdinal.add( property.getName() );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ private void clearDatabase(SessionFactoryImplementor sessionFactory, HibernateOr
for ( Class<?> entityClass : config.entityClearOrder ) {
EntityType<?> entityType;
try {
entityType = sessionFactory.getMetamodel().entity( entityClass );
entityType = sessionFactory.getJpaMetamodel().entity( entityClass );
}
catch (IllegalArgumentException e) {
// When using annotatedTypes to infer the clear order,
Expand All @@ -475,7 +475,7 @@ private void clearDatabase(SessionFactoryImplementor sessionFactory, HibernateOr
// we try to delete all remaining entity types.
// Note we're stabilizing the order, because ORM uses a HashSet internally
// and the order may change from one execution to the next.
List<EntityType<?>> sortedEntityTypes = sessionFactory.getMetamodel().getEntities().stream()
List<EntityType<?>> sortedEntityTypes = sessionFactory.getJpaMetamodel().getEntities().stream()
.sorted( Comparator.comparing( EntityType::getName ) )
.collect( Collectors.toList() );
for ( EntityType<?> entityType : sortedEntityTypes ) {
Expand Down Expand Up @@ -564,6 +564,7 @@ private static Query<?> createSelectOrDeleteAllOfSpecificTypeQuery(EntityType<?>
builder.append( " where type( e ) in (:type)" );
typeArg = entityType.getJavaType();
}
@SuppressWarnings("deprecation")
Query<?> query = QueryType.SELECT.equals( queryType )
? session.createQuery( builder.toString(), entityType.getJavaType() )
: session.createQuery( builder.toString() );
Expand All @@ -574,7 +575,7 @@ private static Query<?> createSelectOrDeleteAllOfSpecificTypeQuery(EntityType<?>
}

private static boolean hasEntitySubclass(SessionFactory sessionFactory, EntityType<?> parentEntity) {
Metamodel metamodel = sessionFactory.unwrap( SessionFactoryImplementor.class ).getMetamodel();
Metamodel metamodel = sessionFactory.unwrap( SessionFactoryImplementor.class ).getJpaMetamodel();
for ( EntityType<?> entity : metamodel.getEntities() ) {
if ( parentEntity.equals( entity.getSupertype() ) ) {
return true;
Expand All @@ -596,7 +597,7 @@ private static boolean hasPotentiallyJoinTable(SessionFactoryImplementor session
case ELEMENT_COLLECTION:
return true;
case EMBEDDED:
EmbeddableType<?> embeddable = sessionFactory.getMetamodel().embeddable( attribute.getJavaType() );
EmbeddableType<?> embeddable = sessionFactory.getJpaMetamodel().embeddable( attribute.getJavaType() );
if ( hasPotentiallyJoinTable( sessionFactory, embeddable ) ) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public Class<SchemaManagementTool> getServiceInitiated() {
}

@Override
@SuppressWarnings("rawtypes") // Can't do better: Map is raw in the superclass
public SchemaManagementTool initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
public SchemaManagementTool initiateService(Map<String,Object> configurationValues, ServiceRegistryImplementor registry) {
return new MultitenancyTestHelperSchemaManagementTool( tenantIds );
}
}
Expand Down Expand Up @@ -89,8 +88,7 @@ private GenerationTargetToDatabase[] createSchemaTargets(ServiceRegistryImplemen
}

@Override
@SuppressWarnings("rawtypes") // Can't do better: Map is raw in the superclass
public SchemaCreator getSchemaCreator(Map options) {
public SchemaCreator getSchemaCreator(Map<String,Object> options) {
return new SchemaCreator() {
final SchemaCreatorImpl delegate = (SchemaCreatorImpl) toolDelegate.getSchemaCreator( options );

Expand All @@ -104,8 +102,7 @@ public void doCreation(Metadata metadata, ExecutionOptions executionOptions,
}

@Override
@SuppressWarnings("rawtypes") // Can't do better: Map is raw in the superclass
public SchemaDropper getSchemaDropper(Map options) {
public SchemaDropper getSchemaDropper(Map<String,Object> options) {
return new SchemaDropper() {
final SchemaDropperImpl delegate = (SchemaDropperImpl) toolDelegate.getSchemaDropper( options );

Expand All @@ -130,14 +127,12 @@ public void perform(ServiceRegistry serviceRegistry) {
}

@Override
@SuppressWarnings("rawtypes") // Can't do better: Map is raw in the superclass
public SchemaMigrator getSchemaMigrator(Map options) {
public SchemaMigrator getSchemaMigrator(Map<String,Object> options) {
throw notSupported();
}

@Override
@SuppressWarnings("rawtypes") // Can't do better: Map is raw in the superclass
public SchemaValidator getSchemaValidator(Map options) {
public SchemaValidator getSchemaValidator(Map<String,Object> options) {
throw notSupported();
}

Expand Down

0 comments on commit cb7c170

Please sign in to comment.