简体   繁体   中英

Why Named Native Query in JPA gives Type Cast Exception

    List<EmailMaster> result = null;
    EntityManager eManager = getEntityManager();
    Query query = eManager.createNamedQuery("EMAIL_MASTER_BYSTATUS");
    query.setParameter(1, "0");
    result = query.getResultList();----here no problem why
    System.out.println("EMAIL_MASTER_BYSTATUS :" + result.size());

when I am iterating typecast error comes

         EmailMaster em = emIterator.next();

problem only comes when I am putting that in em.

mapping.xml

    <named-native-query name="EMAIL_MASTER_BYSTATUS">
        <query >SELECT * FROM RDT_EMAIL_MASTER WHERE STATUS = ?</query>
    </named-native-query>

YOu should define result class. See here http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html

<resultset name="personAddress">
    <return alias="person" class="eg.Person"/>
    <return-join alias="address" property="person.mailingAddress"/>
</resultset>

<sql-query name="personsWith" resultset-ref="personAddress">
    SELECT person.NAME AS {person.name},
           person.AGE AS {person.age},
           person.SEX AS {person.sex},
           address.STREET AS {address.street},
           address.CITY AS {address.city},
           address.STATE AS {address.state},
           address.ZIP AS {address.zip}
    FROM PERSON person
    JOIN ADDRESS address
        ON person.ID = address.PERSON_ID AND address.TYPE='MAILING'
    WHERE person.NAME LIKE :namePattern
</sql-query>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM