Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
779 views
in Technique[技术] by (71.8m points)

java - JPA query.getResultList()?

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 "
        )
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...