问题
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