简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM