For my web app I have to implement full text search - as I'm already using Hibernate with Spring Data JPA (on top of Spring Boot) I decided to implement Hibernate Search for full text Lucene queries. Everything works as expected but after implementing Hibernate Search I started to receive warnings:
Hibernate's legacy org.hibernate.Criteria API is deprecated; use the JPA javax.persistence.criteria.CriteriaQuery instead
I don't use Criteria API for my Hibernate Search queries (as it is discouraged by Hibernate Search documentation anyway), my code for querying basically looks as follows:
import org.apache.lucene.search.Query;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.BooleanJunction;
import org.hibernate.search.query.dsl.QueryBuilder;
...
@Override
@Transactional
public List<Picture> fullTextSearchByCriteria(List<SearchCriteria> criteria, String pageString) {
final FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Picture.class).get();
final Query luceneQuery = buildFromCriteria(queryBuilder, criteria);
final FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Picture.class);
/* some fine tunning here with calls to .setFirstResult(), .setMaxResults() and
.getResultSize() for pagination. It's used for page wrapper that I omitted here for simplicity sake */
return jpaQuery.getResultList()
}
and buildFromCriteria(queryBuilder, criteria)
is a method that builds boolean junction (also SearchCriteria
class is just a regular POJO, nothing interesting there). This method looks as follows:
@Transactional
private Query buildFromCriteria(QueryBuilder queryBuilder, List<SearchCriteria> criteria) {
if (criteria == null)
return queryBuilder.all().createQuery();
BooleanJunction<BooleanJunction> junction = queryBuilder.bool();
for (SearchCriteria c : criteria) {
junction.must(
queryBuilder
.keyword()
.onField(c.getField())
.matching(c.getValue())
.createQuery()
);
}
return junction.createQuery();
}
Every use of this full text search results in Criteria API deprecation warning.
Does it use deprecated Criteria API under the hood? If so why Hibernate Search would use API that Hibernate deprecated? Actually I also got some deprecation warnings at application startup, when I call
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
for initial indexing.
Notes: I'm using Springs Specification in other part of app, that I would expect may use Criteria API under the hood, but I don't get deprecation warnings when using it and also didn't get any warning before Hibernate Search was added.
EntityManager is obtained via @PersistanceContext
injection.
I would like to get rid of deprecated API usages if possible but I don't understand where Criteria API is used in this case and if it is even possible to change it to JPA CriteriaQuery (as I don't use it explicitly).
My dependencies with versions that I think might be important:
Also using some extra dependencies for Analyzers:
The conversion of Hibernate Search to the latest changes of Hibernate ORM is a complex work in progress. You're hitting HSEARCH-2381.