0

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;
    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jaydeep
  • 17
  • 4
  • error is for Sysout line – Jaydeep Jul 18 '22 at 15:05
  • Your request `select * from user_dao where id =:id` have a `user` for response, not a `boolean`, you must adapt you request to make it select a `boolean`. – Zorglube Jul 18 '22 at 15:09
  • the query's response is a entity and you indicate is a boolean, you need change boolean to entity(your dao), or change your query to return a boolean value. – AbrahamSantos Jul 18 '22 at 15:28

1 Answers1

0

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

Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197
  • yes ty actually i changed the code that our teacher gave us where he had actually passed a query which would return a boolean value , i will now try using existsById method cause he never explored on that – Jaydeep Jul 21 '22 at 08:09