简体   繁体   中英

Order By in JPQL query on a @ElementCollection annotated map

In my @Entity annotated Course class, I have the following @ElementCollection annotated map:

@ElementCollection
private Map<Student, Double> courseStudAndAvgStudGrade;

In the example above, Student is another @Entity annotated class and the value is the average grade from a Course for each Student . I'm trying to write a query in JPQL that would retrieve all the entries of this map and sort by entry value in descending order.

So far, I have the following:

TypedQuery<Tuple> query =
              em.createQuery("SELECT KEY(map), VALUE(map) "
                      + "FROM Course c JOIN c.courseStudAndAvgStudGrade map WHERE c.id = :id", Tuple.class);

This retrieves the values correctly in a Tuple for the desired Course , however adding ORDER BY VALUE(map) DESC to the JPQL query results in a java.sql.SQLException: Subquery returns more than 1 row .

Is it possible to do ORDER BY on a map in JPQL?

JPA/Hibernate does not require the VALUE qualifier ie it's actually just there for completeness. Try the following query:

em.createQuery("SELECT KEY(map), map FROM Course c JOIN c.courseStudAndAvgStudGrade map WHERE c.id = :id ORDER BY map DESC", Tuple.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM