Hello i would like to use Application-Managed Entitymanager in my Application and have
been tryin to get Dependency Injection workin in my Servlet for quite a time now and have
not found the error yet. The PersistenceUnit is mapped successfully when deployed on JBoss
4.0.5 but it seems to me that it is not reachable within the servlet for some reasons?
My directory structure:
lecker-ear.ear
\ META-INF ( application.xml, jboss-app.xml )
\ lecker-ejb.jar ( META-INF( persistence.xml, jboss.xml ), model.Login.java )
\ lecker-war.war ( WEB-INF( web.xml, jboss-web.xml ) + classes & jsps )
application.xml
<?xml version="1.0" encoding="UTF-8"?>
| <application
xmlns="http://java.sun.com/xml/ns/javaee"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
|
http://java.sun.com/xml/ns/javaee/application_5.xsd"
| version="5">
| <display-name>MyApp</display-name>
| <module id="lecker-ejb">
| <ejb>lecker-ejb.jar</ejb>
| </module>
| <module id="lecker-war">
| <web>
| <web-uri>lecker-war.war</web-uri>
| <context-root>lecker-war</context-root>
| </web>
| </module>
| </application>
I have put this into the jboss-app.xml cause I read i need to specify som ClassLoader
though I do not really understand how this is going to affect anything:
jboss-app.xml
<?xml version="1.0"?>
| <!DOCTYPE jboss-app PUBLIC "-//JBoss//DTD J2EE Application 1.4//EN"
| "http://www.jboss.org/j2ee/dtd/jboss-app_4_0.dtd">
| <jboss-app>
|
<loader-repository>lecker-ear:archive=lecker-ear.ear</loader-repository>
| </jboss-app>
persistence.xml
| <?xml version="1.0" encoding="UTF-8"?>
| <persistence
xmlns="http://java.sun.com/xml/ns/persistence">
| <persistence-unit name="leckerPU"
transaction-type="JTA">
| <jta-data-source>java:/LeckerDS</jta-data-source>
| <class>model.Login</class>
| </persistence-unit>
|
web.xml
| <?xml version="1.0" encoding="UTF-8"?>
| <web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
|
| <session-config>
| <session-timeout>
| 30
| </session-timeout>
| </session-config>
| <welcome-file-list>
| <welcome-file>Login.jsp</welcome-file>
| </welcome-file-list>
|
| <servlet>
| <servlet-name>LoginServlet</servlet-name>
| <servlet-class>Login</servlet-class>
| </servlet>
| <servlet-mapping>
| <servlet-name>LoginServlet</servlet-name>
| <url-pattern>/login</url-pattern>
| </servlet-mapping>
| </web-app>
|
I have tried several different ways to obtain the EntityManager within the Login Servlet
but none worked. If I inject it directly the Objects just stay null:
Login.javal
| public class Login extends HttpServlet {
| @PersistenceUnit
| EntityManagerFactory emf;
| @PersistenceContext
| EntityManager em;
|
|
| protected final void doPost(final HttpServletRequest request, final
HttpServletResponse response) throws ServletException, IOException {
| if(em ==null)
| System.out.println("em is null");
| if(emf ==null)
| System.out.println(" emf is null");
|
Specifying the UnitName doesnt help:
Login.javal
| public class Login extends HttpServlet {
| @PersistenceUnit(unitName = "leckerPU")
| EntityManagerFactory emf;
| @PersistenceContext(unitName = "leckerPU")
| EntityManager em;
|
|
| protected final void doPost(final HttpServletRequest request, final
HttpServletResponse response) throws ServletException, IOException {
| if(em ==null)
| System.out.println("em is null");
| if(emf ==null)
| System.out.println(" emf is null");
|
If I try to look up the EntityManager or -Factory by JNDI I get a NameNotBound Exception
what makes me think the problem already occurs while looking up the PU.
Login.javal
| @PersistenceContext(name="persistence/lecker", unitName =
"leckerPU", type = PersistenceContextType.EXTENDED)
| public class Login extends HttpServlet {
|
| protected final void doPost(final HttpServletRequest request, final
HttpServletResponse response) throws ServletException, IOException {
| try {
| EntityManager em = (EntityManager) new
InitialContext().lookup("java:/comp/env/persistence/lecker");
| } catch (NamingException e) {
| System.out.println("grrh");
| e.printStackTrace(); //To change body of catch statement use File |
Settings | File Templates.
| }
| }
|
Looking up the EntityManagerFactory in JNDI doesnt work either.
So what am I actually doing wrong?
View the original post :
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4028003#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...