JPA query.getResultList()? [英] JPA query.getResultList()?

查看:1795
本文介绍了JPA query.getResultList()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JPA 1.0:

I use JPA 1.0:

Query query;
            query = em.createNamedQuery("getThresholdParameters");
            query.setParameter(1, Integer.parseInt(circleId));

            List<Object[]> resultList = new ArrayList();
            resultList  = query.getResultList();

这里我得到的结果为 List< Object []> ,因此我必须将行的所有参数转换为它们各自的类型,这很麻烦。

Here I get result as List<Object[]>, thus I have to type convert all the parameters of the row to their respective types which is cumbersome.

在JPA 2.0中有TypedQuery返回一个实体类型1的对象指定。

In JPA 2.0 there is TypedQuery which return an entity object of type one specifies.

但是当我使用JPA 1时我无法使用它。

But as I am using JPA 1 I can't use it.

如何获得结果作为我想要的类型的实体对象??

How to get result as Entity object of type I want??

编辑:
QUERY

QUERY

@Entity
@Table(name="GMA_THRESHOLD_PARAMETERS")
@NamedQuery(

        name = "getThresholdParameters",

        query = "select gmaTh.minNumberOc, gmaTh.minDurationOc, gmaTh.maxNumberIc, gmaTh.maxDurationIc, gmaTh.maxNumberCellId,"
                + "gmaTh.distinctBnumberRatio, gmaTh.minPercentDistinctBnumber from GmaThresholdParameter gmaTh " 
                + "where gmaTh.id.circleId=?1 AND gmaTh.id.tspId=?2 AND gmaTh.id.flag=?3 "
        )


推荐答案

您的查询选择了许多字段。这样的查询总是返回Object数组的列表。如果你想要一个包含你的GmaThresholdParameter实体实例的列表,那么查询应该是

Your query selects many fields. Such a query always returns a list of Object arrays. If you want a list containing instances of your GmaThresholdParameter entity, then the query should be

select gmaTh from GmaThresholdParameter gmaTh 
where gmaTh.id.circleId=?1 AND gmaTh.id.tspId=?2 AND gmaTh.id.flag=?3

获取实体列表的代码将是

The code to get the list of entities would then be

List<GmaThresholdParameter> resultList = query.getResultList();

您将从编译器获得类型安全警告,您可以忽略。

You'll get a type safety warning from the compiler, that you can ignore.

这篇关于JPA query.getResultList()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆