简体   繁体   English

如何在 Spring 数据 jpa 中正确使用 findBySomeOtherId not findById?

[英]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我正在使用 spring 数据 jpa(MYSQL) 处理 Spring 引导项目,当我到达这个终点时http://localhost:8080/admin/interviews/101

{
    "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这是采访状态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.我想要基于 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.我已经可以在主键的基础上得到它,但我想要它基于面试 ID,我在 interviewStatus 表中有 interviewId 列。

Please use this InterviewStatus findByInterviewId(Interviews interviewId);请使用这个InterviewStatus findByInterviewId(Interviews interviewId); where interviewId is gotten by running Interviews findById(Long id) .通过运行Interviews findById(Long id)获得 interviewId。

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.所以期待采访不是 Integer,但你有 integer。 in this case, you get Interviews using the integer then pass the gotten Interviews as parameter.在这种情况下,您使用 integer 获得采访,然后将获得的采访作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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