title: Referencing deleted entities in JPA/Hibernate
body: <p>I have the following setup: there various entities (<code>Projects</code>, <code>Tests</code>, <code>Users</code>) and then there are <code>TestReports</code>. The <code>TestReports</code> reference the other entities and also some extra data in filesystem.</p>
  
  <p>The reports are generated and are never deleted (for the auditing purposed). However, the other entities can be deleted. This leads to situation, when report references some deleted entity. This is OK from business point of view, the report could just display <code>[deleted id123]</code> instead of deleted entity label and it would be fine. However, I am getting <code>javax.persistence.EntityNotFoundException</code>, when I try to load a report which references deleted entity.</p>
  
  <p>Is there a way to tell Hibernate/JPA to just create phantom object with ID and some <code>isDeleted</code> flag in such a case?</p>
  
  <p>In Hibernate Envers documentation, I have found they support something like that when traversing the revision history, but I haven't find a way to use it in 'normal' entities.</p>
  
  <p><strong>From the Envers documentation:</strong></p>
  
  <blockquote>
    <p>The second parameter, selectDeletedEntities, specifies if revisions, in which the entity was deleted should be included in the results. If yes, such entities will have the revision type DEL and all fields, except the id, null.</p>
  </blockquote>
  
  <p>I am aware of workaround by introducing <code>deleted</code> flag on all objects and use if for <em>soft</em> deletion, instead of actual removal. But this adds an extra overhead, which I would like to avoid.</p>