2 Replies Latest reply on Mar 8, 2007 5:40 AM by lgsw_sam

    entityManager is null

    lgsw_sam

      Hi!
      using jbossas 4.0.5.ga-ejb3 and hibernate 3.2.

      I keep getting nullPointer exception with this class. I have checked that entityManager is null.

      same kind of injecting the PersistenceContext works fine with several other classes..

      I have tried without unitName but we have several persistence units so that wont work.


      Can anyone help me to find the cause?

      package fi.logiasoftware.messageserver.services.backend;
      
      import java.util.List;
      
      import javax.ejb.Local;
      import javax.ejb.Remote;
      import javax.ejb.Stateless;
      import javax.ejb.TransactionAttribute;
      import javax.ejb.TransactionAttributeType;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import org.jboss.annotation.ejb.LocalBinding;
      import org.jboss.annotation.ejb.RemoteBinding;
      
      import fi.logiasoftware.messageserver.config.ActionException;
      import fi.logiasoftware.messageserver.config.Destination;
      
      @Stateless
      @RemoteBinding(jndiBinding = "ActionExceptionHandling")
      @LocalBinding(jndiBinding = "ActionExceptionHandlingLocal")
      @Remote(ActionExceptionHandling.class)
      @Local(ActionExceptionHandling.class)
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public class ActionExceptionHandlingBean implements ActionExceptionHandling {
      
       @PersistenceContext(unitName = "MessageServer")
       private EntityManager entityManager;
      
       private ActionException ae;
       private List<ActionException> ActionExceptions;
      
       public void setActionException(String filename, Destination destination, String hash){
       ae = new ActionException();
       ae.setDestination(destination);
       ae.setFilename(filename);
       ae.setHash(hash);
       entityManager.persist(ae);
       }
      
      
       @SuppressWarnings("unchecked")
       public List<ActionException> getActionExceptions(){
       System.out.println("getActionExceptions ploo");
      
       if(entityManager != null) {
       ActionExceptions = entityManager.createQuery("from ActionException a order by a.id").getResultList();
       System.out.println("getActionExceptions loppuu");
       } else {
       System.out.println("entityManager on null");
       }
      
       return ActionExceptions;
       }
      
       public void removeActionException(ActionException a){
       entityManager.remove(a);
       }
      
      }
      


      persistence.xml

      <persistence-unit name="MessageServer" transaction-type="jta">
      
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/MessageServicesDS</jta-data-source>
      
       <properties>
       <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
       <!-- property name="hibernate.hbm2ddl.auto" value="create"/ -->
       <property name="hibernate.session_factory_name" value="ServiceConfig"/>
       <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
       <property name="hibernate.jdbc.use_get_generated_keys" value="true"/>
       <property name="hibernate.order_updates" value="true"/>
       <property name="hibernate.jdbc.batch_versioned_data" value="true"/>
       <property name="jta.UserTransaction" value="UserTransaction"/>
       <property name="hibernate.current_session_context_class" value="jta"/>
       <property name="hibernate.query.jpaql_strict_compliance" value="false"/>
       <property name="hibernate.show_sql" value="false"/>
       <property name="hibernate.format_sql" value="true"/>
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactory"/>
       <property name="jboss.entity.manager.jndi.name" value="java:/EntityManager"/>
       <property name="hibernate.connection.release_mode" value="auto"/>
       <!-- property name="hibernate.cache.provider_class" value="org.hibernate.cache.OptimisticTreeCacheProvider"/ -->
       <!-- property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache"/ -->
       <!-- property name="hibernate.transaction.auto_close_session" value="true"/ -->
       <!-- property name="hibernate.cache.use_query_cache" value="true"/ -->
       </properties>
      
       <!-- property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/ -->
       </persistence-unit>
      


      Br,
      Sami Männistö

        • 1. Re: entityManager is null
          wolfgangknauf

          Hi Sami,

          your bean and persistence.xml in the same jar file ?

          Your persistence.xml was probably only a snippet ? I miss the xml document root "persistence":

          <?xml version="1.0" encoding="UTF-8"?>
          <persistence xmlns="http://java.sun.com/xml/ns/persistence"
           <persistence-unit name="MessageServer" transaction-type="jta">
           ...
           </persistence-unit>
          </persistence>


          Hope this helps

          Wolfgang

          • 2. Re: entityManager is null
            lgsw_sam

            Hi Wolfgang,

            problem solved.

            I had a bad "typo" on the bean calling this bean..
            So nothing wrong wit persistence.xml or ActionExceptionHandlingBean...

            thanks for your help!

            Br,
            Sami Männistö