I am facing an issue with JPA by trying to execute Native Query with a SQL SERVER 2008 database. I don't really understand what is happening. When I execute the query directly in the database I got the following result (which is what I am expecting):
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
but in the code, the result is not the same when I retrieve the data from my EntityManager. What I get is the following:
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
I am getting the same result in the same line. Very strange...
Please, can someone help me understand what is the issue. I was suspecting PrimeFaces subtable
component, not to able display good data, but now I am sure that the issue come from JPA Native Query.
See below the query I am using :
"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";
See below the code of the query :
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;
}
}
The code of the EJB that call the List:
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();
}
}
The EJB call the Query Manager to get the query to execute.
Thank you in advance !