File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Unable to use @NamedQuery in a session bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Unable to use @NamedQuery in a session bean?" Watch "Unable to use @NamedQuery in a session bean?" New topic
Author

Unable to use @NamedQuery in a session bean?

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
I have a session bean (tried both stateful and stateless) and tried to convert one of the queries used to a Named Query.
However, when I attempt to create an instance of the query at runtime, I get an exception which says Named query not found: findUserByName, where findUserByName is my query name.

Here is the pertinent pieces of code from my session bean. Can anyone see what I'm doing wrong?

Thanks



The line where I attempt to create the query instance is where I get the exception. The exception looks like this:


The NamedQuery annotation I'm using is from the following import:

Is that the correct one?

Thanks for any insight.
Bhagat Singh Rawat
Ranch Hand

Joined: Apr 04, 2009
Posts: 93
Please go through this link Solution. You might get your answer. Looks like configuration issue.


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
It seems the only thing they did that might apply to my case was to move the peristence.xml file from META-INF to a conf directory. However, I've been told that META-INF is the correct location for this file.

Note that I'm not using an external Jar file for the packaging of utility classes. My EAR file consists of an ejb-jar.jar file, a web.war file, and an application.xml file.

I'm sure I'm doing something silly, but I don't see what. My example comes right out of the book: EJB3 in Action, and as far as I can see, I've duplicated it exactly (other than the name of the query, of course).

Can anyone see what I'm doing wrong?

Thanks,
Jakub Stransky
Greenhorn

Joined: Jan 04, 2010
Posts: 4
Are you running NetBeans under linux? I have a similar problem and I have found that problem is packageing - I was also including external jar to final war file. Despite the fact that I was using the same building scritpt a got different war files (structure) on win x linux. Linux one didnt merge the folders correctly because he differenciate upper and lower case. So I got a different place for persistence.xml in final wer file.
Your situations seems very similar ...
Examine the structure of var archive. I run verifier and no collision but the place is wrong ...
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Actually, I'm using Eclipse (Galileo) on Windows XP, deploying to JBoss 5.1.0.GA.
The EAR file seems to be built properly. Here is the layout:

As you can see, the persistence.xml file is in the ejb-jar file, where the session bean also resides.

Here is the persistence.xml file:
Is this happening because I'm using the hbm2ddl? I don't see why that would matter, so I sure hope not.
Jakub Stransky
Greenhorn

Joined: Jan 04, 2010
Posts: 4
Hi,
I am new to J2EE so it was just suggestion. If you are sure then no problem. And also check the availability of @NamedQuery from session beans - because I am not sure about using JPA annotation from EJB container - check JPA specificiation.

Sorry for my poor suggestion, but as I said I am new to J2EE.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Jakub,

Thank you for your help. I am new as well, and appreciate it very much.

I assumed that the annotation was allowed in the session bean, as that seems like the best place to use it. However, in looking at the specification, I see:
JSR220 wrote:The NamedQuery and NamedQueries annotations can be applied to an entity or mapped superclass.
Does this mean that the annotation cannot be used in stateful/stateless session beans?

Thanks,
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
Oh ... got it. After reading the chapter on the @NamedQuery annotation in the EJB3 in Action book, I see this:
Originally, I missed the in the entity part.

So, if I define the named query in the entity class, I am able to create an instance of it in the session bean class.

Whew! It just didn't make sense to me that I would want to execute the query inside the entity class itself. What's the point of that

Thanks to all for your help!
Christian Dillinger
Ranch Hand

Joined: Jul 20, 2009
Posts: 188
Of course it makes sense to declare NamedQueries in the entity class. Because the query must be known to the EntityManager and the source where he gets his information from is the entity itself (or its hbm.xml-File). Declaring a Query in the entity doesn't say, that the entity should provide methods to execute the query...
Prateek Kumar Singh
Greenhorn

Joined: Jan 21, 2011
Posts: 29

Hi,
actually you are going wrong here that define named query in bean class and creating it in same class
your named query should be in entity class and named query for example

if you want to create named query in bean class then follow this example


hope it help you.

Prateek
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to use @NamedQuery in a session bean?
 
Similar Threads
design decision: variation of session fassade
Using Visual Web JavaServerFaces to display db data in a table
Trying to figure out how to use MySQL and generated @Id
tracking changes in an array in a session bean
jsf navigation and database connection