JPQL按地图值排序?

时间:2015-07-23 18:47:00

标签: jpa eclipselink jpql

使用JPA对象中的集合,如:

   @ElementCollection
   private Map<String, Double> fitValue = new HashMap<String, Double>();

是否有JPQL语法允许我order by特定Map索引的值?

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)

解析得很好,但却试图填写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)

关闭但不完全。

感谢。

1 个答案:

答案 0 :(得分:0)

这是使用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而不是整个对象。