• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to use @NamedQuery in a session bean?

 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please go through this link Solution. You might get your answer. Looks like configuration issue.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 30
Hibernate Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic