2013-04-29 110 views
-1

我正試圖用SQL SERVER 2008數據庫執行原生查詢,因此面臨着JPA的問題。我真的不明白髮生了什麼事。 當我直接在數據庫中執行查詢,我得到了下面的結果(這是我期待):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;  
    } 
} 

的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調用查詢管理器來執行查詢。

預先感謝您!

+1

您如何確定JPA是問題?是否打印或調試了集合的結果以驗證集合中返回的所有5 Object []是否與從getResultList()調用返回的相同? – Chris 2013-04-29 12:45:57

+0

是的,我也嘗試通過迭代它們來獲得個體值,並且我注意到,我具有相同的值。但對於在數據庫中執行的相同查詢,我還有另一個結果。 – 2013-04-29 16:49:59

+0

然後顯示你的代碼,因爲字符串是不夠的。 – Chris 2013-04-29 17:58:49

回答

0

Finaly我放棄了JPA,而且我正在使用JDBC與Glassfish JNDI直接連接,並且它工作正常。 謝謝!

0

一直使用的EclipseLink與SQL Server 2008中

同樣的問題,創建一個查詢後與createNativeQuery(queryString, targetEntity),似乎是通過使用.getSingleResult()唯一的工作,但不能與.getResultList()。它返回N倍第一個實體。

恐怕到目前爲止唯一的出路是通過使用createNativeQuery(queryString)來檢索數據爲List<Object[]>並手動執行映射。