1
該代碼使用JPA2條件查詢返回一個空的結果列表:JPA2 CriteriaQuery中空的結果
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<VwReplPrevia> query = criteriaBuilder.createQuery(VwReplPrevia.class);
Root<VwReplPrevia> root = query.from(VwReplPrevia.class);
query.multiselect(root.get(VwReplPrevia_.codigoCentroCustos),
root.get(VwReplPrevia_.numeroCartaoPostagem),
root.get(VwReplPrevia_.codigoServico),
root.get(VwReplPrevia_.quantidadeItems),
root.get(VwReplPrevia_.valorServico));
TypedQuery<VwReplPrevia> typedQuery = getEntityManager().createQuery(query);
String contrato = StringUtils.trim(paginador.getEntityBean().getContrato());
String centroCustos = StringUtils.trim(paginador.getEntityBean().getCentroCustos());
String cartaoPostagem = StringUtils.trim(paginador.getEntityBean().getCartaoPostagem());
String servico = StringUtils.trim(paginador.getEntityBean().getServico());
query.where(criteriaBuilder.equal(root.get(VwReplPrevia_.numeroContrato), contrato));
if (StringUtils.isNotBlank(centroCustos)) {
criteriaBuilder.and(criteriaBuilder.equal(root.get(VwReplPrevia_.codigoCentroCustos), "9912231583"));
}
if (StringUtils.isNotBlank(cartaoPostagem)) {
criteriaBuilder.and(criteriaBuilder.equal(root.get(VwReplPrevia_.numeroCartaoPostagem), cartaoPostagem));
}
if (StringUtils.isNotBlank(servico)) {
criteriaBuilder.and(criteriaBuilder.equal(root.get(VwReplPrevia_.codigoServico), servico));
}
query.orderBy(criteriaBuilder.asc(root.get(VwReplPrevia_.codigoCentroCustos)));
query.groupBy(root.get(VwReplPrevia_.codigoCentroCustos),
root.get(VwReplPrevia_.numeroCartaoPostagem),
root.get(VwReplPrevia_.codigoServico),
root.get(VwReplPrevia_.quantidadeItems),
root.get(VwReplPrevia_.valorServico));
List<VwReplPrevia> resultado = typedQuery.getResultList();
同樣的查詢,使用本機查詢,返回結果的正確的號碼。
String sql = "SELECT vwreplprev0_.sdan8 AS col_0_0_, " +
"vwreplprev0_.sdcnid AS col_1_0_, " +
"vwreplprev0_.sdlitm AS col_2_0_, " +
"vwreplprev0_.sduorg AS col_3_0_, " +
"vwreplprev0_.sdaexp AS col_4_0_ " +
"FROM sfe_view.vw_f4211_repl_previa vwreplprev0_ " +
"WHERE vwreplprev0_.sddmct = '9912231583' " +
"GROUP BY vwreplprev0_.sdan8, " +
"vwreplprev0_.sdcnid, " +
"vwreplprev0_.sdlitm, " +
"vwreplprev0_.sduorg, " +
"vwreplprev0_.sdaexp " +
"ORDER BY vwreplprev0_.sdan8 ASC ";
Collection<PreviaFaturaBean> result = getEntityManager().createNativeQuery(sql).getResultList();
paginador.setColecaoDeRegistros(result);
我不知道是什麼問題。我通過標準查詢實施的唯一參考是http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html,有人可以給我一個提示嗎?
感謝您的錯誤,但即使使用正確的版本,輸出也是一樣的。 – 2013-02-08 18:21:34
什麼是生成的SQL查詢? – 2013-02-08 18:40:58
哦對不起'SELECT vwreplprev0_.sdan8 AS col_0_0_, vwreplprev0_.sdcnid AS col_1_0_, vwreplprev0_.sdlitm AS col_2_0_, vwreplprev0_.sduorg AS col_3_0_, vwreplprev0_.sdaexp AS col_4_0_ FROM sfe_view.vw_f4211_repl_previa vwreplprev0_ WHERE vwreplprev0_.sddmct = '9912231583' GROUP BY vwreplprev0_.sdan8, vwreplprev0_.sdcnid, vwreplprev0_.sdlitm, vwreplprev0_.sduorg, vwreplprev0_.sdaexp ORDER BY vwreplprev0_.sdan8 ASC' – 2013-02-08 18:44:53