简体   繁体   中英

How to properly use findBySomeOtherId not findById in Spring data jpa?

I am working on a Spring boot project with spring data jpa(MYSQL) When I hit this end point http://localhost:8080/admin/interviews/101 I get this error

{
    "timestamp": "2019-10-11T13:45:37.111+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Parameter value [101] did not match expected type [com.yash.arci.model.Interviews (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [101] did not match expected type [com.yash.arci.model.Interviews (n/a)]",

    "trace": "org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [101] did not match expected type [com.yash.arci.model.Interviews (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [101] did not match expected type [com.yash.arci.model.Interviews (n/a)]\r\n\tat org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible

This is interviewStatus class

@Entity
@Table(name ="interview_status" )
public class InterviewStatus implements Serializable {


private static final long serialVersionUID = -6353284727527331855L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name ="interviewId",insertable = false, updatable = false)
private Interviews interviewId;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name ="statusId",insertable = false, updatable = false)
private Status statusId;
..........
}



@Repository
public interface InterviewStatusRepository extends 
JpaRepository<InterviewStatus, Integer> {

InterviewStatus findByInterviewId(Integer interviewId);
}

I want the record on the basis of interviewId. I can already get on the basis of primary key but I want it on the basis of Interview Id, I have interviewId column in interviewStatus table.

Please use this InterviewStatus findByInterviewId(Interviews interviewId); where interviewId is gotten by running Interviews findById(Long id) .

Due to the datatype conflict, it is ok that you pass in as parameter the expected datatype. so it is expecting Interviews not Integer, but you have integer. in this case, you get Interviews using the integer then pass the gotten Interviews as parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM