Question

I have the following QueryDSL query:

List<DTOPerson> names = query.from(p)
  .where(p.lastname.like("%smith%"))
  .orderBy(p.lastname.asc(), p.firstname.asc())
  .list(Projections.bean(DTOPerson.class, p.lastname, p.firstname));

What I would like to have is this:

List<DTOPerson> names = query.from(p)
  .where(p.lastname.like("%smith%"))
  .orderBy(p.lastname.asc(), p.firstname.asc())
  .list(Projections.bean(DTOPerson.class, p.lastname.trim(), p.firstname.trim()));

But when I do that, QueryDSL gives me this error:

java.lang.IllegalArgumentException: Unsupported expression trim(p.lastname)
  at com.mysema.query.types.QBean.createBindings(QBean.java:59)
  at com.mysema.query.types.QBean.<init>(QBean.java:149)
  at com.mysema.query.types.QBean.<init>(QBean.java:138)
  at com.mysema.query.types.Projections.bean(Projections.java:51)    

Is there a way to trim (all) string results?

Was it helpful?

Solution

You will need to use explicit aliases in this case, because Querydsl accepts only paths and alias-operations as arguments to QBean.

List<DTOPerson> names = query.from(p)
  .where(p.lastname.like("%smith%"))
  .orderBy(p.lastname.asc(), p.firstname.asc())
  .list(Projections.bean(DTOPerson.class, 
      p.lastname.trim().as("lastname"), 
      p.firstname.trim().as("firstname")));

Is there a way to trim (all) string results?

You can create a custom FactoryExpression, e.g. subclass of QBean, to do that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top