title: Remove then Query fails in JPA/Hibernate (deleted entity passed to persist)
body: <p>I've got a problem with the removal of entities in my JPA application: basically, I do in this EJB Business method:</p>
  
  <pre><code>load photo list ;
  for each photo {
      //UPDATE
      remove TagPhoto element from @OneToMany relation
      //DISPLAY
      create query involving TagPhoto
      ...
  }
  </code></pre>
  
  <p>and this last query always throws an EntityNotFoundException (deleted entity passed to persist: [...TagPhoto#])</p>
  
  <p>I think I understand the meaning of this exception, like a synchronization problem caused by my Remove, but how can I get rid of it? </p>
  
  <p>EDIT: here is the stack of the exception:</p>
  
  <pre><code>Caused by: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [net.wazari.dao.entity.TagPhoto#&lt;null&gt;]
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:621)
      at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:74)
      at net.wazari.dao.jpa.TagFacade.loadVisibleTags(TagFacade.java:108)
  </code></pre>
  
  <p>and the mapping between Tag-TagPhoto-Photo</p>
  
  <pre><code>public class Tag implements Serializable {
      ...
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "tag")
      private List&lt;TagPhoto&gt; tagPhotoList;
  }
  public class TagPhoto implements Serializable {
      ...
      @JoinColumn(name = "Tag", referencedColumnName = "ID")
      @ManyToOne(optional = false)
      private Tag tag;
      @JoinColumn(name = "Photo", referencedColumnName = "ID")
      @ManyToOne(optional = false)
      private Photo photo;
  }
  public class Photo implements Serializable {
      ...
      @OneToMany(cascade = CascadeType.ALL , mappedBy = "photo")
      private List&lt;TagPhoto&gt; tagPhotoList;
  }
  </code></pre>
  
  <p>(it was automatically generated by Netbeans when I created the project)</p>
  
  <p>EDIT: Does it mean that <code>tagPhoto != tagPhoto.getTag().getTagPhotoList().get(...) != tagPhoto.getPhoto().getTagPhotoList().get(...)</code>? </p>
  
  <p>and how shall I remove them? <code>iterator.remove</code> should not be of any use, and I would think that three <code>em.remove()</code> would do three times the same operation ...</p>