How to do group by substring in Eclipselink?

醉酒当歌 提交于 2019-12-25 05:51:17

问题


I'm trying to group the store by store country and see how many stores there are in each country. The store country happens to be the first 2 characters of the store's code. That means if the store code is "US000010", the country is "US"

My entity object:

...
public class Store {
    @column(name = "code")
    private String code;
    ...    
}

My JPQL is:

  SELECT substring(s.code, 0, 2),
         count(s) 
    FROM Store s 
GROUP BY substring(s.code, 0, 2)

This keeps throwing me ORA-00979: not a GROUP BY expression, which I suspect is due to the JPQL above that is converted to

SELECT SUBSTR(CODE, ?, ?), COUNT(CODE) FROM Store GROUP BY SUBSTR(CODE, ?, ?)

Excluding other alternatives like creating a view or using native query, is there any way I perform a group by substring in Eclipselink using JPQL?


回答1:


I recently had a similar problem grouping by a TO_CHAR function. The solution I found is ExclipseLink specific, and it was just to set a hint on the query: query.setHint(QueryHints.BIND_PARAMETERS, HintValues.FALSE);.

Put this code before executing the query and it will not use parameters, but replace the values directly on the SQL string sent to the JDBC driver



来源:https://stackoverflow.com/questions/34433648/how-to-do-group-by-substring-in-eclipselink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!