crudrepository findBy method signature for list of tuples

前端 未结 2 1417
日久生厌
日久生厌 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<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]));
        }
    }
    

    Your repo is supposed to extend interface JpaSpecificationExecutor<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.

    0 讨论(0)
  • 2020-12-21 02:25

    It is maybe cleaner using a projection :

    @Entity
    @Table(name = "CUSTOMER")
    class CustomerQueryData {
        @Id
        @Column(name = "Id")
        Long id;
        @OneToOne
        @JoinColumns(@JoinColumn(name = "emailId"), @JoinColumn(name = "mobile"))
        Contact contact;
    }
    

    The Contact Entity :

    @Entity
    @Table(name = "CUSTOMER")
    class Contact{
        @Column(name = "EMAIL_ID")
        String emailId;
        @Column(name = "MOBILE")
        String mobile;
    }
    

    After specifying the entities, the repo :

    CustomerJpaProjection extends Repository<CustomerQueryData, Long>, QueryDslPredicateExecutor<CustomerQueryData> {
        @Override
        List<CustomerQueryData> findAll(Predicate predicate);
     }
    

    And the repo call :

    ArrayList<Contact> contacts = new ArrayList<>();
    contacts.add(new Contact("a@b.c","8971"));
    contacts.add(new Contact("e@f.g", "8888"));
    customerJpaProjection.findAll(QCustomerQueryData.customerQueryData.contact.in(contacts));
    

    Not tested code.

    0 讨论(0)
提交回复
热议问题