CrudRepository: find by multiple related entities

12,518

Solution 1

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

Solution 2

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

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

Thanks geoand!

Share:
12,518
Christopher
Author by

Christopher

Updated on June 05, 2022

Comments

  • Christopher
    Christopher almost 2 years

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

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

    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:

    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:

    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.

    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.

    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);"?

    Thanks in advance for any advice you can offer.

    • geoand
      geoand almost 10 years
      Does findByDeptCodeIn(Collection<String> deptCodes) do what you want?
    • Christopher
      Christopher almost 10 years
      Yes, thank you! Didn't know about the 'In' bit there at the end.
    • geoand
      geoand almost 10 years
      I added the comment as an answer :)
  • PhoneixS
    PhoneixS about 8 years
    Can this be also used when you only have the relation in Department class? (I mean without having department attribute in CourseOffering class).
  • geoand
    geoand about 8 years
    @PhoneixS I haven't tried it, but I seriously doubt that it would work