如何在Spring数据中按多列排序JPA查询按顺序计算?

时间:2015-05-27 13:13:28

标签: java spring jpa spring-data-jpa

我正在尝试将以下查询添加到JPA存储库@Query

"select st from Strategy st where st.unId= ?1 order by ( st.totalMargin / st.totalRevenue ) desc"

但是,由于"("括号在查询中),此查询不会解析。

The error thrown while bean initialization is 
Caused by: <openjpa-2.2.2-r422266:1468616 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: "Encountered "(" at 

在@Query中有什么办法可以实现吗?

2 个答案:

答案 0 :(得分:0)

在您的策略对象中,您可以添加一个getter:

public double getMarginRatio() {
  return (this.totalMargin / this.totalRevenue);
}

然后,您的订单语句将变为&#34; order by st.marginRatio&#34;

答案 1 :(得分:0)

是的,使用nativeQuery

@Query(value = "select st from STRATEGY st where st.unId= ?1 order by ( st.totalMargin / st.totalRevenue ) desc", nativeQuery = true)