Setting up an Entity not associated to a DB table for getting resultList from a procedure call

无人久伴 提交于 2019-12-13 00:54:04

问题


I am using jpa 2.1 ability to call a MySql procedure using em.createStoredProcedureQuery().

The procedure itself is working and is returning a numbered list of names and email addresses as seen in MySql Workbench :

My problem is with setting up the POJO for storing the list of results. My entity class is very basic and has only three properties + the getters/and setters. A three-argument constructor and a toString() method are provided. Note that there is no corresponding table associated to this entity in the database, the entity being used only to translate the query result list in a list of java objects :

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

@Entity
public class NomsEtCourriels implements Serializable {

    @Id
    private Integer noLigne;
    private String noms;
    private String courriels;


    public NomsEtCourriels() {
        System.err.println("[NomsEtCouriels]no argument constructor");
    }

    public NomsEtCourriels(Integer noLigne, String noms, String courriels) {
        System.err.println("[NomsEtCouriels]3 field constructor : " + noLigne     + ", noms=" + noms + ", courriels=" + courriels);
        this.noLigne = noLigne;
        this.noms = noms;
        this.courriels = courriels;
    }

+ getters / + setters

    public String toString() {
        return getNoLigne() + ":" + noms + " " + courriels;
    }

When executing, I am getting an error because two of the fieds are returning null values :

javax.persistence.PersistenceException: Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [DatabaseRecord(
    NOMSETCOURRIELS.NOLIGNE => null
    NOMSETCOURRIELS.COURRIELS => <de****@hotmail.com>, 
    NOMSETCOURRIELS.NOMS => null)] during the execution of the query was detected to be null.  Primary keys must not contain null.
Query: ResultSetMappingQuery(referenceClass=NomsEtCourriels )

If I move the @Id annotation to the courriel (email) field, I don't get any error but the output shows that the same two fields are still null even if the DB procedure is indeed returning values :

   [MembresControleur]init this : org.labottedefoin.bfjsf.jpa.MembresControleur@1d84583b
   [ejbMembresFacade]faireEnvoiCourriel envoiCourriel =StoredProcedureQueryImpl(ResultSetMappingQuery())
   [EL Fine]: sql: 2015-03-10 22:42:46.171--ClientSession(955321153)--Connection(1494873499)--Thread(Thread[http-listener-2(1),5,main])--{ CALL envoi_courriel() }
   [ejbMembresFacade]faireEnvoiCourriel  envoiCourriel.execute() returned True.
   [ejbMembresFacade]faireEnvoiCourriel  courriel : null:null <de*****1@hotmail.com>, 
   [ejbMembresFacade]faireEnvoiCourriel  courriel : null:null <au****@hotmail.fr>, 
   [ejbMembresFacade]faireEnvoiCourriel  courriel : null:null <v****@b2b2c.ca>, 
   [ejbMembresFacade]faireEnvoiCourriel  courriel : null:null <cha****@live.fr>, 
   [ejbMembresFacade]faireEnvoiCourriel  courriel : null:null <si*****@b2b2c.ca>, 

Here is the actual method where the DB procedure is called :

public List<NomsEtCourriels> faireEnvoiCourriel () {   
    List<NomsEtCourriels> courriels;
    StoredProcedureQuery envoiCourriel = getEntityManager().createStoredProcedureQuery("envoi_courriel", NomsEtCourriels.class);
    logger.info("[ejbMembresFacade]faireEnvoiCourriel envoiCourriel =" + envoiCourriel);
    if (envoiCourriel.execute()) {
            logger.info("[ejbMembresFacade]faireEnvoiCourriel  envoiCourriel.execute() returned True.");
        courriels = envoiCourriel.getResultList();
        if (courriels != null) {// Iterate over the result set
            for (NomsEtCourriels courriel : courriels) {
                logger.info("[ejbMembresFacade]faireEnvoiCourriel  courriel : " + courriel);
            }
        }
        return courriels;
    } else {
        logger.info("[faireEnvoiCourriel]  execute n'a renvoyé aucun result set");
        return null;
    }
}

Any idea why the line number and name fields are not set correctly in the POJO?

来源:https://stackoverflow.com/questions/28977819/setting-up-an-entity-not-associated-to-a-db-table-for-getting-resultlist-from-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!