Hibernate Search With JPQL

Hello everyone,

I’m currently in the process of migrating our project to Hibernate Search version 6, following the guide you provided. However, I have a few questions that remain unanswered.

In our project, we rely on Specifications for filtering, sorting, and manipulating data due to the presence of multiple relational tables. Now, we want to perform a full-text search on fields like Student fullName, lastName, citizenNumber, code, and so on. We’ve begun implementing Hibernate Search into our project and have made significant progress. However, we’ve encountered an issue that has brought us to a halt.

In our project, we obtain filters via criteria and build specifications for criteria filters. The problem arises when we attempt to integrate Hibernate Search specifications (Predicate) into our existing Specification (javax.persistence.criteria).

We’ve encountered the following error:

Cannot cast 'org.hibernate.search.engine.search.predicate.dsl.impl.MatchPredicateFieldMoreStepImpl$CommonState' to 'javax.persistence.criteria.Predicate'

and we’ve implemented the following code snippet:

public Specification<Student> searchTypesByFirstName(String searchTerm) {
        return (root, query, builder) -> {
            SearchSession searchSession = Search.session(entityManager);

            SearchPredicateFactory predicateFactory = searchSession.scope(Student.class)

            // Type sınıfında yer alan firstName alanında eşleşme sağlayan bir özel filtre oluşturun.
            org.hibernate.search.engine.search.predicate.dsl.PredicateFinalStep predicate = predicateFactory.match()

            Predicate jpaPredicate = (Predicate) predicate.toPredicate();

            return jpaPredicate;

My question, “How can we implement a Hibernate Search predicate into JPQL (javax.persistence.criteria.Predicate)? Is this possible, or is there another solution?”



This cast is simply invalid. There is no relationship whatsoever between JPA predicates and Hibernate Search predicates. They just have a similar (and rather generic) name, that’s all.

I’m not entirely sure what you’re trying to achieve exactly, but if you intend to put Hibernate Search predicates in a JPA query, that simply won’t work.
JPA runs queries against the database, while Hibernate Search runs queries against Lucene/Elasticsearch, and those cannot be mixed at the moment (see HSEARCH-3630).
Hibernate Search doesn’t implement JPA and probably never will; it just integrates with Hibernate ORM for indexing and for loading matching entities after it ran its query against Lucene/Elasticsearch.

You can find more information on how to run a Hibernate Search query in the getting started guide.

1 Like

Thank you, @yrodiere, for your response. I’ve been scouring various blogs, websites, and AI platforms, such as ChatGPT, in search of a solution. However, I couldn’t find a definitive one. It appears that Hibernate Search and JPQL might seem similar at first i see, but they differ significantly in sophistication.

At the moment, I’m certain that I won’t be transitioning from Hibernate Search query predicates to JPQL predicates. Thanks for all, I’m hopeful for a prompt response and support. (like HSEARCH-3630) (P.S. I did come across a potential solution that you contributed to, but unfortunately, it seems that support for it has been discontinued on https://github.com/snowdrop/spring-data-snowdrop.)

Ah, I take it that Specification thing is a Spring Data concept, then.

Yes that project you linked to has fallen behind and has never been updated to Hibernate Search 6. I had my doubts about it anyway, since Spring Data assumes a 1-1 mapping between entity properties and backend “fields”, which is really not the case in Hibernate Search. So, not a great fit.

1 Like