QueryDSL中的动态排序

时间:2016-09-19 14:57:07

标签: jpa querydsl

假设我有一个实体:

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

我希望有一个方法,它会根据OrderSpecifier参数为该实体的字段返回String,该参数将是某个实体字段的名称。

/**
 * fieldName - name of field from Person entity
 */
private OrderSpecifier<?> getSortedColumn(Order order, String fieldName){
    //how to implement this???        
}

1 个答案:

答案 0 :(得分:5)

我实际成功地这样做了:

/**
 * 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);
}