Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QueryDsl orderBy column name

I just started to use QueryDSL and ran into a problem. Is it possible to do the orderBy using the column name? I found this for orderBy dynamic path generation:

Generic querydsl orderBy dynamic path generation with left joins

which is great, but in my case the GUI already sends column names for ordering. For example, "USER_ID" is the column name, and the property is "userid"

@Entity
@Table(name="USER")
public class User implements java.io.Serializable {

private String userid;   

@Id     
@Column(name="USER_ID", unique=true, nullable=false, length=18)
public String getUserid() {
    return this.userid;
}

}

Extracting property name from column name in hibernate is the only solution I can think of.

I'd appreciate any suggestions.

like image 450
Marko Avatar asked Nov 01 '22 02:11

Marko


1 Answers

You can use PathBuilder for dynamic path construction

PathBuilder<User> pbu = new PathBuilder<>(User.class, "user");
query.orderBy(pbu.getString(orderProperty).asc());
like image 119
Timo Westkämper Avatar answered Nov 15 '22 09:11

Timo Westkämper