Java 如何在Spring数据JPA查询中按多列排序,并按顺序计算?
我正在尝试将以下查询添加到JPA repository@queryJava 如何在Spring数据JPA查询中按多列排序,并按顺序计算?,java,spring,jpa,spring-data-jpa,Java,Spring,Jpa,Spring Data Jpa,我正在尝试将以下查询添加到JPA repository@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
"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
启动bean初始化时引发的错误
原因:org.apache.openjpa.persistence.ArgumentException:“遇到”(“在
在@Query中有什么方法可以实现这一点吗?在您的策略对象中,您可以添加一个getter:
public double getMarginRatio() {
return (this.totalMargin / this.totalRevenue);
}
然后,您的order by语句将变为“order by st.marginRatio
”Yes with nativeQuery
@Query(value = "select st from STRATEGY st where st.unId= ?1 order by ( st.totalMargin / st.totalRevenue ) desc", nativeQuery = true)
请把你的代码放好这不是真正的Spring数据JPA相关,打开JPA会被阻塞…查询会像现在一样传递给你的JPA提供者。但是我想知道这在JPA中是否可能…@nitindadriyal-尝试使用两列之间的除法(x/y)按值排序。是的,我知道,但我认为您的查询也将在没有括号的情况下运行。不,它不会。在这种情况下,它无法解析“/”符号。感谢Tristan,我尝试了此操作。这不起作用。它需要一个现有列名。