crudrepository findBy method signature for list of tuples

前端 未结 2 1441
日久生厌
日久生厌 2020-12-21 01:41

I have an Entity Class like this:

@Entity
@Table(name = \"CUSTOMER\")
class Customer{
    @Id
    @Column(name = \"Id\")
    Long id;
    @Column(name = \"EM         


        
2条回答
  •  感动是毒
    2020-12-21 02:21

    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 {
    
        // 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 tuples;
    
        public ClaimSpecification(List tuples) {
            this.tuples = tuples;
        }
    
        @Override
        public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
            // will be connected with logical OR
            List predicates = new ArrayList<>();
    
            tuples.forEach(tuple -> {
                List innerPredicates = new ArrayList<>();
                if (tuple.getEmail() != null) {
                     innerPredicates.add(cb.equal(root
                         .get(CONST_EMAIL_ID), tuple.getEmail()));
                }
                if (tuple.getMobile() != null) {
                     innerPredicates.add(cb.equal(root
                         .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 predicates, CriteriaBuilder cb) {
            return cb.or(predicates.toArray(new Predicate[0]));
        }
    
        private Predicate andTogether(List predicates, CriteriaBuilder cb) {
            return cb.and(predicates.toArray(new Predicate[0]));
        }
    }
    

    Your repo is supposed to extend interface JpaSpecificationExecutor.

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

提交回复
热议问题