File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using EntityManager from a servlet?

 
Karl Stenerud
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been reading a number of web pages and blogs that show how to use an EntityManager in a servlet, and yet I have been unable to get a single example to work

In an existing J2EE application, deployed to JBoss, I can from any EJB do the following:



And it just works. However, if I try the same thing from a servlet (in the same J2EE application):



It fails with a NullPointerException because em never got injected (and JBoss never complained, either)

If I try it like this:



I get javax.naming.NameNotFoundException: persistence not bound

I've tried it with a persistence context ref in web.xml and without. It seems to make no difference whatsoever!



The whole JNDI "magical incantation" thing has always driven me up the wall, but this is sheer madness! How is one ever supposed to know what magical string value to use where?
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you packaging your persistence.xml file? Can we see its contents?
 
Karl Stenerud
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
META-INF/persistence.xml:



The data source is deployed like so:


 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
STOP!!!


Please, please don't.

You are tightly coupling one technology with another. Please don't.

This is not the responsibility of a Servlet. A Servlet takes a request object, gets information from it and should immediately pass it off to some other Java class/code do to the rest of the work. Once the work is done, the data stuff that needs to be sent to a response is passed back into the Servlet from the java call, it is then the Servlets responsibility to take the data put it into the response object, and pass it off to some view, usually a JSP page.

But do not do any business logic, or EntityManager dao/data stuff in Servlet code.

Mark
 
Karl Stenerud
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not for putting business code in the servlet. This is for getting servlet configuration data from the database.

I want to get an entity manager so that I can inject it into a configuration object that will do the DB magic to get me the configuration options I need to initialize this servlet.
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karl Stenerud wrote:This is not for putting business code in the servlet. This is for getting servlet configuration data from the database.

I want to get an entity manager so that I can inject it into a configuration object that will do the DB magic to get me the configuration options I need to initialize this servlet.


OK. Good glad it is not business code. Personally, I still wouldn't put servlet configuration options into a database, but into a servlet configuration file instead. But that is just my opinion, and you are free to move about the cabin.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic