1

I have query like that:

    @Query(value = "SELECT new com.domain.ActivityStatistic( " +
        "adm.id, " +
        "adm.fullName, " +
        "COUNT(CASE WHEN (act.action = 'APPROVE') THEN act.action END) AS approved, " +
        "max(act.actionTime) AS lastActionTime) " +
        "FROM Actions act, Admins adm LEFT JOIN adm.group gr " +
        "WHERE adm.id = act.adminId AND act.actionTime BETWEEN ?1 AND ?2 AND gr.id = ?3 " +
        "GROUP BY adm.id")
Page<ActivityStatistic> getActivityStatistics(LocalDateTime from,
                                                              LocalDateTime to,
                                                              long groupId,
                                                              Pageable pageable);

How can I sort it by the new field that I created: lastActionTime, approved ? I can run it by native sql in postgresql: pgadmin. But in jpa, when I using sort with field name is approved, it auto become act.approved in JPA query.

I used to read this post Spring Data and how to sort by a column not in an Entity but it not help.

1
  • I resolved this problem by using native query, and create new Entity (without create a table) with property: approved, lastActionTime. And to sort with pageable I using table name instead of table alias name (ex: using admins instead of adm. And it work. Commented Jun 28, 2018 at 4:03

1 Answer 1

1

You can't apply in JPQL on a table column that isn't mapped to a property of an entity. The reason for this is that JPA including JPQL operates on these entities.

Use a native query instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.