Question

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 !

Was it helpful?

Solution

Finaly I have abandon JPA, and I am using JDBC direct connection with Glassfish JNDI, and it works fine. Thank you !

OTHER TIPS

Had the same issue using Eclipselink with SQL SERVER 2008.

After creating a query with createNativeQuery(queryString, targetEntity), seems that is working only by using .getSingleResult(), but not with .getResultList(). It returns N times the first entity.

I am afraid that by the moment the only way out is to retrieve the data as List<Object[]> by using createNativeQuery(queryString) and do the mapping manually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top