Search in sources :

Example 6 with ProgrammaticMappingConfigurationContext

use of org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext in project hibernate-search by hibernate.

the class MySearchMappingConfigurer method configure.

@Override
public void configure(HibernateOrmMappingConfigurationContext context) {
    // <1>
    ProgrammaticMappingConfigurationContext mapping = context.programmaticMapping();
    // <2>
    TypeMappingStep bookMapping = mapping.type(Book.class);
    // <3>
    bookMapping.indexed();
    // <4>
    bookMapping.property("title").fullTextField().analyzer(// <5>
    "english");
}
Also used : TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) ProgrammaticMappingConfigurationContext(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext)

Example 7 with ProgrammaticMappingConfigurationContext

use of org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext in project hibernate-search by hibernate.

the class ProgrammaticMappingAccessTypeIT method setup.

@Before
public void setup() {
    backendMock.expectSchema(IndexedEntity.NAME, b -> b.field("propertyWithGetterAndFieldDeclaredInParent", String.class).field("propertyWithFieldDeclaredInParent", String.class).field("propertyWithFieldDeclaredInParentThenGetterInChild", String.class).field("propertyWithGetterAndFieldDeclaredInParentThenOverridden", String.class).field("propertyWithGetterDeclaredAbstractInParent", String.class).field("propertyWithGetterAndField", String.class).field("propertyWithFieldOnly", String.class).field("propertyWithGetterAndDifferentlyNamedField", String.class));
    mapping = setupHelper.start().withConfiguration(b -> {
        ProgrammaticMappingConfigurationContext mapping = b.programmaticMapping();
        TypeMappingStep indexedEntityMapping = mapping.type(IndexedEntity.class);
        indexedEntityMapping.indexed().index(IndexedEntity.NAME);
        indexedEntityMapping.property("id").documentId();
        indexedEntityMapping.property("propertyWithGetterAndFieldDeclaredInParent").genericField();
        indexedEntityMapping.property("propertyWithFieldDeclaredInParent").genericField();
        indexedEntityMapping.property("propertyWithFieldDeclaredInParentThenGetterInChild").genericField();
        indexedEntityMapping.property("propertyWithGetterAndFieldDeclaredInParentThenOverridden").genericField();
        indexedEntityMapping.property("propertyWithGetterDeclaredAbstractInParent").genericField();
        indexedEntityMapping.property("propertyWithGetterAndField").genericField();
        indexedEntityMapping.property("propertyWithFieldOnly").genericField();
        indexedEntityMapping.property("propertyWithGetterAndDifferentlyNamedField").genericField();
    }).setup(IndexedEntity.class, ParentIndexedEntity.class);
    backendMock.verifyExpectationsMet();
}
Also used : TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) ProgrammaticMappingConfigurationContext(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext) Before(org.junit.Before)

Example 8 with ProgrammaticMappingConfigurationContext

use of org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext in project hibernate-search by hibernate.

the class DocumentationSetupHelper method testParamsForBothAnnotationsAndProgrammatic.

public static List<DocumentationSetupHelper> testParamsForBothAnnotationsAndProgrammatic(BackendSetupStrategy backendSetupStrategy, BackendConfiguration defaultBackendConfiguration, Set<Class<?>> additionalAnnotatedClasses, Consumer<ProgrammaticMappingConfigurationContext> programmaticMappingContributor) {
    List<DocumentationSetupHelper> result = new ArrayList<>();
    // Annotation-based mapping
    HibernateOrmSearchMappingConfigurer annotationMappingConfigurer = additionalAnnotatedClasses.isEmpty() ? null : context -> context.annotationMapping().add(additionalAnnotatedClasses);
    result.add(new DocumentationSetupHelper(backendSetupStrategy, null, annotationMappingConfigurer));
    // Programmatic mapping
    HibernateOrmSearchMappingConfigurer programmaticMappingConfigurer = context -> programmaticMappingContributor.accept(context.programmaticMapping());
    result.add(new DocumentationSetupHelper(backendSetupStrategy, false, programmaticMappingConfigurer));
    return result;
}
Also used : Arrays(java.util.Arrays) SchemaManagementStrategyName(org.hibernate.search.mapper.orm.schema.management.SchemaManagementStrategyName) SimpleSessionFactoryBuilder(org.hibernate.search.util.impl.integrationtest.mapper.orm.SimpleSessionFactoryBuilder) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) HibernateOrmMappingHandle(org.hibernate.search.util.impl.integrationtest.mapper.orm.HibernateOrmMappingHandle) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Consumer(java.util.function.Consumer) AutomaticIndexingSynchronizationStrategyNames(org.hibernate.search.mapper.orm.automaticindexing.session.AutomaticIndexingSynchronizationStrategyNames) List(java.util.List) ProgrammaticMappingConfigurationContext(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext) HibernateOrmMapperSettings(org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings) HibernateOrmSearchMappingConfigurer(org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer) Map(java.util.Map) MappingSetupHelper(org.hibernate.search.util.impl.integrationtest.common.rule.MappingSetupHelper) BackendMappingHandle(org.hibernate.search.util.impl.integrationtest.common.stub.backend.BackendMappingHandle) BackendSetupStrategy(org.hibernate.search.util.impl.integrationtest.common.rule.BackendSetupStrategy) Collections(java.util.Collections) BackendConfiguration(org.hibernate.search.util.impl.integrationtest.common.rule.BackendConfiguration) HibernateOrmSearchMappingConfigurer(org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer) ArrayList(java.util.ArrayList)

Example 9 with ProgrammaticMappingConfigurationContext

use of org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext in project hibernate-search by hibernate.

the class DefaultReindexOnUpdateIT method no_associationInverseSideUnknown.

/**
 * If ReindexOnUpdate.NO is the default,
 * even if the inverse side of associations is not correctly set up,
 * then Hibernate Search will handle embedding, but not automatic reindexing.
 */
@Test
public void no_associationInverseSideUnknown() {
    backendMock.expectSchema("ParentEntity", b -> b.field("value", String.class).objectField("child", b2 -> b2.field("value", String.class)));
    backendMock.expectSchema("ChildEntity", b -> b.field("value", String.class));
    mapping = setupHelper.start().withConfiguration(builder -> {
        builder.defaultReindexOnUpdate(ReindexOnUpdate.NO);
        builder.addEntityType(ParentEntity.class);
        builder.addEntityType(ChildEntity.class);
        ProgrammaticMappingConfigurationContext mappingDefinition = builder.programmaticMapping();
        TypeMappingStep parentMapping = mappingDefinition.type(ParentEntity.class);
        parentMapping.indexed();
        parentMapping.property("id").documentId();
        parentMapping.property("value").genericField();
        parentMapping.property("child").indexedEmbedded();
        TypeMappingStep childMapping = mappingDefinition.type(ChildEntity.class);
        childMapping.indexed();
        childMapping.property("id").documentId();
        childMapping.property("value").genericField();
    }).setup();
    backendMock.verifyExpectationsMet();
    ParentEntity parent = new ParentEntity();
    parent.id = 1;
    parent.value = "val1";
    ChildEntity child = new ChildEntity();
    child.id = 2;
    child.value = "val2";
    parent.child = child;
    child.parent = parent;
    // Test indexed-embedding
    try (SearchSession session = mapping.createSession()) {
        session.indexingPlan().add(parent);
        backendMock.expectWorks("ParentEntity").add("1", b -> b.field("value", "val1").objectField("child", b2 -> b2.field("value", "val2")));
    }
    // Test automatic reindexing
    try (SearchSession session = mapping.createSession()) {
        session.indexingPlan().addOrUpdate(child);
        backendMock.expectWorks("ChildEntity").addOrUpdate("2", b -> b.field("value", "val2"));
    // The child was updated, but automatic reindexing is disabled,
    // thus the parent (which index-embeds the childs) will NOT be reindexed.
    }
}
Also used : ReindexOnUpdate(org.hibernate.search.mapper.pojo.automaticindexing.ReindexOnUpdate) SearchException(org.hibernate.search.util.common.SearchException) PojoModelPath(org.hibernate.search.mapper.pojo.model.path.PojoModelPath) MethodHandles(java.lang.invoke.MethodHandles) JavaBeanMappingSetupHelper(org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test) SearchMapping(org.hibernate.search.mapper.javabean.mapping.SearchMapping) ProgrammaticMappingConfigurationContext(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext) Rule(org.junit.Rule) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) FailureReportUtils(org.hibernate.search.util.impl.integrationtest.common.reporting.FailureReportUtils) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) Before(org.junit.Before) TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) ProgrammaticMappingConfigurationContext(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext) SearchSession(org.hibernate.search.mapper.javabean.session.SearchSession) Test(org.junit.Test)

Example 10 with ProgrammaticMappingConfigurationContext

use of org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext in project hibernate-search by hibernate.

the class DefaultReindexOnUpdateIT method default_associationInverseSideUnknown.

/**
 * If ReindexOnUpdate.DEFAULT is the default,
 * and the inverse side of associations is NOT correctly set up,
 * then Hibernate Search bootstrap will fail.
 */
@Test
public void default_associationInverseSideUnknown() {
    assertThatThrownBy(() -> setupHelper.start().withConfiguration(builder -> {
        builder.defaultReindexOnUpdate(ReindexOnUpdate.DEFAULT);
        builder.addEntityType(ParentEntity.class);
        builder.addEntityType(ChildEntity.class);
        ProgrammaticMappingConfigurationContext mappingDefinition = builder.programmaticMapping();
        TypeMappingStep parentMapping = mappingDefinition.type(ParentEntity.class);
        parentMapping.indexed();
        parentMapping.property("id").documentId();
        parentMapping.property("value").genericField();
        parentMapping.property("child").indexedEmbedded();
        TypeMappingStep childMapping = mappingDefinition.type(ChildEntity.class);
        childMapping.indexed();
        childMapping.property("id").documentId();
        childMapping.property("value").genericField();
    }).setup()).isInstanceOf(SearchException.class).satisfies(FailureReportUtils.hasFailureReport().typeContext(ParentEntity.class.getName()).pathContext(".child<no value extractors>.value<no value extractors>").failure("Unable to find the inverse side of the association on type '" + ParentEntity.class.getName() + "'" + " at path '.child<no value extractors>'", "Hibernate Search needs this information in order to reindex '" + ParentEntity.class.getName() + "' when '" + ChildEntity.class.getName() + "' is modified."));
}
Also used : TypeMappingStep(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep) ProgrammaticMappingConfigurationContext(org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext) SearchException(org.hibernate.search.util.common.SearchException) Test(org.junit.Test)

Aggregations

ProgrammaticMappingConfigurationContext (org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext)11 TypeMappingStep (org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep)8 Before (org.junit.Before)5 Test (org.junit.Test)5 MethodHandles (java.lang.invoke.MethodHandles)4 JavaBeanMappingSetupHelper (org.hibernate.search.integrationtest.mapper.pojo.testsupport.util.rule.JavaBeanMappingSetupHelper)4 SearchMapping (org.hibernate.search.mapper.javabean.mapping.SearchMapping)4 SearchSession (org.hibernate.search.mapper.javabean.session.SearchSession)4 BackendMock (org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock)4 Rule (org.junit.Rule)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 PojoModelPath (org.hibernate.search.mapper.pojo.model.path.PojoModelPath)3 SearchException (org.hibernate.search.util.common.SearchException)3 Consumer (java.util.function.Consumer)2 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)2 SessionFactory (org.hibernate.SessionFactory)2