春季启动-找不到用户类型的属性ID

时间:2020-05-01 14:14:55

标签: java spring spring-boot spring-data-jpa

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

我正在尝试通过最少地实现pinterest克隆来研究Spring MVC和Spring Data JPA。因此,以下是我认为与我的问题有关的代码部分。

存储库类:

public interface CourseStudentRepository extends JpaRepository <CourseStudent, Long> {

    List<CourseStudent> findByCourseInstructorId(Long instructorId);

    List<CourseStudent> findByStudentID (Long studentId);
}

实体类:

@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.

2 个答案:

答案 0 :(得分:1)

错误说没有ID类的Student字段,但是您在存储库中使用了List<CourseStudent> findByStudentID。此处的字段名称区分大小写,IDId不相同。

使用findByStudentId代替findByStudentID

List<CourseStudent> findByStudentId (Long studentId);

答案 1 :(得分:0)

照顾好案件。名称必须为findByStudentId。小写的d结尾