Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CrudRepository: find by multiple related entities

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.

like image 871
Christopher Avatar asked Jun 10 '14 16:06

Christopher


People also ask

How does hibernate fetch multiple entities by id?

You just need to define the class of the entities you want to fetch and provide a List or array of primary keys. Hibernate then uses default values provided by the database specific dialect you use in your application or you provide your own set of configuration data.

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

Which are the methods of CrudRepository interface?

The methods defined in the CrudRepository interface is as below. <S extends T> save(S entity) – Used to save a single entity at a time. Iterable<S> saveAll(Iterable<S> entities) – we can save multiple entities at a time. Optional<T> findById(ID id) – use to get entity basis of id.


2 Answers

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

like image 140
geoand Avatar answered Nov 06 '22 12:11

geoand


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

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

Thanks geoand!

like image 1
Christopher Avatar answered Nov 06 '22 14:11

Christopher