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?
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:
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).
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 ...
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.
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...