jpa 结果集映射抛出 NPE

Posted

技术标签:

【中文标题】jpa 结果集映射抛出 NPE【英文标题】:jpa result-set-mapping throw NPE 【发布时间】:2011-09-29 10:43:41 【问题描述】:

我无法将查询结果映射到 POJO。

JPA 执行查询正常(它在日志中跟踪)但在映射过程中抛出 NPE。

怎么了?

NPE:

java.lang.NullPointerException
at org.eclipse.persistence.queries.EntityResult.getValueFromRecord(EntityResult.java:147)
at org.eclipse.persistence.queries.ResultSetMappingQuery.buildObjectsFromRecords(ResultSetMappingQuery.java:165)
at org.eclipse.persistence.queries.ResultSetMappingQuery.executeDatabaseQuery(ResultSetMappingQuery.java:215)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:675)
at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:589)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2857)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1225)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1207)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1181)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:453)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:681)
at com.silyan.meyem.view.visor.tienda.TiendaResources.delusuario(TiendaResources.java:284)

执行代码:

List<TiendaTO> tiendas = entityManager.createNamedQuery("TiendaResources.tiendas_del_usuario", TiendaTO.class).setParameter(1, user.getId()).getResultList();

元数据:

    <named-native-query name="TiendaResources.tiendas_del_usuario" result-set-mapping="TiendaTOMapping">
<query>
select
    tienda.id as id,
    tienda.nombre as nombre,
    tienda.idporcliente as idporcliente,
    tienda.cliente_id as clienteid,
    ST_X(direccion.geolocalizacion) as x,
    ST_Y(direccion.geolocalizacion) as y
from
    user_tienda,
    tienda,
    direccion
where
    user_tienda.user_id = ? and
    tienda.id = user_tienda.tienda_id and
    direccion.id = tienda.direccion_id
</query>
</named-native-query>

<sql-result-set-mapping name="TiendaTOMapping">
    <entity-result entity-class="com.silyan.meyem.view.visor.tienda.TiendaTO" >
        <field-result name="id" column="id"/>
        <field-result name="idPorCliente" column="idporcliente"/>
        <field-result name="nombre" column="nombre"/>
        <field-result name="x" column="x"/>
        <field-result name="y" column="y"/>
    </entity-result>
</sql-result-set-mapping>

平台: Glassfish 3.0.1 - eclipselink 2.0.1.v20100213-r6600

谢谢!

【问题讨论】:

【参考方案1】:

com.silyan.meyem.view.visor.tienda.TiendaTO 是映射类吗?如果没有,那么您将面临这个问题:https://bugs.eclipse.org/bugs/show_bug.cgi?id=288957

但另一方面,“entity-result entity-class”,预期实体是有道理的,当然一些实现也可以支持其他类.

【讨论】:

TiendaTO是一个没有@Entity注解的POJO,因为没有映射到数据库表,所以在eclipselink中是不是不可能将结果查询映射到POJO? 示例中的错字。我使用“结果集映射”而不是“结果类”

以上是关于jpa 结果集映射抛出 NPE的主要内容,如果未能解决你的问题,请参考以下文章

JPA:迭代大型结果集的正确模式是啥?

JPA:如何将原生查询结果集转换为 POJO 类集合

使用 JPA 2.1,我如何将本机查询结果映射到 @Transient 字段(taht is a Set)?

JPA:仅当结果集不为空时才缓存查询

有没有办法获取带有结果集的 JPA 命名查询的计数大小?

jpa(休眠)中具有重复行的结果集