4 Replies Latest reply on Dec 10, 2009 9:25 AM by Gabriel Muench

    How to find Entity by Id

    Gabriel Muench Newbie

      Hello,
      i am using jboss portal 2.7.2 bundle with jboss 4.2.3 and i like to use ejb3 persistence for database query.
      But i get this exception:

      15:45:31,530 ERROR [CustomerHome] get failed
       java.lang.NullPointerException
       at swk.hibernate.dao.CustomerHome.findById(Unknown Source)
       at de.counter.CounterBean.<init>(CounterBean.java:69)


      My datasource:
      <datasources>
       <local-tx-datasource>
       <jndi-name>HelloWorldDS</jndi-name>
       <connection-url>jdbc:mysql://xxxx/xxxx</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>xxxx</user-name>
       <password>xxxx</password>
       <min-pool-size>5</min-pool-size>
       <max-pool-size>20</max-pool-size>
       </local-tx-datasource>
      </datasources>

      persistence.xml:
      <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="customerdb">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/HelloWorldDS</jta-data-source>
       <properties>
       <property name="hibernate.archive.autodetection" value="class, hbm"></property>
       <property name="hibernate.show_sql" value="true"/>
       <property name="hibernate.format_sql" value="true"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
       </properties>
       </persistence-unit>
      </persistence>
      

      and the entity with Stateless Home Class:
      CustomerEntity
      @Entity
      @Table(name = "Customer", catalog = "swkcustomerportal")
      public class Customer implements java.io.Serializable {
      
       private Integer idCustomer;
       private String jbossUser;
       ...
      
       public Customer() {
       }
      
       public Customer(String jbossUser, ...) {
       this.jbossUser = jbossUser;
       ...
       }
      
       public Customer(String jbossUser, ....)
       ...
       }
      
       @Id
       @GeneratedValue(strategy = IDENTITY)
       @Column(name = "idCustomer", unique = true, nullable = false)
       public Integer getIdCustomer() {
       return this.idCustomer;
       }
      
       protected void setIdCustomer(Integer idCustomer) {
       this.idCustomer = idCustomer;
       }
       ...
      
      CustomerHomeStateless
      @Stateless
      public class CustomerHome {
      
       private static final Log log = LogFactory.getLog(CustomerHome.class);
      
       @PersistenceContext(unitName="customerdb")
       private EntityManager entityManager;
      
       public void persist(Customer transientInstance) {
       ...
       }
      
       public void remove(Customer persistentInstance) {
       ...
       }
      
       public Customer merge(Customer detachedInstance) {
       ...
       }
      
       public Customer findById(Integer id) {
       log.debug("getting Customer instance with id: " + id);
       try {
      
       Customer instance = (Customer)entityManager.find(Customer.class, id);
       log.debug("get successful");
       return instance;
       } catch (RuntimeException re) {
       log.error("get failed", re);
       throw re;
       }
       }
      }

      I shorted the Classes to give an easier overview.
      The Customer and the CustomerHome are in different packages of the same jar File. I thought that it is no problem.

      Jboss Console gives me the following information:
      15:28:33,054 INFO [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.entity.PersistenceUnitDeployment
       15:28:33,070 INFO [JmxKernelAbstraction] installing MBean: persistence.units:jar=HibernateCustomer.jar,unitName=customerdb with dependencies:
       15:28:33,070 INFO [JmxKernelAbstraction] jboss.jca:name=HelloWorldDS,service=DataSourceBinding
       15:28:33,086 INFO [PersistenceUnitDeployment] Starting persistence unit persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
       15:28:33,164 INFO [Version] Hibernate EntityManager 3.2.1.GA
       15:28:33,164 INFO [Version] Hibernate Annotations 3.2.1.GA
       15:28:33,351 INFO [Ejb3Configuration] found EJB3 Entity bean: swk.hibernate.model.Customer
       ...
       15:28:34,773 INFO [HbmBinder] Mapping class: swk.hibernate.model.Customer -> Customer
       ...
       15:28:36,273 INFO [SessionFactoryObjectFactory] Factory name: persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
       15:28:36,273 INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
       15:28:36,289 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
       15:28:36,289 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
       15:28:36,289 INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}
       15:28:36,742 INFO [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.stateless.StatelessContainer
       15:28:37,039 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:jar=HibernateCustomer.jar,name=CustomerHome,service=EJB3 with dependencies:
       15:28:37,039 INFO [JmxKernelAbstraction] persistence.units:jar=HibernateCustomer.jar,unitName=customerdb
       15:28:37,055 INFO [EJBContainer] STARTED EJB: swk.hibernate.dao.CustomerHome ejbName: CustomerHome
       15:28:37,055 INFO [JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.stateless.StatelessContainer
       ...
       15:28:37,289 INFO [EJB3Deployer] Deployed: file:/D:/server/jboss-portal-2.7.2/server/default/deploy/HibernateCustomer.jar


      My portlet does the following:
      Customer customers;
       ...
      
       public CounterBean() {
       ...
       try {
       customers = (Customer) new CustomerHome().findById(1);
       ...


      I must omit something, or I did not understand it correctly.
      Can someone help me?


        • 1. Re: How to find Entity by Id
          jaikiran pai Master

           

          new CustomerHome()


          CustomerHome is a @Stateless bean. You are not supposed to instantiate a EJB using the new operator. Instead you should lookup the bean from JNDI. See this for more details http://www.jboss.org/index.html?module=bb&op=viewtopic&t=137590#4158957

          • 2. Re: How to find Entity by Id
            Gabriel Muench Newbie

            I couldnt find a JNDIView where my CustomerHome is listed, so i took CustomerHome because of the console output where the ejbname is CustomerHome.
            Where could i proof the name of the jndi name for my CustomerHome?

            I made a lookup in the client.

            Context context = new InitialContext();
            CustomerHome customer = (CustomerHome)context.lookup("CustomerHome");
            customer.findById(1);


            And become the Error Message:
            ERROR [JBossInjectionProvider] Injection failed on managed bean.
             javax.naming.NameNotFoundException: de.counter.CounterBean not bound
             at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
            

            So I thought, jbossinjectionprovider doesnt know where the managedbean has to lookup. That way i gave it these properties.
            Properties properties = new Properties();
            properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
            properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
            properties.put("java.naming.provider.url","localhost:1099");


            Now, i am getting nothing. no error, no success message.
            If i cancel my changes (the properties or give a other jndi name) it is always the same, no answers from the console.
            Sorry, but that has completly confused me.



            • 3. Re: How to find Entity by Id
              Wolfgang Knauf Master

              Hi,

              the JNDIView is here: http://www.jboss.org/community/wiki/DisplayTheJDNITreeWithTheJMXConsole

              The default JNDI name of "CustomerHome" is "EarName/JarName/Beanclass" if the the app is deployed as EAR file. If it is a JAR file, it is "JarName/Beanclass".

              You don't need JNDI properties if your portal webapp is running in the same JBoss instance as the EJBs.

              Hope this helps

              Wolfgang

              • 4. Re: How to find Entity by Id
                Gabriel Muench Newbie

                Thanks Wolfgang Knauf and jaikiran. That helped me much. Know its working fine.