spring-data-jpa Jpa Sping Boot 错误->无法将对象转换为布尔类型

vtwuwzda  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(175)

你好实际的错误是:

Exception in thread "main" org.springframework.core.convert.ConversionFailedException: 

Failed to convert from type [java.lang.Object[]] to type [boolean] for value '{2, ramesh, pass, 12345, ramu}'; 

nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [boolean]

这里我尝试创建一个方法来通过id查找用户,但是当我尝试将值放入boolean类型时,它给出了上述错误

@Query("select case when count(s)>0 then true else false end from user_dao s where s.id =:id ")

@Query(value = "select * from user_dao where id =:id ", nativeQuery = true)
boolean isStudentExistsById(@Param("id") Integer id);

main方法-〉中,这应该输出true或false。

System.out.println(userRepo.isStudentExistsById(2));

在Bean的构造器中

UserDao(int id, String name, String phone, String user_name, String 
    password) {

        this.id = id;
        this.name = name;
        this.phone = phone;
        this.user_name = user_name;
        this.password = password;
    }
rn0zuynd

rn0zuynd1#

最简单的方法--使用CrudRepository的方法(你的仓库通常继承了它):
boolean existsById(Integer id)
有关其他选项,请参见Spring Data JPA and Exists query

相关问题