title: why JPA can save my entity but It can't delete the entity?
body: <p>In my JSF2-JPA2-Spring3 project, I can insert new entities but can't remove entities. This is the error message: java.lang.IllegalArgumentException: Removing a detached instance entity.Entity#8</p>
  
  <p>This is my persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  </code></pre>
  
  <p>http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
      version="1.0"></p>
  
  <pre><code>&lt;persistence-unit name="myPersistenceUnit"
      transaction-type="RESOURCE_LOCAL"&gt;
      &lt;properties&gt;            
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
          &lt;property name="hibernate.show_sql" value="true" /&gt;
      &lt;/properties&gt;   
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is my service:</p>
  
  <pre><code>@Service("myService")
  </code></pre>
  
  <p>public class MyServiceImpl implements MyService {</p>
  
  <pre><code>@Resource(name="MyRepository")
  MyDAO myDao;
  
  @Transactional
  public void deleteEntity(Entity entity) throws DAOException {
      myDao.delete(entity);
  }
  </code></pre>
  
  <p>This is my dao:</p>
  
  <pre><code>@Repository("MyRepository")
  </code></pre>
  
  <p>public class UserDAO{</p>
  
  <pre><code>private EntityManager entityManager;
  
  @PersistenceContext
  public void setEntityManager(EntityManager entityManager) {
      this.entityManager = entityManager;
  }
  
  public void delete(Entity entity) throws Exception {
  
      try {
          entityManager.remove(entity);
      } catch (DataAccessException e) {
          throw new Exception(e);
      }
  }
  </code></pre>
  
  <p>This is applicationContext.xml:</p>
  
  <pre><code>&lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" &gt;
  
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="dataSource" ref="myDataSource" /&gt;
      &lt;property name="persistenceUnitName" value="myPersistenceUnit"/&gt; 
  &lt;/bean&gt;
  
  &lt;bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;     
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven /&gt;
  &lt;tx:annotation-driven  transaction-manager="transactionManager"/&gt;
  
  
  &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;tx:advice id="transactionInterceptor" transaction-manager="transactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="*" rollback-for="Throwable" /&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  &lt;aop:config&gt;
      &lt;aop:advisor pointcut=" execution(* service.*Service.*(..))"
          advice-ref="transactionInterceptor" /&gt;
  &lt;/aop:config&gt;
  </code></pre>
  
  <p>I try to remove an entity from a list that feed using this method in dao:</p>
  
  <pre><code>public List&lt;Entity&gt; getAll() throws Exception {
      List&lt;Entity&gt; list = null;
  
      try {
            list = entityManager.createQuery("Select e from Entity e").getResultList();
      } catch (DataAccessException e) {
          throw new Exception(e);
      }
  
      return list;
  }
  </code></pre>