7 Replies Latest reply: Oct 25, 2012 3:18 PM by gimbal2 RSS

    @EJB and @PersistenceContext not working in JBOSS.5.1.0.GA?

    647345
      I am implementing a Rest Webservice Using RESTEasy and JPA for persistance and deploying in JBOSS.5.1.0.GA. JPA is working fine when we are getting EMF and EM programmatically.

      emf = Persistence.createEntityManagerFactory(Constants.GTL_ORACLE_PERSISTENCE_UNIT);
      em = emf.createEntityManager();

      But when I tried to inject EntityManager using @PersistenceContext . I am getting null value.

      Now we tried it using @Stateless EJB but that too failed in our service class.

      Our Service class is as follows:-

      public class ProductResourceService implements ProductResource {

      private boolean oneTime = false;
      @EJB(mappedName = "MyService")
      private PersistanceService persistanceService;


      @Override
      public String saveProduct(@PathParam("id") String id) {
      System.out.println("requset got id[" + id + "]");



      Product product = new Product();
      product.setName(id);
      product.setDescription("testing Persistance");
      String status = "Failed";
      try {
      System.out.println("persistanceService " + persistanceService);
      persistanceService.saveObject(product);

      status = "Success";
      } catch (Exception e) {
      System.out.println("Error" + e);
      }
      return status;
      }

      --------------------------------------------------------

      PersistanceService.java
      -------------------------------

      @Local(PersistanceServiceLocal.class)
      @Stateless(mappedName = "MyService")
      public class PersistanceService implements PersistanceServiceLocal {

      private EntityManager em;

      @PersistenceContext
      public void setEm(EntityManager em) {
      this.em = em;
      }


      public void saveObject(Object obj) {

      try {
      em.getTransaction().begin();
      em.persist(obj);
      em.getTransaction().commit();
      } finally {
      em.close();
      }

      }

      public Object getSingleResultSet(String namedQuery) {

      Query query = null;
      try {
      query = em.createNamedQuery(namedQuery);
      } finally {
      em.close();
      }
      if (query == null) {
      return null;
      }
      return query.getSingleResult();

      }

      I have persistence.xml configured in this way.

      <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="productDetails" transaction-type="JTA" >
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/OracleDS</jta-data-source>

      <class>com.gtl.cdi.rest.model.Product</class>
      <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
      <property name="hibernate.hbm2ddl.auto"
      value="create-drop" />

      </properties>


      </persistence-unit>

      Can you please tell me where I went wrong.I have been spending several days trying to resolve it....Is it that @PersistenceContext is not supported by JBOSS 5.1.0GA???

      Any help will be appreciated.

      Edited by: Ryan Jimmy on Oct 24, 2012 10:57 PM