Hibernate Search 7.1.1无数据库集成搜索功能异常求助
解决方案
核心问题分析
你遇到的两个异常本质都是独立模式下Hibernate Search无法从索引数据重建MetadataSearch实体对象:
HSEARCH000614:缺少投影构造函数,Hibernate Search不知道如何用索引字段实例化实体HSEARCH900004:是投影配置冲突/缺失导致的衍生异常
步骤1:修复实体类,添加投影构造函数
给实体类的全参构造函数标注@ProjectionConstructor,让Hibernate Search明确用这个构造函数从索引字段重建实体。同时确保所有需要返回的字段都开启了projectable=YES:
import lombok.*; import lombok.experimental.Accessors; import org.hibernate.search.engine.backend.types.Projectable; import org.hibernate.search.mapper.pojo.mapping.definition.annotation.*; @Getter @Setter @NoArgsConstructor @AllArgsConstructor(onConstructor = @__(@ProjectionConstructor)) // 关键:添加投影构造函数注解 @Accessors(chain = true) @Indexed public class MetadataSearch { @DocumentId private Integer id; @GenericField(projectable = Projectable.YES) private Integer oId; @GenericField(projectable = Projectable.YES) private Integer cId; @GenericField(projectable = Projectable.YES) private Integer oParentId; @GenericField(projectable = Projectable.YES) private Integer oType; // 如果需要在搜索结果中返回这两个字段,必须开启projectable @GenericField(projectable = Projectable.YES) private Integer oStartVersion; @GenericField(projectable = Projectable.YES) private Integer oEndVersion; @FullTextField(projectable = Projectable.YES, analyzer = "english") private String oName; @FullTextField(analyzer = "english", projectable = Projectable.YES) private String oPhysicalName; @FullTextField(analyzer = "english", projectable = Projectable.YES) private String oDefinition; @FullTextField(analyzer = "english", projectable = Projectable.YES) private String oComment; }
步骤2:移除冲突的映射配置
你的SearchMappingConfig同时使用了注解映射和编程式映射,且关闭了注解发现,导致实体类上的注解配置失效。简化该类,保留必要配置即可:
import org.hibernate.search.mapper.pojo.standalone.mapping.StandalonePojoMappingConfigurationContext; import org.hibernate.search.mapper.pojo.standalone.mapping.StandalonePojoMappingConfigurer; public class SearchMappingConfig implements StandalonePojoMappingConfigurer { @Override public void configure(StandalonePojoMappingConfigurationContext context) { // 注释掉冲突的配置,保留注解映射的默认行为 // context.annotationMapping() // .discoverAnnotationsFromReferencedTypes(false) // .discoverAnnotatedTypesFromRootMappingAnnotations(false); // ProgrammaticMappingConfigurationContext mappingContext = context.programmaticMapping(); // TypeMappingStep mappingStep = mappingContext.type(MetadataSearch.class); // mappingStep.searchEntity(); // mappingStep.indexed(); // mappingStep.property("id").documentId(); // mappingStep.property("oName"); } }
步骤3:修复索引方法的语法错误
你的index()方法中存在字符串未闭合的语法错误,修正后才能正确索引数据:
public boolean index() { try (SearchSession session = searchMapping.createSession()) { MetadataSearch metadata = new MetadataSearch() .setId(6553610) .setCId(65536) .setOId(1) .setOParentId(0) .setOName("Hello") .setOPhysicalName("hello") .setODefinition("hello def") // 修复:添加闭合双引号 .setOComment("hello comment"); session.indexingPlan().addOrUpdate(metadata); return true; } }
验证效果
完成上述修改后:
- 调用
index()方法确认数据正常写入索引 - 调用
search()方法,Hibernate Search会通过@ProjectionConstructor标注的构造函数,用索引中的字段值实例化MetadataSearch对象,返回正确的搜索结果
内容的提问来源于stack exchange,提问作者sb29595
相关产品推荐
相关产品推荐

