Skip to content

Commit

Permalink
HSEARCH-4843 Remove the need for mappers to implement EntityReference…
Browse files Browse the repository at this point in the history
…Factory
  • Loading branch information
yrodiere committed May 1, 2023
1 parent b597081 commit 896e56f
Show file tree
Hide file tree
Showing 28 changed files with 160 additions and 177 deletions.
7 changes: 6 additions & 1 deletion documentation/src/main/asciidoc/migration/index.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,12 @@ Use `PojoAdditionalMetadataCollectorTypeNode#markAsEntity(String, org.hibernate.
It was introduced by mistake and does not have any use.
* Mappers are no longer expected to provide a custom class to represent entity references,
e.g. in search projections or in indexing failure reports.
They should use `org.hibernate.search.engine.common.EntityReference` instead.
They should use `org.hibernate.search.engine.common.EntityReference` instead,
which is the type that will be instantiated by default.
Mappers that for some reason still need to rely on custom entity references classes
will need to have their custom entity references class implement `org.hibernate.search.engine.common.EntityReference`,
and will need to call `org.hibernate.search.mapper.pojo.mapping.spi.AbstractPojoMappingImplementor.AbstractPojoMappingImplementor(org.hibernate.search.mapper.pojo.mapping.spi.PojoMappingDelegate, org.hibernate.search.mapper.pojo.mapping.spi.PojoEntityReferenceFactory)`
when their mapping gets instantiated.
* Many `execute*(...)`/`send*(...)` methods related to indexing plans now take an `OperationSubmitter` as an argument
(see the javadoc of `OperationSubmitter`)
and no longer take an `EntityReferenceFactory` as an argument
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.hibernate.search.mapper.orm.loading.impl.HibernateOrmNonEntityIdPropertyEntityLoadingStrategy;
import org.hibernate.search.mapper.orm.loading.impl.LoadingTypeContext;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSessionTypeContext;
import org.hibernate.search.mapper.pojo.identity.spi.IdentifierMapping;
import org.hibernate.search.mapper.pojo.mapping.building.spi.PojoTypeExtendedMappingCollector;
import org.hibernate.search.mapper.pojo.model.path.spi.PojoPathFilter;
import org.hibernate.search.mapper.pojo.model.spi.PojoPropertyModel;
Expand All @@ -35,7 +34,6 @@ abstract class AbstractHibernateOrmTypeContext<E>
private final EntityPersister entityPersister;
private final boolean documentIdIsEntityId;
private final HibernateOrmEntityLoadingStrategy<? super E, ?> loadingStrategy;
private final IdentifierMapping identifierMapping;
private final PojoPathFilter dirtyFilter;
private final PojoPathFilter dirtyContainingAssociationFilter;
private final List<PojoRawTypeIdentifier<? super E>> ascendingSuperTypes;
Expand All @@ -47,7 +45,6 @@ abstract class AbstractHibernateOrmTypeContext<E>
this.jpaEntityName = builder.jpaEntityName;
MetamodelImplementor metamodel = sessionFactory.getMetamodel();
this.entityPersister = metamodel.entityPersister( builder.hibernateOrmEntityName );
this.identifierMapping = builder.identifierMapping;
this.ascendingSuperTypes = builder.ascendingSuperTypes;
if ( builder.documentIdSourcePropertyName != null ) {
if ( builder.documentIdSourcePropertyName.equals( entityPersister().getIdentifierPropertyName() ) ) {
Expand Down Expand Up @@ -98,11 +95,6 @@ public EntityPersister entityPersister() {
return entityPersister;
}

@Override
public IdentifierMapping identifierMapping() {
return identifierMapping;
}

@Override
public HibernateOrmEntityLoadingStrategy<? super E, ?> loadingStrategy() {
return loadingStrategy;
Expand Down Expand Up @@ -141,7 +133,6 @@ abstract static class AbstractBuilder<E> implements PojoTypeExtendedMappingColle
private final String hibernateOrmEntityName;
private String documentIdSourcePropertyName;
private ValueReadHandle<?> documentIdSourcePropertyHandle;
private IdentifierMapping identifierMapping;
private PojoPathFilter dirtyFilter;
private PojoPathFilter dirtyContainingAssociationFilter;
private final List<PojoRawTypeIdentifier<? super E>> ascendingSuperTypes;
Expand All @@ -161,11 +152,6 @@ public void documentIdSourceProperty(PojoPropertyModel<?> documentIdSourceProper
this.documentIdSourcePropertyHandle = documentIdSourceProperty.handle();
}

@Override
public void identifierMapping(IdentifierMapping identifierMapping) {
this.identifierMapping = identifierMapping;
}

@Override
public void dirtyFilter(PojoPathFilter dirtyFilter) {
this.dirtyFilter = dirtyFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.search.engine.backend.Backend;
import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.backend.index.IndexManager;
import org.hibernate.search.engine.backend.reporting.spi.BackendMappingHints;
import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
Expand Down Expand Up @@ -57,7 +56,6 @@
import org.hibernate.search.mapper.orm.session.impl.ConfiguredAutomaticIndexingStrategy;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSearchSession;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSearchSessionMappingContext;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSessionTypeContext;
import org.hibernate.search.mapper.orm.spi.BatchMappingContext;
import org.hibernate.search.mapper.orm.tenancy.spi.TenancyConfiguration;
import org.hibernate.search.mapper.pojo.mapping.spi.AbstractPojoMappingImplementor;
Expand All @@ -75,7 +73,6 @@
@SuppressWarnings("deprecation")
public class HibernateOrmMapping extends AbstractPojoMappingImplementor<HibernateOrmMapping>
implements SearchMapping, AutoCloseable, HibernateOrmMappingContext,
EntityReferenceFactory<org.hibernate.search.mapper.orm.common.EntityReference>,
HibernateOrmListenerContextProvider, BatchMappingContext,
HibernateOrmScopeMappingContext, HibernateOrmSearchSessionMappingContext,
AutomaticIndexingMappingContext, CoordinationStrategyContext {
Expand Down Expand Up @@ -114,7 +111,8 @@ public static MappingImplementor<HibernateOrmMapping> create(
SchemaManagementListener schemaManagementListener = new SchemaManagementListener( schemaManagementStrategyName );

return new HibernateOrmMapping(
mappingDelegate, typeContextContainer, sessionFactory,
mappingDelegate,
typeContextContainer, sessionFactory,
coordinationStrategyHolder,
configuredAutomaticIndexingStrategy,
cacheLookupStrategy, fetchSize,
Expand Down Expand Up @@ -145,7 +143,7 @@ private HibernateOrmMapping(PojoMappingDelegate mappingDelegate,
EntityLoadingCacheLookupStrategy cacheLookupStrategy,
int fetchSize,
SchemaManagementListener schemaManagementListener) {
super( mappingDelegate );
super( mappingDelegate, org.hibernate.search.mapper.orm.common.impl.HibernateOrmEntityReference::new );
this.typeContextContainer = typeContextContainer;
this.sessionFactory = sessionFactory;
this.coordinationStrategyHolder = coordinationStrategyHolder;
Expand Down Expand Up @@ -222,17 +220,6 @@ public ProjectionMappedTypeContext mappedTypeContext(String mappedTypeName) {
return typeContextContainer.indexedByJpaEntityName().getOrFail( mappedTypeName );
}

@Override
public EntityReferenceFactory<org.hibernate.search.mapper.orm.common.EntityReference> entityReferenceFactory() {
return this;
}

@Override
public org.hibernate.search.mapper.orm.common.EntityReference createEntityReference(String typeName, Object identifier) {
HibernateOrmSessionTypeContext<?> typeContext = typeContextContainer.byJpaEntityName().getOrFail( typeName );
return new org.hibernate.search.mapper.orm.common.impl.HibernateOrmEntityReference( typeContext.typeIdentifier(), typeContext.jpaEntityName(), identifier );
}

@Override
public <T> SearchScope<T> scope(Collection<? extends Class<? extends T>> types) {
return createScope( types );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.hibernate.search.mapper.orm.scope.impl;

import org.hibernate.search.engine.backend.common.spi.DocumentReferenceConverter;
import org.hibernate.search.mapper.orm.loading.impl.LoadingSessionContext;
import org.hibernate.search.mapper.orm.massindexing.impl.HibernateOrmMassIndexingSessionContext;
import org.hibernate.search.mapper.orm.spi.BatchSessionContext;
Expand All @@ -16,7 +15,4 @@ public interface HibernateOrmScopeSessionContext
extends PojoScopeSessionContext, LoadingSessionContext, HibernateOrmMassIndexingSessionContext,
BatchSessionContext {

@Deprecated
DocumentReferenceConverter<org.hibernate.search.mapper.orm.common.EntityReference> documentReferenceConverter();

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public SearchScopeImpl(HibernateOrmScopeMappingContext mappingContext,

public SearchQuerySelectStep<?, org.hibernate.search.mapper.orm.common.EntityReference, E, SearchLoadingOptionsStep, ?, ?> search(
HibernateOrmScopeSessionContext sessionContext, HibernateOrmSelectionLoadingContext.Builder loadingContextBuilder) {
return delegate.search( sessionContext, sessionContext.documentReferenceConverter(),
loadingContextBuilder );
return delegate.search( sessionContext, loadingContextBuilder );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
import org.hibernate.engine.spi.ActionQueue;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.backend.common.spi.DocumentReferenceConverter;
import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep;
import org.hibernate.search.mapper.orm.automaticindexing.session.impl.DelegatingAutomaticIndexingSynchronizationStrategy;
Expand Down Expand Up @@ -56,7 +54,7 @@
@SuppressWarnings("deprecation")
public class HibernateOrmSearchSession extends AbstractPojoSearchSession
implements SearchSession, HibernateOrmSessionContext, HibernateOrmScopeSessionContext,
SearchIndexingPlanSessionContext, DocumentReferenceConverter<org.hibernate.search.mapper.orm.common.EntityReference>,
SearchIndexingPlanSessionContext,
AutomaticIndexingEventSendingSessionContext {

/**
Expand Down Expand Up @@ -219,20 +217,6 @@ public EntityReferenceFactory<?> entityReferenceFactory() {
return mappingContext.entityReferenceFactory();
}

@Override
public DocumentReferenceConverter<org.hibernate.search.mapper.orm.common.EntityReference> documentReferenceConverter() {
return this;
}

@Override
public org.hibernate.search.mapper.orm.common.EntityReference fromDocumentReference(DocumentReference reference) {
HibernateOrmSessionTypeContext<?> typeContext =
typeContextProvider.byJpaEntityName().getOrFail( reference.typeName() );
Object id = typeContext.identifierMapping()
.fromDocumentIdentifier( reference.id(), this );
return new org.hibernate.search.mapper.orm.common.impl.HibernateOrmEntityReference( typeContext.typeIdentifier(), typeContext.jpaEntityName(), id );
}

@Override
public PojoSelectionLoadingContext defaultLoadingContext() {
return loadingContextBuilder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.hibernate.search.mapper.orm.session.impl;

import org.hibernate.search.mapper.pojo.identity.spi.IdentifierMapping;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;

/**
Expand All @@ -16,8 +15,6 @@ public interface HibernateOrmSessionTypeContext<E> {

PojoRawTypeIdentifier<E> typeIdentifier();

IdentifierMapping identifierMapping();

String jpaEntityName();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.common.spi;

import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;

/**
* A delegate for the POJO implementation of {@link org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory}.
* <p>
* Implementations of this class generally simply call a constructor.
*/
@FunctionalInterface
public interface PojoEntityReferenceFactoryDelegate {

EntityReference create(PojoRawTypeIdentifier<?> typeIdentifier, String name, Object id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.mapper.pojo.mapping.impl;

import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReferenceFactoryDelegate;
import org.hibernate.search.mapper.pojo.work.impl.PojoWorkTypeContext;

final class PojoEntityReferenceFactory implements EntityReferenceFactory<EntityReference> {

public final PojoEntityReferenceFactoryDelegate delegate;
private final PojoTypeManagerContainer typeManagers;

PojoEntityReferenceFactory(PojoEntityReferenceFactoryDelegate delegate,
PojoTypeManagerContainer typeManagers) {
this.delegate = delegate;
this.typeManagers = typeManagers;
}

@Override
public EntityReference createEntityReference(String typeName, Object identifier) {
PojoWorkTypeContext<?, ?> typeContext = typeManagers.byEntityName().getOrFail( typeName );
return delegate.create( typeContext.typeIdentifier(), typeContext.entityName(), identifier );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hibernate.search.engine.reporting.FailureHandler;
import org.hibernate.search.engine.search.projection.definition.spi.ProjectionRegistry;
import org.hibernate.search.engine.tenancy.spi.TenancyMode;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReferenceFactoryDelegate;
import org.hibernate.search.mapper.pojo.search.definition.impl.PojoSearchQueryElementRegistry;
import org.hibernate.search.mapper.pojo.work.impl.PojoIndexingPlanEventProcessingStrategy;
import org.hibernate.search.mapper.pojo.work.impl.PojoIndexingPlanEventSendingStrategy;
Expand Down Expand Up @@ -83,6 +84,11 @@ public ProjectionRegistry projectionRegistry() {
return searchQueryElementRegistry;
}

@Override
public PojoEntityReferenceFactory createEntityReferenceFactory(PojoEntityReferenceFactoryDelegate delegate) {
return new PojoEntityReferenceFactory( delegate, typeManagers );
}

@Override
public <R extends EntityReference, E, C> PojoScopeDelegate<R, E, C> createPojoScope(
PojoScopeMappingContext mappingContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.concurrent.CompletableFuture;

import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.backend.reporting.spi.BackendMappingHints;
import org.hibernate.search.engine.backend.types.converter.runtime.ToDocumentValueConvertContext;
import org.hibernate.search.engine.backend.types.converter.runtime.spi.ToDocumentValueConvertContextImpl;
Expand All @@ -23,6 +24,8 @@
import org.hibernate.search.mapper.pojo.bridge.runtime.ValueBridgeToIndexedValueContext;
import org.hibernate.search.mapper.pojo.bridge.runtime.impl.IdentifierBridgeToDocumentIdentifierContextImpl;
import org.hibernate.search.mapper.pojo.bridge.runtime.impl.ValueBridgeToIndexedValueContextImpl;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReferenceFactoryDelegate;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReference;
import org.hibernate.search.mapper.pojo.scope.spi.PojoScopeMappingContext;
import org.hibernate.search.mapper.pojo.session.spi.PojoSearchSessionMappingContext;
import org.hibernate.search.mapper.pojo.work.spi.PojoIndexer;
Expand All @@ -39,12 +42,29 @@ public abstract class AbstractPojoMappingImplementor<M>

private boolean stopped = false;

private final PojoEntityReferenceFactoryDelegate entityReferenceFactoryDelegate;
private final EntityReferenceFactory<?> entityReferenceFactory;
private final ToDocumentValueConvertContext toDocumentValueConvertContext;
private final IdentifierBridgeToDocumentIdentifierContext toDocumentIdentifierContext;
private final ValueBridgeToIndexedValueContext toIndexedValueContext;

public AbstractPojoMappingImplementor(PojoMappingDelegate delegate) {
this( delegate, PojoEntityReference::new );
}

/**
* @param delegate The {@link PojoMappingDelegate}
* @param entityReferenceFactoryDelegate The {@link PojoEntityReferenceFactoryDelegate},
* used to implement the {@link EntityReferenceFactory}.
* @deprecated Use {@link AbstractPojoMappingImplementor}.
* This constructor is only present for backwards compatibility, for mappers that expose a custom entity reference type.
*/
@Deprecated
public AbstractPojoMappingImplementor(PojoMappingDelegate delegate,
PojoEntityReferenceFactoryDelegate entityReferenceFactoryDelegate) {
this.delegate = delegate;
this.entityReferenceFactoryDelegate = entityReferenceFactoryDelegate;
this.entityReferenceFactory = delegate.createEntityReferenceFactory( entityReferenceFactoryDelegate );
this.toDocumentValueConvertContext = new ToDocumentValueConvertContextImpl( this );
this.toDocumentIdentifierContext = new IdentifierBridgeToDocumentIdentifierContextImpl( this );
this.toIndexedValueContext = new ValueBridgeToIndexedValueContextImpl( this );
Expand Down Expand Up @@ -89,6 +109,16 @@ public FailureHandler failureHandler() {
return delegate().failureHandler();
}

@Override
public final PojoEntityReferenceFactoryDelegate entityReferenceFactoryDelegate() {
return entityReferenceFactoryDelegate;
}

@Override
public final EntityReferenceFactory<?> entityReferenceFactory() {
return entityReferenceFactory;
}

@Override
public final ToDocumentValueConvertContext toDocumentValueConvertContext() {
return toDocumentValueConvertContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import java.util.Collection;
import java.util.Optional;

import org.hibernate.search.engine.backend.common.spi.EntityReferenceFactory;
import org.hibernate.search.engine.backend.work.execution.DocumentCommitStrategy;
import org.hibernate.search.engine.backend.work.execution.DocumentRefreshStrategy;
import org.hibernate.search.engine.common.EntityReference;
import org.hibernate.search.engine.environment.thread.spi.ThreadPoolProvider;
import org.hibernate.search.engine.reporting.FailureHandler;
import org.hibernate.search.engine.search.projection.definition.spi.ProjectionRegistry;
import org.hibernate.search.engine.tenancy.spi.TenancyMode;
import org.hibernate.search.mapper.pojo.common.spi.PojoEntityReferenceFactoryDelegate;
import org.hibernate.search.mapper.pojo.work.spi.PojoIndexingQueueEventProcessingPlan;
import org.hibernate.search.mapper.pojo.work.spi.PojoIndexingQueueEventSendingPlan;
import org.hibernate.search.mapper.pojo.model.spi.PojoRawTypeIdentifier;
Expand All @@ -39,6 +41,8 @@ public interface PojoMappingDelegate extends AutoCloseable {

ProjectionRegistry projectionRegistry();

EntityReferenceFactory<?> createEntityReferenceFactory(PojoEntityReferenceFactoryDelegate delegate);

<R extends EntityReference, E, C> PojoScopeDelegate<R, E, C> createPojoScope(
PojoScopeMappingContext mappingContext,
Collection<? extends PojoRawTypeIdentifier<? extends E>> targetedTypes,
Expand Down

0 comments on commit 896e56f

Please sign in to comment.