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
同じ行で同じ結果が得られます。非常に奇妙です... 誰かが問題を理解するのを手伝ってくれませんか。PrimeFacessubtable
コンポーネントが適切なデータを表示できないのではないかと疑っていましたが、問題が 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 はクエリ マネージャを呼び出して、実行するクエリを取得します。
前もって感謝します !