繁体   English   中英

Spring 启动 - 未找到用户类型的属性 ID

[英]Spring boot - No property ID found for type User

好吧,我搜索了谷歌,发现了很多结果,但没有一个能够回答我的问题。 所以,就这样吧。

我正在尝试通过执行 pinterest 克隆的最小实现来研究 Spring MVC 和 Spring 数据 JPA。 因此,以下是我认为与我的问题相关的代码部分。

存储库 Class:

public interface CourseStudentRepository extends JpaRepository <CourseStudent, Long> {

    List<CourseStudent> findByCourseInstructorId(Long instructorId);

    List<CourseStudent> findByStudentID (Long studentId);
}

实体 class:

@Data
@Entity
@Table(name = "course_student")
public class CourseStudent implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "student_id", referencedColumnName = "id")
    private User student;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "course_id", referencedColumnName = "id")
    private Course course;

}

错误:

Failed to create query for method public abstract java.util.List com.sha.serverside.repository.CourseStudentRepository.findByStudentID(java.lang.Long)! No property ID found for type User! Did you mean 'id'? Traversed path: CourseStudent.student.

错误说没有ID字段是Student class 但您在存储库中使用List<CourseStudent> findByStudentID 这里的字段名区分大小写, IDId不一样。

使用findByStudentId而不是findByStudentID

List<CourseStudent> findByStudentId (Long studentId);

参考: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

处理好这个案子。 名称必须是findByStudentId 小写d结尾

暂无
暂无

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

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