8 Replies Latest reply on May 23, 2007 7:31 PM by Michael Dinsmore

    How to get this (very simple) tutorial to work in JBoss 4.2.

    hellek Newbie

      I am trying for hours already to get this tutorial to work in JBoss 4.2.0:
      http://www.eclipse.org/webtools/jst/components/j2ee/scenarios/jee5_application_creation_tutorial.html

      I always get a NPE with that:
      @PersistenceUnit(name="EJBProject1") EntityManagerFactory emf;

      ...
      ...
      (inside a method)
      EntityManager em1 = emf.createEntityManager(); // <== This causes a NullPointerException

      Maybe its related to this: http://www.jboss.com/index.html?module=bb&op=viewforum&f=221
      but I can't figure out how to fix it. Any help is appreciated.
      The eclipse project files can be downloaded here: http://hellekalek.com/diverses/workspace.zip

        • 1. Re: How to get this (very simple) tutorial to work in JBoss
          wayne baylor Apprentice

          I bet the injection of the EntityManager Factory is failing.

          Check out this post:
          http://jboss.com/index.html?module=bb&op=viewtopic&t=107353

          • 2. Re: How to get this (very simple) tutorial to work in JBoss
            hellek Newbie

            Yes I did already, I wanted to paste this URL where I wrote "maybe its related to this".

            I just don't understand it well enough to know what I exactly need to do. Tried a lot but it never worked :-(
            I work on that problem for a full day now, somehow embarassing already.

            • 3. Re: How to get this (very simple) tutorial to work in JBoss
              hellek Newbie

              I also found this (yesterday already) http://forum.java.sun.com/thread.jspa?threadID=5162297&tstart=60
              but I just don't understand it.

              They all have interfaces etc. and in the tutorial I tried to follow, they don't have such things at all.

              In the tutorial they have:
              1.) An EJB-Project with the persistence.xml and a Bean. The bean is just a "copy" of an existing table with getters and setters for all columns and the @Entity and @Id Annotations.
              My persistence.xml looks like this:

              <?xml version="1.0" encoding="UTF-8"?>
              <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
               <persistence-unit name="EJBProject1">
               <provider>org.hibernate.ejb.HibernatePersistence</provider>
               <jta-data-source>java:/TestDS</jta-data-source>
               <class>com.Benutzer</class>
               <properties>
               <property name="hibernate.dialect"
               value="org.hibernate.dialect.MySQLDialect" />
               <property name="hibernate.transaction.manager_lookup_class"
               value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
               <property name="hibernate.hbm2ddl.auto" value="update"/>
               </properties>
               </persistence-unit>
              </persistence>


              The datasource works fine, i.e. hibernate succesfully updates the table if I mess it up.

              2.) A WebProject
              With a servlet which looks like this:
              public class Servlet1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
              
              public Servlet1() {
               super();
              }
              
              @PersistenceUnit(name="EJBProject1")
              EntityManagerFactory emf;
              
              /* (non-Java-doc)
              * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
              */
              protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              response.setContentType("text/html");
              response.setHeader("Pragma", "No-cache");
              response.setDateHeader("Expires", 0);
              response.setHeader("Cache-Control", "no-cache");
              
              List users = emf.createEntityManager().createQuery("select b from Benutzer b").getResultList();
              PrintWriter writer = response.getWriter();
              
              for (Iterator iterator = users.iterator(); iterator.hasNext();) {
               Benutzer benutzer = (Benutzer) iterator.next();
               writer.print(benutzer.getName());
               }
               writer.flush();
              writer.flush();
              }
              }
              


              and a plain normal web.xml that just maps Servlet1 to an url

              3.) Those 2 projects are then combined into an EARProject. Result is a .EAR-File with a jar for the Bean and a war for the webproject inside and the following application.xml
              <?xml version="1.0" encoding="UTF-8"?>
              <application id="Application_ID" version="5.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_5_0.xsd">
               <display-name>
               EARProject1</display-name>
               <module>
               <web>
               <web-uri>WebProject2.war</web-uri>
               <context-root>WebProject2</context-root>
               </web>
               </module>
               <module>
               <ejb>EJBProject1.jar</ejb>
               </module>
              </application>


              All 3 projects have a MANIFEST.MF that just says
              Manifest-Version: 1.0
              Class-Path:
              


              I hope this illustrates the content very well, unfortunately I don't understand how to apply what people say in those topics about JNDI etc. onto this structure.

              It would really be great if somebody could tell me how to do it.

              • 4. Re: How to get this (very simple) tutorial to work in JBoss
                wayne baylor Apprentice

                Okay, here's what I would try:

                Use the Session Facade Pattern.

                Create a stateless session bean and inject the EntityManagerFactory into it using the code you have in your servlet class (cut and paste it). Get a reference to this bean in your servlet using JNDI lookup:

                InitialContext ctx = new InitialContext();
                FacadeBeanLocal bean = ctx.lookup("FacadeBean/local");
                bean.doWhatever();


                The lookup string will vary depending on your packaging.
                Using and EAR: "<ear-name>/<bean-name>/[local | remote]"
                Using a JAR: "<bean-name>/[local | remote]"

                Make sure you label the interface your facade bean implements with either @Local or @Remote.

                • 6. Re: How to get this (very simple) tutorial to work in JBoss
                  Michael Dinsmore Newbie

                  I've done the (new InitialContext().lookup("beanName/local") and that works all fine and dandy. I'd really PREFER to use the
                  @EJB(name="beanName/local") private Bean beanName; instead, but its not working.

                  Note: I'm using 4.0.4GA. I had applied some EJB 3.0 patch a long time ago and forgot which one so I just tried with what seems to be the latest possible version for my version of JBoss -- I applied the jboss-ejb-3.0-RC9-fd.zip using the Ant install.xml script. I use Java 5.

                  Am I doing something wrong? I basically removed the InitialContext code and use the @EJB instead and that's the only change so I know its not any deployment, etc type of error.

                  Thanks for any insight. I think it seems like the @EJB injection is really useful and can simplify the testing process. Maybe I'm wrong since I can't even get that working yet... but I'd like to potentially simplify some unit tests and my mock ejb containers....

                  • 7. Re: How to get this (very simple) tutorial to work in JBoss
                    wayne baylor Apprentice

                    Nope, I don't think you're doing anything wrong :)

                    JBoss 4.0.x and 4.2 don't support injection in servlets. The reason is posted here:
                    http://jboss.com/index.html?module=bb&op=viewtopic&t=107353

                    • 8. Re: How to get this (very simple) tutorial to work in JBoss
                      Michael Dinsmore Newbie

                      Thanks. I guess that kind of clears it up. I wasn't trying to do it "in servlets" but I guess that language was JBoss's way of saying, "it just doesn't work". It would be nice if they are more clear on that.

                      So is the first version of JBoss that really supports this is the JBoss 5.x version? Without the @EJB injection, it seems to me I'd be upgrading to the EJB3 without any real clear benefits other than "its different". At least with the resource injection, I can remove my Delegate and refer to the bean directory -- and just have the interface define it. This saves from implementing a concreate class which duplicates the same type of code. For me, I'm saying better = less code.

                      thanks!
                      -Mike