Home > Software engineering >  In springBoot JPA findById() taking only id of integer type. I want to find it by emailID. Is there
In springBoot JPA findById() taking only id of integer type. I want to find it by emailID. Is there

Time:09-15

I am using SpringBootApplication with JPA to define Controller mappings and to perform CRUD operations. I want to fetch student details from database by emailId I have written SQL query in DAO but it returning sql row in String format How can I map to the StudentDetail object? findById taking only id of integer type. I want to find it by emailID. Is there any method like findByEmailId?

StudentDetail.java

@Table(name="studentpersonaldetails")
@Entity
public class StudentDetail {

    @Id
    @Column(name="emailid")
    String emailId;
    @Column(name="firstname")
    String firstName;
    @Column(name="lastname")
    String lastName;
    @Column(name="mobilenumber")
    String mobileNumber;
    String address;
    String batch;
    String course;
    String gender;
    String year;

    Paramaterised constructor, getters-setters &  toString method 
}

StudentController.java

@GetMapping("/getpersonaldetails/{emailId}")
    public StudentDetail getPersonalDetails(@PathVariable String emailId)
    {
        emailId = emailId.substring(1);
        return studentService.getPersonalDetails(emailId);      
    }

StudentService.java

public StudentDetail getPersonalDetails(String emailId) {   
        return studentDao.getPersonalDetails(emailId);
}

StudentDAO.java

@Query(value = "SELECT * FROM studentpersonaldetails WHERE emailId=?1", nativeQuery = true)
    StudentDetail getPersonalDetails(String emailId);

Error

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [com.cdac.hms.model.StudentDetail]

CodePudding user response:

Can you create a new object of type StudentDetail and build it up with the results from. Then return that

var results = studentService.getPersonalDetails(emailId); 
StudentDetail sd = new StudentDetail(results);
return sd; 

CodePudding user response:

the query is incorrect, you have to change the parameter.

try this code :

@Query(value = "SELECT * FROM studentpersonaldetails WHERE emailId= :emailId", nativeQuery = true)
    StudentDetail getPersonalDetails(String emailId);
  • Related