我通过尝试使用SQL SERVER 2008数据库执行本机查询来解决JPA的问题。我真的不明白发生了什么。 当我直接在数据库中执行查询时,我得到了以下结果(这是我所期待的):
DS Node Total MinDate MaxDate
EMM CCND 7796 2013-04-16 2013-04-22
EMM CCNV 12049 2013-04-16 2013-04-22
EMM CGSN 1252 2013-04-16 2013-04-22
EMM MSC 7456 2013-04-16 2013-04-22
EMM SMSC 3999 2013-04-16 2013-04-22
但是在代码中,当我从EntityManager中检索数据时结果不一样。 我得到的是以下内容:
DS Node Total MinDate MaxDate
EMM CCND 7796 2013-04-16 2013-04-22
EMM CCND 7796 2013-04-16 2013-04-22
EMM CCND 7796 2013-04-16 2013-04-22
EMM CCND 7796 2013-04-16 2013-04-22
EMM CCND 7796 2013-04-16 2013-04-22
我在同一行得到了相同的结果。很奇怪...
请有人帮我理解是什么问题。我怀疑PrimeFaces subtable
组件,不能显示良好的数据,但现在我确信问题来自JPA Native Query。
请参阅下面我正在使用的查询:
"select distinct DownStream.IDDownStream as DownStream, PortailMediation.IDNoeudOrigineCDR as Node, COUNT(CDR.NomSortie) as TotalFiles," +
" MIN(convert(varchar(19),DateCDR,120)) as MinDate, MAX(convert(varchar(19),DateCDR,120)) as MaxDate" +
" from DownStream, CDR, Equipement, PortailMediation" +
" where DownStream.IDDownStream = Equipement.IDDownStream" +
" and PortailMediation.IDEquipement = Equipement.IDEquipement" +
" and CDR.IDPortailMediation = PortailMediation.IDPortailMediation" +
" and DownStream.Nom = '" + downStream +
"' and convert(varchar(10),DateCDR,103) between '" + beginStr + "' and '" + endStr +
"' group by DownStream.IDDownStream, PortailMediation.IDNoeudOrigineCDR" +
" order by DownStream.IDDownStream, PortailMediation.IDNoeudOrigineCDR";
见下面的查询代码:
public class QueryManager {
private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
private String beginStr;
private String endStr;
/**
*
* @param beginDate
* @param endDate
* @return
*/
public String getGroupedQuery(Date beginDate, Date endDate){
beginStr = sdf.format(beginDate);
endStr = sdf.format(endDate);
String groupedQuery = "select DownStream.IDDownStream as DownStream, PortailMediation.IDNoeudOrigineCDR as Node, COUNT(CDR.NomSortie) as TotalFiles," +
" MIN(convert(varchar(19),DateCDR,120)) as MinDate, MAX(convert(varchar(19),DateCDR,120)) as MaxDate" +
" from DownStream, CDR, Equipement, PortailMediation" +
" where DownStream.IDDownStream = Equipement.IDDownStream" +
" and PortailMediation.IDEquipement = Equipement.IDEquipement" +
" and CDR.IDPortailMediation = PortailMediation.IDPortailMediation" +
" and convert(varchar(10),DateCDR,103) between '" + beginStr + "' and '" + endStr +
"' group by DownStream.IDDownStream, PortailMediation.IDNoeudOrigineCDR" +
" order by DownStream.IDDownStream, PortailMediation.IDNoeudOrigineCDR";
return groupedQuery;
}
/**
*
* @param beginDate
* @param endDate
* @param downStream
* @return
*/
public String getGroupedQueryByDownStream(Date beginDate, Date endDate, String downStream) {
beginStr = sdf.format(beginDate);
endStr = sdf.format(endDate);
String groupedQuery = "SELECT DownStream.IDDownStream AS DownStream, PortailMediation.IDNoeudOrigineCDR AS Node, COUNT(CDR.NomSortie) AS TotalFiles," +
" MIN(convert(varchar(19),DateCDR,120)) AS MinDate, MAX(convert(varchar(19),DateCDR,120)) AS MaxDate" +
" FROM DownStream, CDR, Equipement, PortailMediation" +
" WHERE DownStream.IDDownStream = Equipement.IDDownStream" +
" AND PortailMediation.IDEquipement = Equipement.IDEquipement" +
" AND CDR.IDPortailMediation = PortailMediation.IDPortailMediation" +
" AND DownStream.Nom = '" + downStream +
"' AND convert(varchar(10),DateCDR,103) BETWEEN '" + beginStr + "' AND '" + endStr +
"' GROUP BY DownStream.IDDownStream, PortailMediation.IDNoeudOrigineCDR" +
" ORDER BY DownStream.IDDownStream, PortailMediation.IDNoeudOrigineCDR";
return groupedQuery;
}
}
调用List的EJB代码:
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import tg.moov.imereport.dao.DownStreamTotal;
import tg.moov.imereport.util.QueryManager;
@Named
@Stateless
public class DownStreamTotalEJB {
@PersistenceContext
private EntityManager em;
private QueryManager qm = new QueryManager();
public DownStreamTotalEJB() {
}
/**
* Get the grouped files by period
* @param begin
* @param end
* @return
*/
public List<DownStreamTotal> getGroupedData(Date begin, Date end) {
Query q = em.createNativeQuery(qm.getGroupedQuery(begin, end), DownStreamTotal.class);
return q.getResultList();
}
/**
* Get the grouped files by period and by downStream
* @param begin
* @param end
* @param downStream
* @return
*/
public List<DownStreamTotal> getGroupedDataByDownStream(Date begin, Date end, String downStream) {
Query q = em.createNativeQuery(qm.getGroupedQueryByDownStream(begin, end, downStream), DownStreamTotal.class);
return q.getResultList();
}
}
EJB调用查询管理器以使查询执行。
提前谢谢!
答案 0 :(得分:0)
最后我放弃了JPA,我使用JDBC直接连接Glassfish JNDI,它运行正常。 谢谢!
答案 1 :(得分:0)
在SQL SERVER 2008中使用Eclipselink时遇到了同样的问题。
使用createNativeQuery(queryString, targetEntity)
创建查询后,似乎只能使用.getSingleResult()
,但不能使用.getResultList()
。它返回第一个实体的N倍。
我担心目前唯一的出路是使用List<Object[]>
将数据检索为createNativeQuery(queryString)
并手动执行映射。