简体   繁体   English

Spring数据,通过嵌套对象的属性查找

[英]Spring data, find by property of a nested object

I have the following code: 我有以下代码:

@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;
}

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. 我已经设置了一个spring数据mongodb存储库,我正在尝试创建一个方法,它将基于我作为参数传入的className返回List<Student> Based on everything I've read, I assumed that the following would work: 根据我读过的所有内容,我认为以下内容可行:

public List<Student> findByStudentGradesClassName(final String className);

but that gives an error saying that the parameter type should be a Grade object. 但是这给出了一个错误,说参数类型应该是Grade对象。 I really only want to pass in a String className . 我真的只想传入一个String className

Is this possible? 这可能吗?

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. 请注意,您仍然必须使用大写字母启动字段名称。

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

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