aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes EJBQL and its return type 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 "EJBQL and its return type" Watch "EJBQL and its return type" New topic
Author

EJBQL and its return type

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2419
    
    7

Hello all, I'm new to EJB and wish to get the following straighten out:
Suppose I have an entity class Abc with the field email as PK mapped to db table Abc that say have the following columns: email, pwd, name, etc.


Suppose I also have a session bean that want to use the named query "Abc.findByEmail" -> in the bean class I have


Exactly what is that "a" mean between SELECT and FROM? Is it same as *(asterisk) in standard SQL select statements?

Now if I do abcQ.setParameter("email", mail).getSingleResult(); what would I get? How about abcQ.setParameter("email", mail).getResultList();?

The only thing I know is the getSingleResult() return an Object and getResultList() returns a List. The question I want to know is does the getSingleResult() return a single row in the table or a column (field) as an Object or something else?

Given my table has multiple columns, if the returned object is the entire row, how to split it into the correct columns - use those getters methods of the entity class?

Oh one more thing, how to set/pass session attributes from session beans to servlets, similar to web component's session = request.getSession() then session.setAttribute("name",obj)?

Thanks
[ June 02, 2008: Message edited by: K. Tsang ]

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi,

a query in EJBQL returns exactly an object of the type of your entity class. This is very comfortable as you can work directly with the expected type without any casts or conversions. So in your case you get an object of type "Abc" as result type.

Marco
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2419
    
    7

Originally posted by Marco Ehrentreich:
Hi,

a query in EJBQL returns exactly an object of the type of your entity class. This is very comfortable as you can work directly with the expected type without any casts or conversions. So in your case you get an object of type "Abc" as result type.

Marco


Wow you are fast. Thanks Marco. Any idea on the session atttibutes thing?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Any idea on the session atttibutes thing?

Why not just leave this data in the EJB layer of your application? With dependency injection you get very, very easy access to your EJBs directly from a servlet that it's not really necessary to duplicate this data into the servlet itself. Furthermore the attributes in your HttpSession (or any other scope) are only Objects and therefor not very type-safe.

Just use something like this in your servlet:

And you get a reference to the session bean (and the data you actually want) injected into your servlet by the container.

Marco
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: EJBQL and its return type