Closed skuntsel closed 6 years ago
+1
Only change I'd like to see is that SoftDeleteType.DELETED is the default, not ACTIVE. It's more self-documenting and booleans themselves also default to false.
Yes, that's probably more convenient to set DELETED as the default type. Also, several supplementary methods might be refactored to the utility library not to mix reflection code in the service.
It could also make sense to add a couple of helper methods to deal with active entities in the paging section of the service class, i.e. preset additional parameter to criteria queries holding the state.
I've as per https://github.com/omnifaces/omnipersistence/commit/a7b50b2196416e5fc55164508a11a179b8867123 inverted "Active" with "SoftDeleted" and made the default behavior of getById(), findById() and getAll() to exclude soft deleted ones.
Thank you for your contribution!
Added functionality for soft deletable entities.
In some applications there are requirements that the entities cannot be deleted but rather inactivated, or soft deleted. To account for such requirements a new annotation is introduced,
@SoftDeletable
, where you specify the type of soft delete column.Herewith two settings are proposed,
SoftDeleteType.ACTIVE
(where you've got anactive
column in the database separating active entities from soft deleted ones) andSoftDeleteType.DELETED
(where you've got andeleted
column in the database separating soft deleted entities from active ones). When you place the@SoftDeletable
annotation on any persistent field ofboolean
type, theBaseEntityService
will provide for helper methods to soft delete/undelete and get active entity/entities. When these methods are invoked for an entity with no fields marked as soft deleted, aNonSoftDeletableEntityException
will be thrown.Example setup:
and
Example usage:
Also, behaviour for calling
BaseEntityService#save
method was modified for entities which ids are not autogenerated. Now, calling this method on such entities doesn't throwIllegalEntityStateException
but rather callsBaseEntityService#persist
orBaseEntityService#update
basing on the result ofBaseEntityService#exists
method call, thus there is no need to manually define which method to call.This is now the allowed behaviour:
Test cases for these scenarios were also created.
As a remark, for this to work correctly on the
@MappedSuperclass
-derived entities as well, theReflections#accessField
of the omniutils library has to be modified to account for superclass fields as well, i.e. to something like: