Java Code Examples for javax.persistence.criteria.CriteriaBuilder.createTupleQuery()

The following are Jave code examples for showing how to use createTupleQuery() of the javax.persistence.criteria.CriteriaBuilder class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: stroom-query   File: QueryServiceCriteriaImpl.java   Source Code and License Vote up 6 votes
@Override
public Optional<SearchResponse> search(final ServiceUser user,
                                       final SearchRequest request){
    final String dataSourceUuid = request.getQuery().getDataSource().getUuid();

    try (final Session session = database.openSession()) {
        final CriteriaBuilder cb = session.getCriteriaBuilder();

        final CriteriaQuery<Tuple> cq = cb.createTupleQuery();
        final Root<T> root = cq.from(this.dtoClass);

        cq.multiselect(this.fields.stream()
                .map(f -> root.get(f.getName()))
                .collect(Collectors.toList()));

        final Predicate requestPredicate = getPredicate(cb, root, request.getQuery().getExpression());
        final Predicate dataSourcePredicate = cb.equal(root.get(QueryableEntity.DATA_SOURCE_UUID), dataSourceUuid);

        cq.where(cb.and(requestPredicate, dataSourcePredicate));
        final List<Tuple> tuples = session.createQuery(cq).getResultList();
        final SearchResponse searchResponse = projectResults(request, tuples);

        return Optional.of(searchResponse);
    }
}