Class SplibEditJpaService<F extends SplibEditForm, E extends jp.ecuacion.lib.jpa.entity.EclibEntity>

Type Parameters:
F - SplibEditForm
E - AbstractEntity
All Implemented Interfaces:
SplibJpaServiceInterface<E>

@Transactional(rollbackFor=Exception.class) public abstract class SplibEditJpaService<F extends SplibEditForm, E extends jp.ecuacion.lib.jpa.entity.EclibEntity> extends SplibEditService<F> implements SplibJpaServiceInterface<E>
Provides abstract edit service.
  • 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 in SplibSoftDeleteAdvice), 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 use save() to realize hook.
      But it's not enough because when you call save(), 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.