简体   繁体   English

CrudRepository:由多个相关实体查找

[英]CrudRepository: find by multiple related entities

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: Department.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. CourseOfferingRepository中未注释掉的三个函数按预期工作。 I am trying to get the fourth to work. 我想让第四个工作。

What I'd like to do is be able to return all CourseOfferings where the department code is one of many department codes. 我想做的是能够返回所有CourseOfferings,其中部门代码是许多部门代码之一。 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. 请注意,CourseOffering表本身只包含一个department_id整数,该整数引用Department表中的ID,其中存储了实际的deptCode。

How would I go about getting that commented out CrudRepository function to work properly? 我如何才能将注释掉的CrudRepository函数正常工作? Or put another way, how does one make the plural version of "List findByDepartmentCode(String deptCode);"? 或换句话说,如何制作复数版本的“List findByDepartmentCode(String deptCode);”?

Thanks in advance for any advice you can offer. 提前感谢您提供的任何建议。

You need to change the commented out code to: 您需要将注释掉的代码更改为:

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

Check out this part of the documentation to see what other keywords are allowed 查看文档的这一部分,了解允许的其他关键字

As geoand pointed out in the comments, the answer is: 正如geoand在评论中指出的那样,答案是:

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

Thanks geoand! 谢谢地理!

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

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