Java CrudRepository:通过多个相关实体查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24146830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:28:44  来源:igfitidea点击:

CrudRepository: find by multiple related entities

javasqlspringhibernatespring-data

提问by Christopher

I'm having some trouble designing a query in a CrudRepository.

我在 CrudRepository 中设计查询时遇到了一些麻烦。

I have two entities, CourseOffering and Department (only relevant code shown):

我有两个实体,CourseOffering 和 Department(仅显示相关代码):

CourseOffering.java:

CourseOffering.java:

public class CourseOffering implements Serializable
{
    private Department department;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JoinColumn(name = "DepartmentId", nullable = true)
    @JsonProperty
    public Department getDepartment()
    {
        return this.department;
    }

    public void setDepartment(Department department)
    {
        this.department = department;
    }
}

Department.java:

部门.java:

public class Department implements Serializable
{
    private Set<CourseOffering> courses;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "department")
    public Set<CourseOffering> getCourses() {
        return this.courses;
    }

    public void setCourses(Set<CourseOffering> courses) {
        this.courses = courses;
    }
}

and the CrudRepository in question:

和有问题的 CrudRepository:

CourseOfferingRepository.java:

CourseOfferingRepository.java:

import java.util.List;
import edu.ucdavis.dss.dw.entities.CourseOffering;
import org.springframework.data.repository.CrudRepository;

public interface CourseOfferingRepository extends CrudRepository<CourseOffering, Long>
{
    CourseOffering getOneByTermIdAndNumberAndDepartmentId(long termId, String number,
            long departmentId);

    List<CourseOffering> findByDepartmentCode(String deptCode);

    //List<CourseOffering> findAllByDepartmentCode(String deptCodes);

    List<CourseOffering> findByTermCode(String termCode);
}

The three functions in CourseOfferingRepository which are not commented out work as expected. I am trying to get the fourth to work.

CourseOfferingRepository 中未注释掉的三个函数按预期工作。我正在努力让第四个工作。

What I'd like to do is be able to return all CourseOfferings where the department code is one of many department codes. Note that the CourseOffering table itself only holds a department_id integer which references the ID in the Department table, where the actual deptCode is stored.

我想要做的是能够返回所有部门代码是许多部门代码之一的CourseOfferings。请注意,CourseOffering 表本身仅包含一个 Department_id 整数,该整数引用了 Department 表中的 ID,其中存储了实际的 deptCode。

How would I go about getting that commented out CrudRepository function to work properly? Or put another way, how does one make the plural version of "List findByDepartmentCode(String deptCode);"?

我将如何让注释掉的 CrudRepository 函数正常工作?或者换句话说,如何制作“List findByDepartmentCode(String departmentCode);”的复数版本?

Thanks in advance for any advice you can offer.

预先感谢您提供的任何建议。

采纳答案by geoand

You need to change the commented out code to:

您需要将注释掉的代码更改为:

List<CourseOffering> findByDeptCodeIn(Collection<String> deptCodes)

List<CourseOffering> findByDeptCodeIn(Collection<String> deptCodes)

Check out thispart of the documentation to see what other keywords are allowed

查看文档的这一部分以了解允许使用哪些其他关键字

回答by Christopher

As geoand pointed out in the comments, the answer is:

正如 geoand 在评论中指出的那样,答案是:

List<CourseOffering> findByDepartmentCodeIn(List<String> deptCodes);

Thanks geoand!

谢谢geoand!