2

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();
alexcornejo
  • 163
  • 1
  • 12
  • Did you try to declare "private Key id;" as protected instead of private? – Stefania Oct 21 '19 at 16:01
  • As far as I know this should work correctly. Which version of Hibernate Search are you using? What is the type of the `languageId` variable in your query code? – yrodiere Oct 21 '19 at 16:06
  • @Stefania I changed the id modifier to "protected", but the result is the same, I forgot to specify I have all access methods for the properties. – alexcornejo Oct 21 '19 at 16:42
  • @yrodiere languageId is Long type, I am using version 5.11.2.Final. The behavior is very strange, I suppose it is due to I am inheriting from an abstract class?, Or maybe I need to put another annotation over "Id" property? – alexcornejo Oct 21 '19 at 16:46
  • The most likely explanation is that a query of the wrong type is generated. Please add the full code of your query, including how you create the queryBuilder in particular. – yrodiere Oct 21 '19 at 17:29
  • @yrodiere please check the UPDATE 2. – alexcornejo Oct 21 '19 at 21:01

1 Answers1

4

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.

yrodiere
  • 9,280
  • 1
  • 13
  • 35
  • This saved my day. I haven't found this tweak anywhere. Thanks :) But one more doubt, Every time I query on `@IndexedEmbedded` entity's ID, single SQL query is triggered. Is this correct behaviour? I checked indexes using Luke, and they look OK. – Jignesh M. Khatri Nov 11 '20 at 05:41
  • I don't understand what you mean by "I query on @IndexedEmbedded entity's ID". Maybe a new question with some code is in order? – yrodiere Nov 12 '20 at 08:25
  • I mean, I understand it as "I apply a predicate on the ID", but that has nothing to do with SQL queries, so you probably mean something else. Yeah, a code example a the logs would help. – yrodiere Nov 12 '20 at 08:26