title: Override EntityManager.remove() in Spring Roo
body: <p>I would like to override the <code>EntityManager.remove()</code> method for certain entities so that I can set an <code>active</code> boolean attribute to <code>false</code> rather than remove the object from the database entirely. This can be seen as a soft delete.</p>
  
  <p>The remove method in the <code>Entity_Roo_Jpa_ActiveRecord.aj</code> file for my class (called <code>Entity</code>, used as a superclass) looks like:</p>
  
  <pre><code>@Transactional
  public void remove() {
      if (this.entityManager == null) this.entityManager = entityManager();
      if (this.entityManager.contains(this)) {
          this.entityManager.remove(this);
      } else {
          Entity attached = Entity.findEntity(this.id);
          this.entityManager.remove(attached);
      }
  }
  </code></pre>
  
  <p>I've found the definition of which <code>EntityManagerFactory</code> to use in <code>META-INF/spring/applicationContext.xml</code>:</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>Would it be possible to subclass <code>LocalContainerEntityManagerFactoryBean</code> and provide my own <code>EntityManager</code> for a certain class, or is there a better way?</p>
  
  <p>I've also noticed the use of <code>@PersistenceContext</code> to declare an attribute for the EntityManager:</p>
  
  <pre><code>@PersistenceContext
  transient EntityManager Entity.entityManager;
  </code></pre>
  
  <p>But I suspect this is merely to store the EntityManager object and not to specify which implementation class to use (since EntityManager is an interface).</p>
  
  <p><strong>EDIT:</strong> The answer may lie in <a href="https://stackoverflow.com/questions/2421339/how-to-inject-jpa-entitymanager-using-spring">this answer</a>.</p>