title: Why JPA acts like my Entity is detached if I try removing it, but not if I edit it?
body: <p>So I have a basic JSF Datatable, the relavent part is:</p>
  
  <pre><code>&lt;h:dataTable value="#{actorTableBackingBean.allActors}" var="actor"&gt;
  
      &lt;h:column headerText="Actor Name" sortBy="#{actor.firstName}"&gt;
          &lt;h:outputText value="#{actor.firstName}" /&gt;
      &lt;/h:column&gt;
  
      &lt;h:column&gt;
          &lt;h:form&gt;
              &lt;h:commandButton value="Delete Actor"
                               action="#{actorTableBackingBean.deleteActor(actor)}"/&gt;
          &lt;/h:form&gt;
      &lt;/h:column&gt;
  
      &lt;h:column&gt;
          &lt;h:form&gt;
              &lt;h:commandButton value="Randomize Actor Name"
                               action="#{actorTableBackingBean.editActor(actor)}"/&gt;
          &lt;/h:form&gt;
      &lt;/h:column&gt;
  
  &lt;/h:dataTable&gt;
  </code></pre>
  
  <p>And this is what ActorTableBackingBean looks like:</p>
  
  <pre><code>@Named
  @RequestScoped
  public class ActorTableBackingBean implements Serializable {
  
      @Inject
      ActorDao actorDao;
  
      private List&lt;Actor&gt; allActors;
  
      public List&lt;Actor&gt; getAllActors() {
          return allActors;
      }
  
      @PostConstruct
      public void fillUp(){
          allActors = actorDao.getAllT();
      }
  
      public String deleteActor(Actor a){
          removeActor(a);
          return "/allActors.xhtml";
      }
  
      private String removeActor(Actor a){
          try{
              actorDao.deleteActor(a);
              return null;
          }catch (Exception e){
              return null;
          }
      }
  
      public String editActor(Actor actor){
          actor.setFirstName("SomeRandonName");
          actorDao.editActor(actor);
          return "/allActors.xhtml";
      }
  
  }
  </code></pre>
  
  <p>And finally ActorDao: </p>
  
  <pre><code>@Stateless
  public class ActorDao extends GeneralDao&lt;Actor&gt; implements Serializable {
  
      @Override
      protected Class&lt;Actor&gt; getClassType() {
          return Actor.class;
      }
  
      @Override
      public Actor getWithId(int id){
          TypedQuery&lt;Actor&gt; typedQuery =
                  em.createQuery("Select a From Actor a WHERE a.actorId =" + id,Actor.class);
          return typedQuery.getSingleResult();
      }
  
      public void editActor(Actor a){
          em.merge(a);
      }
  
      public void deleteActor(Actor a){
          em.remove(a);
      }
  
  }
  </code></pre>
  
  <p>So as you can see edit Actor calls <strong>em.merge(a)</strong> and this works just fine.
  However <strong>em.remove(a)</strong> will return:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: Entity must be managed to call remove: com.tugay.sakkillaa.model.Actor@6ae667f, try merging the detached and try the remove again.
  </code></pre>
  
  <p>Even if I try:</p>
  
  <pre><code> public void deleteActor(Actor a){
      em.merge(a); 
      em.remove(a);
   }
  </code></pre>
  
  <p>I am still getting the same exception.</p>
  
  <p>So how it works for editing the row, but not for deleting it? </p>
  
  <p>Only way I could make it work was:</p>
  
  <pre><code>public void deleteActor(Actor a){
      Actor actorToBeRemoved = getWithId(a.getActorId());
      em.remove(actorToBeRemoved);
  }
  </code></pre>
  
  <p>What is it that I am doing wrong, or not able to understand? </p>