1

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();

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.

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

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

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 "
        )
Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101
  • By defining the proper JPQL. Post the named query the code refers to. And PS: that instance of ArrayList you are manually creating is instantly overwritten by the list that the query returns. – Gimby Jan 03 '14 at 10:19
  • @Gimby: HAve posted the query – Siddharth Trikha Jan 03 '14 at 10:21
  • 1
    Looks like this has been asked before. http://stackoverflow.com/questions/6119683/typedquery-equivalent-for-jpa-1-0 If it was me I'd be looking to upgrade to JPA 2, this could be the perfect excuse. Also don't waste time instantiating resultList if you're only going to overwrite it. – Ben Thurley Jan 03 '14 at 10:21
  • @BenThurley: I didn't get the overwrite thing? Where else shall I capture the resultList? – Siddharth Trikha Jan 03 '14 at 10:23
  • (I posted an answer to try and show you what we mean) – Gimby Jan 03 '14 at 10:54
  • This answer using constructor expressions might also solve your problem: http://stackoverflow.com/a/6532840/139595 – Jörn Horstmann Jan 03 '14 at 12:05
  • Gimby has explained the overwrite. Basically you create an Arraylist object for the resultList reference but then point the resultList reference to the ArrayList returned by getResultList(). Your original ArraList will then be eligible for garbage collection since it is not referenced anywhere. It's probably only a minor issue here but it's worth understanding because its Java basics. – Ben Thurley Jan 03 '14 at 13:39

4 Answers4

1

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • :This will also return id with all other members of entity, which I don't want – Siddharth Trikha Jan 03 '14 at 10:30
  • Then you'll have to deal with a `List`. Note that excluding some columns when selecting rows from a table generally doesn't have any measurable impact on performance. You're probably optimizing prematurely, and in the wrong way. – JB Nizet Jan 03 '14 at 10:32
1

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.

what JB Nizet posted is enough.

List<GmaThresholdParameter> resultList = query.getResultList();
Gimby
  • 5,095
  • 2
  • 35
  • 47
-1

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

    final Collection<YourType> typedResult = new ArrayList<YourType> 
    for(final Object result : query.getResultList()) 
    { 
         typedResult.add((YourType) result); 
    }  
    return typedResult; 
ND27
  • 447
  • 1
  • 5
  • 16
-2
 List<GmaThresholdParamerter> result= query.getResultList();
   for( GmaThresholdParamerter res : result)
   {
     System.out.println("" +res.getMinNumberOc());
     System.out.println("" +res.getMinDurationOc());
  }
jeff porter
  • 6,560
  • 13
  • 65
  • 123