Search code examples
jpaeclipselinkjpql

JPQL order by on map value?


With a collection in a JPA object like:

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

Is there a JPQL syntax that lets me order by the value of a particular Map index?

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)

parses fine but throws a NPE trying to fill-in the 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)

Close but not quite.

Thanks.


Solution

  • This is one solution to the problem using a 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"
    

    Luckily I only needed the id's and not the entire object.