crudrepository findBy元组列表的方法签名 [英] crudrepository findBy method signature for list of tuples

查看:986
本文介绍了crudrepository findBy元组列表的方法签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的实体类:

@Entity
@Table(name = "CUSTOMER")
class Customer{
    @Id
    @Column(name = "Id")
    Long id;
    @Column(name = "EMAIL_ID")
    String emailId;
    @Column(name = "MOBILE")
    String mobile;
}

如何使用crudrepository spring data jpa为以下查询编写findBy方法?

How to write findBy method for the below query using crudrepository spring data jpa?

select * from customer where (email, mobile) IN (("a@b.c","8971"), ("e@f.g", "8888"))

我期待像

List<Customer> findByEmailMobileIn(List<Tuple> tuples);

我想从给定的对中获取客户列表

I want to get the list of customers from given pairs

推荐答案

我认为这可以通过 org.springframework.data.jpa.domain.Specification 来完成。您可以传递元组列表并以这种方式继续(不要关心元组不是实体,但是您需要定义此类):

I think this can be done with org.springframework.data.jpa.domain.Specification. You can pass a list of your tuples and proceed them this way (don't care that Tuple is not an entity, but you need to define this class):

public class CustomerSpecification implements Specification<Customer> {

    // names of the fields in your Customer entity
    private static final String CONST_EMAIL_ID = "emailId";
    private static final String CONST_MOBILE = "mobile";

    private List<MyTuple> tuples;

    public ClaimSpecification(List<MyTuple> tuples) {
        this.tuples = tuples;
    }

    @Override
    public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
        // will be connected with logical OR
        List<Predicate> predicates = new ArrayList<>();

        tuples.forEach(tuple -> {
            List<Predicate> innerPredicates = new ArrayList<>();
            if (tuple.getEmail() != null) {
                 innerPredicates.add(cb.equal(root
                     .<String>get(CONST_EMAIL_ID), tuple.getEmail()));
            }
            if (tuple.getMobile() != null) {
                 innerPredicates.add(cb.equal(root
                     .<String>get(CONST_MOBILE), tuple.getMobile()));
            }
            // these predicates match a tuple, hence joined with AND
            predicates.add(andTogether(innerPredicates, cb));
        });

        return orTogether(predicates, cb);
    }

    private Predicate orTogether(List<Predicate> predicates, CriteriaBuilder cb) {
        return cb.or(predicates.toArray(new Predicate[0]));
    }

    private Predicate andTogether(List<Predicate> predicates, CriteriaBuilder cb) {
        return cb.and(predicates.toArray(new Predicate[0]));
    }
}

你的回购应该扩展接口 JpaSpecificationExecutor< Customer>

Your repo is supposed to extend interface JpaSpecificationExecutor<Customer>.

然后构造一个带有元组列表的规范并将其传递给方法 customerRepo.findAll(Specification< Customer>) - 它返回一个客户列表。

Then construct a specification with a list of tuples and pass it to the method customerRepo.findAll(Specification<Customer>) - it returns a list of customers.

这篇关于crudrepository findBy元组列表的方法签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆