在JPA查询上映射pojo

Supposed I have a query on jpa like

@Query(
        value = "SELECT user_no, count(*) " +
                "FROM users " +
                "where status = 'VCS' group by user_task_no",
        nativeQuery = true
)
List<Object> getUsers();

Now I can get the result like

[
    [
        100,
        2
    ],
    [
        200,
        2
    ],
    [
        300,
        3
    ]
]

However, I want the result to be

[
    {
        "user_no": 100,
        "count": 2
    },
    {
        "user_no": 200,
        "count": 2
    },
    {
        "user_no": 300,
        "count": 3
    }
]

I tried to make a class like

public class UserCount {
    private Long userNo;
    private Long count;

    public UserCount(Long userNo, Long count) {
        this.userNo = userNo;
        this.count = count;
    }
}

and make it

@Query(
        value = "SELECT new fullpackage.UserCount(user_no, count(*)) " +
                "FROM users " +
                "where status = 'VCS' group by user_task_no",
        nativeQuery = true
)
List<UserCount> getUsers();

However, I'm getting an error

syntax error at or near "." ...
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

any idea on how can I fix this?


you can use projections baeldung.com/spring-data-jpa-projections

yeah i already used that and fixed