简体   繁体   English

JPA query.getResultList()?

[英]JPA query.getResultList()?

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

Your query selects many fields.您的查询选择了许多字段。 Such a query always returns a list of Object arrays.这样的查询总是返回一个 Object 数组列表。 If you want a list containing instances of your GmaThresholdParameter entity, then the query should be如果您想要一个包含 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.您将收到编译器发出的类型安全警告,您可以忽略该警告。

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.由于您将 getResultList() 返回的列表分配给与您用于自己的空 ArrayList 相同的变量,您的应用程序将失去与您自己的空 ArrayList 的任何连接,Java 会将其作为垃圾收集。 Essentially you created it for absolutely no purpose.本质上,您完全没有任何目的地创建它。

what JB Nizet posted is enough. JB Nizet 发布的内容就足够了。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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