aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJB3.0 persistence API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3.0 persistence API" Watch "EJB3.0 persistence API" New topic
Author

EJB3.0 persistence API

Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Hello,

I have a question about using EJB3.0 persistence API. If I write a query which join some tables e.g 3 tables (a, b, c), so there are number of fields (a.f1, b.f1, c.f2) retrieved. I am using query.getResultList() to return a Vector, but I want the result is an object collection so that I can use it to call it's method to retrieve the value of field, does anyone how to do this?

Thanks,
Jack
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Originally posted by Jack Lau:
Hello,

I have a question about using EJB3.0 persistence API. If I write a query which join some tables e.g 3 tables (a, b, c), so there are number of fields (a.f1, b.f1, c.f2) retrieved. I am using query.getResultList() to return a Vector, but I want the result is an object collection so that I can use it to call it's method to retrieve the value of field, does anyone how to do this?

Thanks,
Jack


getResultList() return a (non-generified java.util) List.
List is a collection.

It is highly suggested (and all java.util Collections do), that Collections should have a constructor that takes a collection (and adds its elements into the new collection).

If you want the 5.0 generified version of some collection:
new LinkedList<MyTypes>(resultList);
will do it,


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Jack,

The result of getResultList() is a List. If you simply returned a single type of entity from the query then you would be able to do the following:

If you have a SELECT clause containing multiple items then each element of the List will be an array of Object (Object[]) that contains all of the select items. Unfortunately this is an array, not a Collection, and you can't narrow it, even if the objects stored in it were the same type (which is not very likely anyway).


-Mike
Pro JPA 2: Mastering the Java Persistence API
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Thanks!

But if I want to return more than 1 entity, I found that there is an anontation called @SqlResultSetMapping but I do not know how to use it...


How to get the result and what is the return type of the @SqlResultSetMapping ?

Thanks,
Jack
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
I wouldn't recommend using raw SQL queries and @SqlResultSetMapping annotations. The result will be the same as if you used JPQL, except that they will be more work to map the result and less portable.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: EJB3.0 persistence API