title: EntityManager cannot use persist to save element to database
body: <p>I met the problem of persisting element to database using EntityManager. Based on the  answers I found, I tried those 4 ways in my DaoJpa to do such thing but still failed. Here I attached the four ways I tried:</p>
  
  <p>Code in Controller part:</p>
  
  <pre><code>   @Transactional 
     SmartProduct smartProduct = new SmartProduct();
              smartProduct.setName("Dove Soap");
              smartProductDao.persist(smartProduct);
  </code></pre>
  
  <p>1.
      DaoJpa:</p>
  
  <pre><code> @Transactional
   public void persist(SmartProduct smartProduct) {
              entityManager.persist(smartProduct);
  </code></pre>
  
  <p>Doesn't work!</p>
  
  <p>2.</p>
  
  <pre><code>@Transactional
  public void persist(SmartProduct smartProduct) {
  entityManager.persist(smartProduct);
  entityManager.flush();
  </code></pre>
  
  <blockquote>
    <p>Exception I got: no transaction is in progress</p>
  </blockquote>
  
  <p>3.</p>
  
  <pre><code>@Transactional
  public void persist(SmartProduct smartProduct) {
  EntityTransaction emTransaction = entityManager.getTransaction();
          emTransaction.begin();  
          entityManager.persist(smartProduct);
          emTransaction.commit();
          entityManager.close();
  </code></pre>
  
  <blockquote>
    <p>Exception I got:
        Not allowed to create transaction on shared EntityManager - use Spring 
        transactions or EJB CMT instead</p>
  </blockquote>
  
  <p>4.</p>
  
  <pre><code>@Transactional
  public void persist(SmartProduct smartProduct) {
                      EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
                  EntityManager em = emf.createEntityManager();
                  EntityTransaction etx = em.getTransaction();
                  etx.begin();
                  em.persist(smartProduct);
                  etx.commit();
                  em.close();
                  emf.close();
  </code></pre>
  
  <blockquote>
    <p>Exception I got:
        The application must supply JDBC connections</p>
  </blockquote>
  
  <p>Could someone help me  figure out the problem please? Many thanks in advance! </p>
  
  <p>Many thanks JustinKSU's help. I add the annotation in Spring context and then it solved!
  Here is the previous version of my Spring context:</p>
  
  <pre><code>&lt;bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" /&gt;
  
      &lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"&gt;
          &lt;property name="persistenceUnitName" value="persistenceUnit" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>After added the </p>
  
  <pre><code>&lt;tx:annotation-driven /&gt;
  </code></pre>
  
  <p>it works:</p>
  
  <pre><code>&lt;tx:annotation-driven /&gt;
      &lt;bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" /&gt;
  
      &lt;bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"&gt;
          &lt;property name="persistenceUnitName" value="persistenceUnit" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  </code></pre>