我正在试着写一个"existsBy“查询,但是不能让它工作。我知道JpaRepository中有一个existByID,但我需要通过属性student_id进行检查。我尝试过无数种写函数名的方法,但我似乎不能把它写对。
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private long student_id;
+other fields and getters and setters...
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
boolean existsByStudentid(Long student_id);
}
错误:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property student found for type Student!
发布于 2020-04-06 22:36:33
Spring Data使用下划线作为保留字符。我认为这样使用是不可能的。我认为没有其他选项可以重命名该变量。
因此,该字段的命名必须遵循以下约定
private long studentId;
(下划线可用于遍历嵌套属性:要解决此歧义,可以在方法名中使用_来手动定义遍历点。)
发布于 2020-04-06 21:34:12
您的属性名称为student_id
因此查询方法应该是
@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
boolean existsByStudent_id(Long student_id);
}
Btw:_ in属性名称在Java expect for constants中不是推荐的样式
https://stackoverflow.com/questions/61060970
复制相似问题