• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB3.0 persistence API

 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic