Class SplibEditJpaService<F extends SplibEditForm, E extends jp.ecuacion.lib.jpa.entity.EclibEntity>
- Type Parameters:
F- SplibEditFormE- AbstractEntity
- All Implemented Interfaces:
SplibJpaServiceInterface<E>
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected <T extends jp.ecuacion.lib.jpa.entity.EclibEntity>
TinsertOrUpdate(SplibRepository<T, ?> repo, T e) Offers a utility function to upsert database safely in the library.Methods inherited from class SplibEditService
edit, getInsertPage, getUpdatePage, isInsert, pageMethods inherited from class SplibGeneral1FormService
prepareForm, prepareFormMethods inherited from class SplibGeneralService
getParams, throwWarningMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface SplibJpaServiceInterface
findAndOptimisticLockingCheck, getRepositoryForOptimisticLocking, getVersionsForOptimisticLocking
-
Field Details
-
em
protected jakarta.persistence.EntityManager em
-
-
Constructor Details
-
SplibEditJpaService
public SplibEditJpaService()
-
-
Method Details
-
insertOrUpdate
protected <T extends jp.ecuacion.lib.jpa.entity.EclibEntity> T insertOrUpdate(SplibRepository<T, ?> repo, T e) Offers a utility function to upsert database safely in the library.In jpa or spring data jpa world, what you have to do to insert is to
repository.save(e).
And no need to do anything to update thanks to "dirty checking" (jpa automatically detects and updates changes of managed entities on commit).But the feature of the library conflicts with the dirty checking feature.
The library supports "soft delete flag" feature, which should delete key-duplicated soft-deleted record on insert or update.
We can hook "insert" and delete duplicated soft-deleted record because "save()" method is always called (it's done inSplibSoftDeleteAdvice), but "update" we cannot because no method is called during the update derived from "dirty checking".
To prevent primary key duplication (which does not happen in principle because the library needs surrogate key strategy) or unique key dupliication on update in the library environment, we need to usesave()to realize hook.
But it's not enough because when you callsave(), updates derived from "dirty checking" processed right before the hook procedure processed. That's why in this method "detach()" firstly to disable "dirty checking", then save to prevent key duplication on update.
Strictly speaking,detach()is not needed on insert, but "insertOrUpdate" method is created for simplicity.
-