Home > Net >  Jpa Spring Boot error -> cannot convert object to type boolean
Jpa Spring Boot error -> cannot convert object to type boolean

Time:07-21

Hello the actual error is :

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]

Here i tried to create a method to find users by id , but when i tried to put the values into boolean type it gives above error

@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);

in main method -> this should print true or false.

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

in constructor of 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;
    }

CodePudding user response:

Easiest way - use method of CrudRepository (your repository inherited it usually):

boolean existsById(Integer id)

For other options see Spring Data JPA and Exists query

  • Related