动态JPQL查询qith JOIN

时间:2017-02-26 10:29:31

标签: jpa spring-boot jpql

不得不写一个jpql查询,基于输入需要添加和条件而且对于某些输入必须需要JOIN查询。

@Override
        public List<IncidentHdr> fetchIncidents(IncidentHdrDto incidentHdrDto) {

            StringBuilder query = new StringBuilder();
            query.append(ReposJPQL.GET_INCIDENT_DETAILS);

            Map<String, Object> parameters = new HashMap<String, Object>();
            List<String> criteria = new ArrayList<String>();

            if(incidentHdrDto.getIncidentId() > 0) {
                criteria.add("inc.incidentId = :incidentId");
                parameters.put("incidentId", incidentHdrDto.getIncidentId());
            }

            if(incidentHdrDto.getCatCode() > 0) {
                criteria.add("inc.catCode = :catCode");
                parameters.put("catCode", incidentHdrDto.getCatCode());
            }

            if(incidentHdrDto.getType != null) {
                //here i need to generate a join query
                //SELECT * FROM INCIDENT JOIN CATEGORY_MAST ON(INCIDENT.CAT_CODE = CATEGORY_MAST.CAT_CODE) WHERE CATEGORY_MAST.TYPE_CODE = 16
            }



            Query q = em.createQuery(query.toString());
            logger.info("Get Incidents Query : "+query.toString());

            for (Entry<String, Object> entry : parameters.entrySet()) {
                q.setParameter(entry.getKey(), entry.getValue());
            }



            List<IncidentHdr> incidentHdrs = q.getResultList();
            return incidentHdrs;
        }

其中ReposJPQL是具有where条件的基本查询。

public interface ReposJPQL {

    public String GET_INCIDENT_DETAILS = "SELECT inc FROM IncidentHdr inc WHERE 1 = 1" ;

}

0 个答案:

没有答案