Spring数据,通过嵌套对象的属性查找 [英] Spring data, find by property of a nested object
本文介绍了Spring数据,通过嵌套对象的属性查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
@Entity
public class StudentEntity {
@Id
private String id;
private Student student;
...
}
public class Student {
private String name;
private List<Grade> grades;
}
public class Grade {
private String className;
private String grade;
}
我已经建立了一个Spring数据mongodb存储库,并且我正在尝试创建一个方法,该方法将基于作为参数传入的className
返回一个List<Student>
.根据我读过的所有内容,我认为以下方法会起作用:
I've set up a spring data mongodb repository and I'm trying to create a method that will return me a List<Student>
based on a className
that I pass in as parameter. Based on everything I've read, I assumed that the following would work:
public List<Student> findByStudentGradesClassName(final String className);
,但是会出现错误,表明参数类型应为Grade
对象.我真的只想传递String className
.
but that gives an error saying that the parameter type should be a Grade
object. I really only want to pass in a String className
.
这可能吗?
推荐答案
您必须用下划线分隔嵌套字段:
You would have to separate nested fields with underscore:
public List<Student> findByStudent_Grades_ClassName(final String className);
请注意,您仍然必须以大写字母开头字段名称.
Note, that you still have to start field names with uppercase.
这篇关于Spring数据,通过嵌套对象的属性查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文