title: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
body: <p>No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call</p>
  
  <p>when I do a test with JUnit, persist method works and I see that my object is inserted, but when I call the method via my Controller doesn't work</p>
  
  <p>here is my Project :</p>
  
  <p>applicationContext.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:util="http://www.springframework.org/schema/util"
      xmlns:task="http://www.springframework.org/schema/task"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
          http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"&gt;
  
  
  
  &lt;!--    &lt;bean id="notification" class="com.app.sqli.notification.NotificationTask" /&gt; --&gt;
  
  &lt;!--    &lt;task:scheduled-tasks&gt; --&gt;
  &lt;!--        &lt;task:scheduled ref="notification" method="notifier" cron="*/2 * * * * *"/&gt; --&gt;
  &lt;!--    &lt;/task:scheduled-tasks&gt; --&gt;
  
  &lt;context:component-scan base-package="com.app.sqli" /&gt;  
  
      &lt;mvc:annotation-driven /&gt;
      &lt;bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
        &lt;property name="dataSource" ref="dataSource" /&gt;
         &lt;property name="packagesToScan" value="com.app.sqli.entities" /&gt;
        &lt;property name="jpaVendorAdapter"&gt;
           &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
        &lt;/property&gt;
        &lt;property name="jpaProperties"&gt;
           &lt;props&gt;
              &lt;prop key="hibernate.hbm2ddl.auto"&gt;validate&lt;/prop&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
           &lt;/props&gt;
        &lt;/property&gt;
     &lt;/bean&gt;
  
  
     &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
        &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
        &lt;property name="url" value="jdbc:mysql://localhost:3306/sqli" /&gt;
        &lt;property name="username" value="root" /&gt;
        &lt;property name="password" value="" /&gt;
     &lt;/bean&gt;
  
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
        &lt;property name="entityManagerFactory" ref="entityManagerFactoryBean" /&gt;
     &lt;/bean&gt;
  
  
     &lt;tx:annotation-driven /&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>my Model Class: </p>
  
  <pre><code>package com.app.sqli.models;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  
  @Entity
  public class Collaborateur {
  
      @Id
      private int id;
      private String nom;
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getNom() {
          return nom;
      }
  
      public void setNom(String nom) {
          this.nom = nom;
      }
  
  }
  </code></pre>
  
  <p>my DAO Class</p>
  
  <pre><code>package com.app.sqli.dao;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  
  import org.springframework.stereotype.Repository;
  
  import com.app.sqli.models.Collaborateur;
  
  @Repository
  public class CollaborateurDao implements IcollaborateurDao{
  
      @PersistenceContext
      private EntityManager em;
  
      @Override
      public void addCollaborateur(Collaborateur c) {
      em.persist(c);
  
      }
  
  }
  </code></pre>
  
  <p>My Service Class</p>
  
  <pre><code>package com.app.sqli.services;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.app.sqli.dao.IcollaborateurDao;
  import com.app.sqli.models.Collaborateur;
  
  @Service
  @Transactional
  public class CollaborateurService implements IcollaborateurService{
  
      @Autowired
      private IcollaborateurDao cdao;
  
      @Override
      public void addCollaborateur(Collaborateur c) {
          cdao.addCollaborateur(c);
  
      }
  
  
  }
  </code></pre>
  
  <p>And My Controller </p>
  
  <pre><code>package com.app.sqli.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import com.app.sqli.models.Collaborateur;
  import com.app.sqli.services.IcollaborateurService;
  
  @org.springframework.stereotype.Controller
  public class Controller {
      @Autowired
      private IcollaborateurService cserv;
  
      @RequestMapping(value = "/index")
      public String index(Model m) {
          System.out.println("insertion ...");
          Collaborateur c = new Collaborateur();
          c.setId(11);
          c.setNom("nom");
          cserv.addCollaborateur(c);
          return "index";
      }
  
  }
  </code></pre>