I have the following setup: there various entities (Projects, Tests, Users) and then there are TestReports. The TestReports reference the other entities and also some extra data in filesystem.
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 [deleted id123] instead of deleted entity label and it would be fine. However, I am getting javax.persistence.EntityNotFoundException, when I try to load a report which references deleted entity.
Is there a way to tell Hibernate/JPA to just create phantom object with ID and some isDeleted flag in such a case?
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.
From the Envers documentation:
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.
I am aware of workaround by introducing deleted flag on all objects and use if for soft deletion, instead of actual removal. But this adds an extra overhead, which I would like to avoid.
I’m not sure if this is what you’re looking. You can tell hibernate to set a null on the field rather than trowing an EntityNotFoundException, by using the
@NotFound(action=NotFoundAction.IGNORE)annotation. There isn’t much documentation about it, you can find how to use it here.