我有一个使用JpaRepository
的查询
@Query(
value = "SELECT count(user_name) AS user_count " +
"FROM users " +
"where status = 'B' ",
nativeQuery = true
)
List<Users> usersStatCount();
它给了我一个错误,
Could not execute query...
The column name user_name was not found in result set
但是当我在 pgadmin 上尝试查询时,它工作正常。
当我尝试一个简单的
@Query(
value = "SELECT * " +
"FROM users " +
"where status = 'B' ",
nativeQuery = true
)
List<Users> usersStatCount();
它也工作正常。
最佳答案
这是因为查询返回一个值,而不是一行,它必须映射到实体。请尝试以下操作:
@Query(
value = "SELECT count(user_name) " +
"FROM users " +
"where status = 'B' ",
nativeQuery = true
)
Long usersStatCount();
关于java - 在结果集中找不到列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60859736/