如何在Hibernate搜索中为继承的字段建立索引? [英] How to index a inherited field in Hibernate-search?

查看:83
本文介绍了如何在Hibernate搜索中为继承的字段建立索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java jpa Hibernate搜索应用程序中工作,我知道一个实体中每个@Id注释都会自动为Hibernate搜索索引.问题是我有一个包含@Id批注的主域"类,然后又有一个继承了主域"的类,这似乎是Hibernate搜索无法识别继承的@Id字段.>

这是我的主域类.

  @MappedSuperclass@Inheritance(strategy = InheritanceType.JOINED)公共抽象类MasterDomain< Key扩展Object>实现可序列化{@ID@GeneratedValue(策略= GenerationType.IDENTITY)私钥ID;} 

我有一个继承于该类的语言"类:

  @Indexed@实体公共类语言扩展了MasterDomain< Long> {@场地私有字符串名称;} 

最后,我还有一个名为"LanguageRelation"的类,它与Language类有关.看起来像:

  @Indexed@实体公共类LanguageRelation扩展了MasterDomain< Long> {@IndexedEmbedded私人语言} 

因此,当我构建一个Lucene查询来搜索LanguageRelation实体时,我可以按如下语言名称进行搜索:

  queryBuilder.keyword().onField("language.name").matching(languageName).createQuery() 

但是我无法通过语言ID进行搜索,就像这样:

  queryBuilder.keyword().onField("language.id").matching(languageId).createQuery() 

先前的查询返回0个结果,如您所见,似乎Hibernate搜索无法识别从MasterDomain继承的@Id,有什么建议吗?

更新1 => 我忘了告诉MasterDomain类在试图执行Lucene查询的单独模块中.也许这可能会引起问题?

更新2 这是我尝试构建Lucene查询的完整代码.

  FullTextEntityManager fullTextEntityManager= Search.getFullTextEntityManager(entityManager);org.hibernate.search.query.dsl.QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(LanguageRelation.class).得到();长语言ID = 29公升;org.apache.lucene.search.Query查询= queryBuilder.keyword().onField("language.id").matching(languageId).createQuery();org.hibernate.search.jpa.FullTextQuery fullTextQuery= fullTextEntityManager.createFullTextQuery(query,LanguageRelation.class);列表< LanguageRelation>resultList = fullTextQuery.getResultList(); 

解决方案

我认为问题很简单,因为默认情况下未嵌入ID.

尝试替换以下内容:

  @IndexedEmbedded 

与此:

  @IndexedEmbedded(includeEmbeddedObjectId = true) 

然后重新索引数据,然后再次运行查询.

I am working in a java jpa Hibernate-search application, I know Hibernate-search index automatically every @Id annotation in an entity. The problem is that I have a "master domain" class with contains the @Id annotation, and then I have another class with inherit "master domain", then seems to be the Hibernate search is not recognizing the @Id field inherited.

this is my master domain class.

@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class MasterDomain<Key extends Object> implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key id;
}

I have a class "Language" which inherits this class:

@Indexed
@Entity
public class Language extends MasterDomain<Long>{

    @Field
    private String name;
}

Finally I have another class called "LanguageRelation" which is related with Language class. It looks like:

@Indexed
@Entity
public class LanguageRelation extends MasterDomain<Long>{

   @IndexedEmbedded
   private Language language;
}

So, when I build a lucene query to search LanguageRelation entities, I am able to search by language names like this:

queryBuilder.keyword().onField("language.name").matching(languageName).createQuery()

But I am not able to search by language id, like this:

queryBuilder.keyword().onField("language.id").matching(languageId).createQuery()

Previous query returns 0 results, as you can see, it seems to be Hibernate search is not recognizing the @Id inherited from MasterDomain, any suggestion?

UPDATE 1 => I forgot to tell MasterDomain class is in separated module from which I am trying to execute the Lucene Query. Maybe this could get into the problem?

UPDATE 2 This is the full code of how I am trying to build my Lucene query.

FullTextEntityManager fullTextEntityManager
                = Search.getFullTextEntityManager(entityManager);

org.hibernate.search.query.dsl.QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory()
                .buildQueryBuilder()
                .forEntity(LanguageRelation.class)
                .get();

Long languageId = 29L;
org.apache.lucene.search.Query query = queryBuilder.keyword().onField("language.id").matching(languageId).createQuery();

org.hibernate.search.jpa.FullTextQuery fullTextQuery
                = fullTextEntityManager.createFullTextQuery(query, LanguageRelation.class);
List<LanguageRelation> resultList = fullTextQuery.getResultList();

解决方案

I think the problem is simply that the ID isn't embedded by default.

Try replacing this:

@IndexedEmbedded

With this:

@IndexedEmbedded(includeEmbeddedObjectId = true)

Then reindex your data, and run your query again.

这篇关于如何在Hibernate搜索中为继承的字段建立索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆