问题
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();
回答1:
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.
来源:https://stackoverflow.com/questions/58489569/how-to-index-a-inherited-field-in-hibernate-search