column not found in result set

Beginner :

I have a query that uses JpaRepository

@Query(
        value = "SELECT count(user_name) AS user_count " +
                "FROM users " +
                "where status = 'B' ",
        nativeQuery = true
)
List<Users> usersStatCount();

It gives me an error,

Could not execute query...
The column name user_name was not found in result set

But when I tried the query on pgadmin it is working fine.

And when I tried a simple

@Query(
        value = "SELECT * " +
                "FROM users " +
                "where status = 'B' ",
        nativeQuery = true
)
List<Users> usersStatCount();

It is working fine as well.

Andronicus :

It's because the query returns a single value, not a row, that has to mapped to entity. Try the following:

@Query(
        value = "SELECT count(user_name) " +
                "FROM users " +
                "where status = 'B' ",
        nativeQuery = true
)
Long usersStatCount();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360712&siteId=1