简体   繁体   English

Spring Data Jpa 多个 findBy 查询的一种方法

[英]Spring Data Jpa One method for multiple findBy Queries

I have this JobPosting class and a repository interface for this entity.我有这个 JobPosting 类和这个实体的存储库接口。 I wanna be able to search for all combinations of company, skills, title and city fields of JobPosting class.我希望能够搜索 JobPosting 类的公司、技能、职位和城市字段的所有组合。 The only way I know is creating a different method in repository interface .我知道的唯一方法是在存储库界面中创建不同的方法。 For instance for searching by city and title i need to create a method named findByCityIdAndTitleContaining(Long id,String title).例如,要按城市和标题搜索,我需要创建一个名为 findByCityIdAndTitleContaining(Long id,String title) 的方法。 For searching by skills and title i need to create a method named LfindBySkillsIdAndTitleContaining(Long id,String title).为了按技能和职称搜索,我需要创建一个名为 LfindBySkillsIdAndTitleContaining(Long id,String title) 的方法。 The problem is if i create different method for each possibility there will be too many methods.问题是如果我为每种可能性创建不同的方法,就会有太多的方法。 Is there any better way ?有没有更好的方法?

Entity (I didn't include get set methods here):实体(我在这里没有包括 get set 方法):

@Entity
public class JobPosting
{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    @ManyToOne
    private Company company;
    private Date publishedAt;
    private String title,description;
    private boolean isActive;
    @ManyToOne
    private City city;
    @ManyToMany
    @JoinTable(name="job_posting_skill",joinColumns=@JoinColumn(name="job_posting_id"),inverseJoinColumns=@JoinColumn(name="skill_id"))
    private Set<Skill> skills;
}

Repository:存储库:

package app.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import app.entity.JobPosting;

public interface JobPostingRepository extends JpaRepository<JobPosting,Long>
{
    Page<JobPosting> findAll(Pageable pageable);
    List<JobPosting> findByCompanyId(long companyId);
    List<JobPosting> findByTitleContaining(String title);
    List<JobPosting> findBySkillsId(Long id);
    List<JobPosting> findByCityId(Long id);
}

Spring Data provides JpaSpecificationExecutor which can fit your requirements. Spring Data 提供了 JpaSpecificationExecutor 可以满足您的要求。

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaSpecificationExecutor.html https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaSpecificationExecutor.html

List<T>     findAll(Specification<T> spec)
Returns all entities matching the given Specification.

A Specification has a predicate method:规范有一个谓词方法:

interface Specification<T> {
    Predicate toPredicate(Root<T> root, CriteriaQuery query, CriteriaBuilder cb);
}

It has a CriteriaBuilder, so in theory you will still need to define what exactly you need to match for, however you wont have to create multiple findByXYZ.它有一个 CriteriaBuilder,所以理论上你仍然需要定义你需要匹配的内容,但是你不必创建多个 findByXYZ。

To be able to use findAll(Specification), your repository needs to extend org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>:为了能够使用 findAll(Specification),您的存储库需要扩展org.springframework.data.jpa.repository.JpaSpecificationExecutor<T>:

Example of usage:用法示例:

https://www.baeldung.com/spring-data-criteria-queries https://www.baeldung.com/spring-data-criteria-queries

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

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