java - JPA/Hibernate - Prevent deletion in PreRemove handler? -
java - JPA/Hibernate - Prevent deletion in PreRemove handler? -
the question title says all. possible in jpa/hibernate gracefully prevent deletion of entity database? flag entity "hidden" instead of removing it.
i want cascade
semantics preserved, such if seek delete entity owns collection of other entity, owning entity , every entity in collection marked hidden without work beingness necessary on part, beyond implementing @preremove
handler prevents deletion , marks entity hidden.
is possible, or need figure out other approach?
is possible in jpa/hibernate gracefully prevent deletion of entity database?
yes, long avoid using entitymanager.remove(entity)
possible. if utilize entitymanager.remove()
, jpa provider flag object deletion using corresponding sql delete statement implying elegant solution not possible 1 time flag object deletion.
in hibernate, can accomplish using @sqldelete
, @where
annotations. however, not play jpa, entitymanager.find()
known ignore filter specified in @where
annotation.
a jpa-only solution hence involve, adding flag i.e. column, in entity classes distinguish logically deleted entities in database "alive" entities. need utilize appropriate queries (jpql , native) ensure logically deleted entities not available in result sets. can utilize @preupdate
, @prepersist
annotations hook onto entity lifecycle events ensure flag updated on persist , update events. again, need ensure not invoke entitymanager.remove
method.
i have suggested using @preremove
annotation hook onto lifecycle event triggered removal of entities, using entity listener prevent deletion fraught problem reasons stated below:
sql delete
occurring in logical sense, need persist object in same transaction recreate it*. problem is not design decision reference entitymanager
in entitylistener, , inference invoke entitymanager.persist
in listener. rationale quite simple - might end obtaining different entitymanager reference in entitylistener , result in vague , confusing behavior in application. if need prevent sql delete
in transaction occurring, must throw exception in entitylistener. ends rolling transaction (especially if exception runtimeexception or application exception declared 1 causes rollbacks), , not offer benefit, entire transaction rolled back. if have alternative of using eclipselink instead of hibernate, appears elegant solution possible if define appropriate descriptorcustomizer
or using additionalcriteria
annotation. both of these appear work entitymanager.remove
, entitymanager.find
invocations. however, might still need write jpql or native queries business relationship logically deleted entities.
* outlined in jpa wikibook on topic of cascading persist:
if remove object have deleted, if phone call persist on object, resurrect object, , become persistent again. may desired if intentional, jpa spec requires behavior cascade persist. if remove object, forget remove reference cascade persist relationship, remove ignored.
java hibernate jpa
Comments
Post a Comment