José Sanabria
José Sanabria

Reputation: 31

Hibernate Search and Hibernate Criteria

I have the following problem using Hibernate Search 5.5.2 with Hibernate Criteria:

Criteria criteria = session.createCriteria(Descriptor.class).add(
                Restrictions.eq("estadoBD", true));
//criteria.setProjection(Projections.property("idDescriptor"));

QueryBuilder queryBuilderDescriptor = fullTextSession
                .getSearchFactory().buildQueryBuilder()
                .forEntity(Descriptor.class).get();
org.apache.lucene.search.Query querySearchDescriptor = queryBuilderDescriptor
                .keyword().onFields("valor").matching(buscar).createQuery();
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(
                querySearchDescriptor, Descriptor.class).setCriteriaQuery(
                criteria);

resultados = fullTextQuery.list();

Output: ---> RESULTADO QUERY LIST SIZE ---> 3

But when I add a projection to the Criteria object does not return my results

Criteria criteria = session.createCriteria(Descriptor.class).add(
            Restrictions.eq("estadoBD", true));
criteria.setProjection(Projections.property("idDescriptor"));

Output: ---> RESULTADO QUERY LIST SIZE ---> 0

Why does this happen?

Upvotes: 1

Views: 263

Answers (1)

Sanne
Sanne

Reputation: 6107

The Criteria should only be used to customize fetch options. This is documented in 5.1.3.4. Fetching strategy. We should improve the validation as I would expect Hibernate Search to throw an exception for your use case.

To use projections with a FullTextQuery, don't apply the projection options to the loading Criteria but apply them to the FullTextQuery directly. See "5.1.3.5. Projection" for a full example, in the same chapter as about Fetching.

Upvotes: 1

Related Questions