Java JPA query.getResultList()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20901282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:09:12  来源:igfitidea点击:

JPA query.getResultList()?

javajakarta-eejpapersistence

提问by Siddharth Trikha

I use JPA 1.0:

我使用 JPA 1.0:

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

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

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.

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

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

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

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

但是当我使用 JPA 1 时,我不能使用它。

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

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

EDIT: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 "
        )

采纳答案by JB Nizet

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

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

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.

您将收到编译器发出的类型安全警告,您可以忽略该警告。

回答by Gimby

I can't respond to this as a comment so I'll just go ahead and make it an answer.

我无法将其作为评论来回应,所以我会继续并使其成为答案。

List<Object[]> resultList = new ArrayList(); // CREATE an empty ArrayList object
resultList  = query.getResultList(); // getResultList ALSO returns its own ArrayList object

And since you assign the list that getResultList() returns to the same variable as you used for your own empty ArrayList, your application loses any connection to your own empty ArrayList and Java will collect it as garbage. Essentially you created it for absolutely no purpose.

由于您将 getResultList() 返回的列表分配给与您用于自己的空 ArrayList 相同的变量,您的应用程序将失去与您自己的空 ArrayList 的任何连接,Java 会将其作为垃圾收集。本质上,您完全没有任何目的地创建它。

what JB Nizet posted is enough.

JB Nizet 发布的内容就足够了。

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

回答by ND27

I have done something similar since I was using JPA 1 at that time:

自从我当时使用 JPA 1 以来,我做了类似的事情:

    final Collection<YourType> typedResult = new ArrayList<YourType> 
    for(final Object result : query.getResultList()) 
    { 
         typedResult.add((YourType) result); 
    }  
    return typedResult; 

回答by hakim mourad

 List<GmaThresholdParamerter> result= query.getResultList();
   for( GmaThresholdParamerter res : result)
   {
     System.out.println("" +res.getMinNumberOc());
     System.out.println("" +res.getMinDurationOc());
  }