1 Reply Latest reply on Oct 25, 2012 12:50 PM by Prasobh K

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

    Ryan Jimmy Newbie

      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 programatically



      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.setDescription("testing Persistance");
      String status = "Failed";
      try { System.out.println("persistanceService " + persistanceService);
      status = "Success"; }
      catch (Exception e) {
      System.out.println("Error" + e);
      } return status;

      @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);
      finally {

      public Object getSingleResultSet(String namedQuery) {
      Query query = null;
      try { query = em.createNamedQuery(namedQuery); }
      finally {
      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" >
      <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />

      Can anyone 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 greatly appreciated.