JPQL 按地图值排序?
JPQL order by on map value?
使用 JPA 对象中的集合,例如:
@ElementCollection
private Map<String, Double> fitValue = new HashMap<String, Double>();
是否有 JPQL 语法可以让我 order by
特定地图索引的值?
SELECT o.individual.id FROM IndividualJob AS o JOIN o.fitValue f WHERE o.job.id = :id AND o.job.dtype = :type AND KEY(f) = '/' ORDER BY VALUE(f)
解析正常但抛出 NPE 试图填写 order by
:
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.mappings.ForeignReferenceMapping.getOrderByNormalizedExpressions(ForeignReferenceMapping.java:2505)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalizeOrderBy(SQLSelectStatement.java:1639)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1428)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildReportQuerySelectStatement(ExpressionQueryMechanism.java:642)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildReportQuerySelectStatement(ExpressionQueryMechanism.java:587)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareReportQuerySelectAllRows(ExpressionQueryMechanism.java:1696)
at org.eclipse.persistence.queries.ReportQuery.prepareSelectAllRows(ReportQuery.java:1207)
at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:798)
at org.eclipse.persistence.queries.ReportQuery.prepare(ReportQuery.java:1075)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:666)
接近但不完全。
谢谢。
这是使用 nativeQuery 解决问题的一种方法:
"select x.id from (select individual_id id, fitvalue fit from individualjob i join individualjob_fitvalue f on i.id = f.individualjob_id and i.job_id = ? and f.fitvalue_key = '/') as x order by x.fit desc"
幸运的是我只需要 ID 而不是整个对象。
使用 JPA 对象中的集合,例如:
@ElementCollection
private Map<String, Double> fitValue = new HashMap<String, Double>();
是否有 JPQL 语法可以让我 order by
特定地图索引的值?
SELECT o.individual.id FROM IndividualJob AS o JOIN o.fitValue f WHERE o.job.id = :id AND o.job.dtype = :type AND KEY(f) = '/' ORDER BY VALUE(f)
解析正常但抛出 NPE 试图填写 order by
:
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.mappings.ForeignReferenceMapping.getOrderByNormalizedExpressions(ForeignReferenceMapping.java:2505)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalizeOrderBy(SQLSelectStatement.java:1639)
at org.eclipse.persistence.internal.expressions.SQLSelectStatement.normalize(SQLSelectStatement.java:1428)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildReportQuerySelectStatement(ExpressionQueryMechanism.java:642)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.buildReportQuerySelectStatement(ExpressionQueryMechanism.java:587)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareReportQuerySelectAllRows(ExpressionQueryMechanism.java:1696)
at org.eclipse.persistence.queries.ReportQuery.prepareSelectAllRows(ReportQuery.java:1207)
at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:798)
at org.eclipse.persistence.queries.ReportQuery.prepare(ReportQuery.java:1075)
at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:666)
接近但不完全。
谢谢。
这是使用 nativeQuery 解决问题的一种方法:
"select x.id from (select individual_id id, fitvalue fit from individualjob i join individualjob_fitvalue f on i.id = f.individualjob_id and i.job_id = ? and f.fitvalue_key = '/') as x order by x.fit desc"
幸运的是我只需要 ID 而不是整个对象。