Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic sorting in QueryDSL

Tags:

jpa

querydsl

Lets say I have an Entity:

@Entity
public class Person {
    @Id
    private int id;
    private String name;
    private Date birthdate;
}

and I would like to have method which will return OrderSpecifier for a field of this entity based on String parameter which will be name of one of entities fields.

/**
 * fieldName - name of field from Person entity
 */
private OrderSpecifier<?> getSortedColumn(Order order, String fieldName){
    //how to implement this???        
}
like image 253
Tomasz Mularczyk Avatar asked Sep 19 '16 14:09

Tomasz Mularczyk


1 Answers

I actualy managed to do it like this:

/**
 * fieldName - name of field from Person entity
 */
private OrderSpecifier<?> getSortedColumn(Order order, String fieldName){
    Path<Object> fieldPath = Expressions.path(Object.class, QPerson.person, fieldName);     
    return new OrderSpecifier(order, fieldPath);
}
like image 200
Tomasz Mularczyk Avatar answered Nov 06 '22 00:11

Tomasz Mularczyk