Question

I have a Spring MVC/Spring Data / Mongo DB application. I have setted up my environement according the the spring data documentation and my repositories work fine (I can execute queries with predicates)

I was wondering if it was possible to execute a type safe query (using Spring Data and QueryDSL) while making a projection (I want only a few fields of a very big document).

The QueryDSL documentation gives an example for Hibernate but states it can be done in all modules QueryDSL Documentation (but I haven't been able to find out how to do it with Mongo)

here's the code snippet for hibernate

class CustomerDTO {

  @QueryProjection
  public CustomerDTO(long id, String name){
     ...
 }

 QCustomer customer = QCustomer.customer;
 JPQLQuery query = new HibernateQuery(session);
 List<CustomerDTO> dtos = qry.from(customer).list(new QCustomerDTO(customer.id,    customer.name));     

Any Ideas ?

Was it helpful?

Solution

This is currently not supported. Feel free to add a ticket for it into our Issue tracker.

The Lucene and Mongodb modules of Querydsl support only direct projections from the query root, but for custom projections something could be figured out.

OTHER TIPS

I've just built a projection like this:

Criteria c1 = Criteria.where("field.name").is("val")
Criteria projection = Criteria.where("field").is(1)
BasicQuery query = new BasicQuery(c1.getCriteriaObject(), projection.getCriteriaObject())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top