在Jpa repo中使用@Query注释构造查询

时间:2017-09-19 14:01:11

标签: spring-mvc jpa spring-data

这是我查询的逻辑:

String customQuery = "";
customQuery = customQuery + "select * from Customer c where";

if (targetGroupDto.getGender() != null && !targetGroupDto.getGender().isEmpty()) {
    customQuery = customQuery + " c.gender = " + targetGroupDto.getGender();
    if (targetGroupDto.getAge() != 0
        || (targetGroupDto.getCity() != null && !targetGroupDto.getCity().isEmpty())
        || (targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty())) {
        customQuery = customQuery + " and ";
    }
}

if (targetGroupDto.getAge() != 0) {
    customQuery = customQuery + " c.age = " + targetGroupDto.getAge();
    if ((targetGroupDto.getCity() != null && !targetGroupDto.getCity().isEmpty())
        || (targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty())) {
        customQuery = customQuery + " and ";
    }
}

if (targetGroupDto.getCity() != null && !targetGroupDto.getCity().isEmpty()) {
    customQuery = customQuery + " c.city = " + targetGroupDto.getCity();
    if ((targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty())) {
        customQuery = customQuery + " and ";
    }
}

if (targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty()) {
    customQuery = customQuery + " c.state = " + targetGroupDto.getState();
}

**

  

这是我编写查询的类,我正在调用它   我的服务类中的方法

**

 @Transactional
public class CustomerRepositoryImpl implements CustomerRepository {

    @Autowired
    private EntityManager entityManager;

    @Override
    public Set<Customer> filterCustomers(TargetGroupDto targetGroupDto) {

        //CriteriaBuilder cb = entityManager.getCriteriaBuilder();

        //CriteriaQuery<Customer> criteriaQuery = cb.createQuery(Customer.class);
        //Root<Customer> customer = criteriaQuery.from(Customer.class);


        Query query;
        String customQuery = "";
        customQuery = customQuery + "select * from Customer c where";

        if (targetGroupDto.getGender() != null && !targetGroupDto.getGender().isEmpty()) {
            customQuery = customQuery + " c.gender = " + targetGroupDto.getGender();
            if (targetGroupDto.getAge() != 0
                    || (targetGroupDto.getCity() != null && !targetGroupDto.getCity().isEmpty())
                    || (targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty())) {
                customQuery = customQuery + " and ";
            }
        }

        if (targetGroupDto.getAge() != 0) {
            customQuery = customQuery + " c.age = " + targetGroupDto.getAge();
            if ((targetGroupDto.getCity() != null && !targetGroupDto.getCity().isEmpty())
                    || (targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty())) {
                customQuery = customQuery + " and ";
            }
        }
        if (targetGroupDto.getCity() != null && !targetGroupDto.getCity().isEmpty()) {
            customQuery = customQuery + " c.city = " + targetGroupDto.getCity();
            if ((targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty())) {
                customQuery = customQuery + " and ";
            }
        }

        if (targetGroupDto.getState() != null && !targetGroupDto.getState().isEmpty()) {
            customQuery = customQuery + " c.state = " + targetGroupDto.getState();
        }
        query = getEntityManager().createQuery(customQuery);
        List<Customer> customers = query.getResultList();
        Set<Customer> results = new HashSet<Customer>(customers);

        return results;
    }

**

  
    
      

我在调用此方法java.lang.NullPointerException时得到此Null Pointer Exception       com.thoughtclan.segmentationofcustomers.service.SocServiceImpl.filterDetails(SocServiceImpl.java:102)         在       com.thoughtclan.segmentationofcustomers.controller.SegmentationOfCustomersController.filterDetails(SegmentationOfCustomersController.java:73)

    
  
     

SegmentationOfCustomersController.java:73) - &GT;客户=   socService.filterDetails(targetGroupDto);

     

SocServiceImpl.java:102) - &GT; filteredCustomers =   customerRepositoryImpl.filterCustomers(targetGroupDto);

     
    

**

  

0 个答案:

没有答案