title: LocalContainerEntityManagerFactoryBean does not persist with @Transactional
body: <p>I'm trying to create a project using Spring 4, Primefaces 5 and Hibernate JPA in Eclipse running into Wildfly 8.0 . My project runs fine right now. The only problem is that, <code>@Transactional</code> annotation doesn't work. I've tried this (as I saw in another SO question) and it works (This in my DAO):</p>
  <pre><code>@Repository(value=&quot;empleadoDAO&quot;)
  public class EmpleadoDAO {
      
      @Resource(name=&quot;myEmf&quot;)
      private EntityManagerFactory em;
      
      private EntityManager emf;
      
      public boolean insertarEmpleado(Empleado emp) {
          boolean res = true;
          emf = em.createEntityManager();
          emf.getTransaction().begin();
          emf.persist(emp);
          emf.getTransaction().commit();
          return res;
      }
  }
  </code></pre>
  <p>If I do that, it works, but if I use <code>@Transactional</code> annotations either in my DAO or in my Service, either using annotation from <code>javax.transaction</code> or using <code>springframework</code> annotation it doesn't work. It doesn't give me an exception or nothing but if I look into the database it doesn't insert anything (I'm using PostgreSQL 9.3).</p>
  <p>Also, currently I'm injecting EntityManagerFactory instead just EntityManager into my DAOs. I tried to inject EntityManager using <code>@PersistenceContext(unitName=&quot;myPersistenceUnit&quot;)</code> but it gives me this error:</p>
  <pre><code>JBAS016069: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named myPersistenceUnit in deployment sprisf2.war
  </code></pre>
  <p>Here is my <code>beans.xml</code> file:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
      xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
      xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
      xmlns:jee=&quot;http://www.springframework.org/schema/jee&quot;
      xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
      xmlns:task=&quot;http://www.springframework.org/schema/task&quot;
      xsi:schemaLocation=&quot;http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd&quot;&gt;
  
      &lt;context:spring-configured /&gt;
      &lt;context:annotation-config /&gt;
      &lt;task:annotation-driven /&gt;
      &lt;context:component-scan base-package=&quot;com.saplic.sprisf.daos&quot; /&gt;
      &lt;context:component-scan base-package=&quot;com.saplic.sprisf.managedbeans&quot; /&gt;
      &lt;context:component-scan base-package=&quot;com.saplic.sprisf.services&quot; /&gt;
      &lt;context:component-scan base-package=&quot;com.saplic.sprisf.entities&quot; /&gt;
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean id=&quot;myEmf&quot;
          class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;
          &lt;property name=&quot;dataSource&quot; ref=&quot;dbDs&quot;&gt;&lt;/property&gt;
          
          &lt;property name=&quot;persistenceUnitName&quot; value=&quot;myPersistenceUnit&quot; /&gt;
          &lt;property name=&quot;packagesToScan&quot; value=&quot;com.saplic.sprisf.entities&quot; /&gt;
          &lt;property name=&quot;jpaVendorAdapter&quot;&gt;
              &lt;bean class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot; /&gt;
          &lt;/property&gt;
          &lt;property name=&quot;jpaProperties&quot;&gt;
              &lt;props&gt;
                  &lt;prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;update&lt;/prop&gt;
                  &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/prop&gt;
                  &lt;prop key=&quot;hibernate.show_sql&quot;&gt;false&lt;/prop&gt;
                  &lt;prop key=&quot;hibernate.format_sql&quot;&gt;false&lt;/prop&gt;
                  &lt;prop key=&quot;hibernate.cache.provider_class&quot;&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;
                  
                  
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      
      &lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;
          &lt;property name=&quot;entityManagerFactory&quot; ref=&quot;myEmf&quot; /&gt;
          &lt;property name=&quot;dataSource&quot; ref=&quot;dbDs&quot; /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot; /&gt;
  
      &lt;jee:jndi-lookup id=&quot;dbDs&quot; jndi-name=&quot;java:jboss/datasources/postgreDS&quot;&gt;&lt;/jee:jndi-lookup&gt;
  
      &lt;mvc:resources location=&quot;/resources/&quot; mapping=&quot;/static/**&quot; /&gt;
  &lt;/beans&gt;
  </code></pre>
  <p>Previously it worked starting and committing the transaction in the code without having a <code>persistence.xml</code> file, but after that I decided to create one trying to inject <code>EntityManager</code> with <code>@PersistenceContext</code>.</p>
  <p>Here's my <code>persistence.xml</code>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;persistence xmlns=&quot;http://java.sun.com/xml/ns/persistence&quot;
      version=&quot;1.0&quot;&gt;
      &lt;persistence-unit name=&quot;myPersistenceUnit&quot;&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;properties&gt;
              &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.PostgreSQLDialect&quot; /&gt;
              &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;update&quot; /&gt;
              &lt;property name=&quot;hibernate.show_sql&quot; value=&quot;false&quot; /&gt;
              &lt;property name=&quot;hibernate.format_sql&quot; value=&quot;true&quot; /&gt;
              &lt;!-- property name=&quot;hibernate.max_fetch_depth&quot; value=&quot;3&quot; /--&gt;
              &lt;!-- property name=&quot;hibernate.order_updates&quot; value=&quot;true&quot; /--&gt;
              
              &lt;property name=&quot;hibernate.default_schema&quot; value=&quot;public&quot; /&gt;
              &lt;property name=&quot;hibernate.cache.provider_class&quot; value=&quot;org.hibernate.cache.EhCacheProvider&quot; /&gt;
              
              &lt;property name=&quot;hibernate.transaction.manager_lookup_class&quot; value=&quot;org.hibernate.transaction.JBossTransactionManagerLookup&quot; /&gt;
              &lt;property name=&quot;jboss.entity.manager.factory.jndi.name&quot; value=&quot;java:jboss/datasources/postgreDS&quot; /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  <p>I referenced this <code>persistence.xml</code> using this property in my <code>LocalContainerEntityManagerFactoryBean</code>:</p>
  <pre><code>&lt;property name=&quot;persistenceXmlLocation&quot; value=&quot;/META-INF/persistence.xml&quot; /&gt;
  </code></pre>
  <p>At least I would like to persist my data using <code>@Transactional</code> in my services without having to start and commit the transaction through code, and it would be great if I can inject <code>EntityManager</code> instead <code>EntityManagerFactory</code>.</p>
  <p>[EDIT] Here are my files <code>EmpleadoManagedBean</code> and <code>EmpleadoService</code> from which I call the DAO method for insert:</p>
  <pre><code>@Component(value=&quot;empleadoMB&quot;)
  @ManagedBean(name=&quot;empleadoMB&quot;)
  @ConversationScoped
  public class EmpleadoManagedBean implements Serializable {
      
      @Resource(name=&quot;empleadoService&quot;)
      private EmpleadoService empleadoService;
      
      private Empleado instance = new Empleado();
      
           public String addEmpleado() {
               empleadoService.insertarEmpleado(instance);
               return &quot;SUCCESS&quot;;
           }
  }
  </code></pre>
  <p>I am sure that instance object is with the necessary data previous to call <code>insertarEmpleado</code>. And here's my <code>Service</code> class:</p>
  <pre><code>@Service(&quot;empleadoService&quot;)
  @Transactional
  public class EmpleadoService {
  
      @Resource(name=&quot;empleadoDAO&quot;)
      private EmpleadoDAO empleadoDAO;
      
      @Transactional
      public void insertarEmpleado(Empleado emp) {
          empleadoDAO.insertarEmpleado(emp);
      }
  }
  </code></pre>
  <p>[EDIT] By seeing some examples, I saw that some people just declare <code>&lt;tx:annotation-driven /&gt;</code> and then a bean named <code>transactionManager</code>. I changed the name of the bean to <code>txManager</code> and it gave me an error indicating that there's no bean with id <code>transactionManager</code>, so I think that it injects into the Services but for some reason the annotation doesn't work.</p>