JPA createNativeQuery不返回结果列表

时间:2015-08-06 11:45:16

标签: jpa

我有一个表,当我查询如下时,我看到2个结果行

select avg(cast (ctc as DECIMAL(9,2))),
avg(cast(ectc as DECIMAL(9,2))),
experience from app.INFORMATION
where skills like '%Java%' group by experience

我将其转换为JPA,如下所示

public HashMap<String, List<String>> getSalaryAvg(String skill) {
    System.out.println("inside dao");
    HashMap<String, List<String>> map = new HashMap<String,List<String>>();
    Query q = em
            .createNativeQuery("select avg(cast (i.ctc as DECIMAL(9,2))),avg(cast(i.ectc as DECIMAL(9,2))),i.experience from INFORMATION i where i.skills like ?skill group by i.experience");
    q.setParameter("skill","%skill%");

    @SuppressWarnings("unchecked")
    List<Object[]> list = q.getResultList();

    for (Object[] result : list) {
        List<String> l=new ArrayList<String>();
        l.add(result[0].toString());
        l.add(result[1].toString());
        map.put(result[3].toString(), l);
        System.out.println(" results " + result[0].toString() + " "
                + result[1].toString());
    }

    return map;
}

但我没有看到任何结果。原生查询中是否存在语法错误?

1 个答案:

答案 0 :(得分:0)

谢谢克里斯!发现错误

Query q = em
        .createNativeQuery("select avg(cast (i.ctc as DECIMAL(9,2))),avg(cast(i.ectc as DECIMAL(9,2))),i.experience from INFORMATION i where i.skills like ?skill group by i.experience");
q.setParameter("skill","%"+skill+"%");

这解决了问题