0 Replies Latest reply on Nov 14, 2005 12:53 PM by marc lapierre

    EntityManager is null in @Stateless EJB using @PersistenceCo

    marc lapierre Newbie

      I can't seem to get an EntityManager persisted automatically in a @Stateless EJB... any help appreciated.

      TService.java

      package tester;
      
      import org.jboss.annotation.ejb.Management;
      import org.jboss.annotation.ejb.Service;
      
      @Service (objectName="testingt:service=testingt")
      @Management(TServiceInterface.class)
      public class TService implements TServiceInterface {
      
       public void doSomething() {
       TestEntityManager tem = new TestEntityManager();
       tem.listEntities();
      
       }
      
      }
      



      TestEntityManager.java
      package tester;
      
      import java.util.List;
      
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.Query;
      
      @Stateless
      public class TestEntityManager implements TestEntityManagerInterface {
      
       @PersistenceContext(unitName="Tester")
       EntityManager em;
      
       public List listEntities() {
       Query query = em.createQuery("from TestEntity testEntity");
       return query.getResultList();
       }
      }
      



      TestEntity.java
      package tester;
      
      import java.io.Serializable;
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratorType;
      import javax.persistence.Id;
      import javax.persistence.Table;
      
      @Entity
      @Table(name="testentity")
      public class TestEntity implements Serializable {
      
       public static final long serialVersionUID = 1L;
      
       private Integer id;
       private String name;
      
       public void setId(Integer id) {
       this.id = id;
       }
      
       @Id(generate=GeneratorType.AUTO)
       @Column(name="id")
       public Integer getId(){
       return this.id;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      
       @Column(name="name")
       public String getName(){
       return this.name;
       }
      }
      



      persistence.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <entity-manager>
       <name>Tester</name>
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/DefaultDS</jta-data-source>
       <properties>
       <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
       </properties>
      </entity-manager>