3 Replies Latest reply on Aug 28, 2012 8:05 AM by Wolf-Dieter Fink

    java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

    John Scott Newbie

      I'm developing my first enterpise application with jpa ejb ecc

       

      when i try to save an object i get this error  : java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

       

      this is my persistance.xml

       

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.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_2_0.xsd">
          <persistence-unit name="jpa"  transaction-type="RESOURCE_LOCAL">
              <provider>org.hibernate.ejb.HibernatePersistence</provider>
               <jta-data-source>java:jboss/newDataSource</jta-data-source>
      
              <properties>
                  <property name="hibernate.hbm2ddl.auto" value="create-drop"/>     
              </properties>
      
          </persistence-unit> 
      </persistence>
      

       

      and this is my bean

      package exampleBean;
      
      import javax.ejb.Stateless;
      import javax.ejb.TransactionAttribute;
      import javax.ejb.TransactionAttributeType;
      import javax.ejb.TransactionManagement;
      import javax.ejb.TransactionManagementType;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.transaction.TransactionManager;
      
      import pojo.Privato;
      
      import exampleInterface.ManagedPrivato;
      
      @Stateless
      public class ManagedPrivatoBean implements ManagedPrivato {
      
      
          @PersistenceContext(unitName="jpa")
          EntityManager em;
      
      
          @Override
          public void savePrivato(String nome , String cognome) {
              // TODO Auto-generated method stub
              Privato pr = new Privato();
              pr.setNome(nome);pr.setCognome(cognome);
      
              em.getTransaction().begin();
              em.persist(pr);
              em.getTransaction().commit();
      
              System.out.println("Utente Salvato");
      
      
          }
      
          @Override
          public void updatePrivato() {
              // TODO Auto-generated method stub
      
          }
      
          @Override
          public void deletePrivato() {
              // TODO Auto-generated method stub
      
          }
      
      }