sap-commerce-cloud

Inbound Persistence Hooks = >PrePersistHook: Getting old instance


The PrePersistHook offers this method

Optional<ItemModel> execute(ItemModel item);

When implementing the PrePersistHook the changed model is passed to the method. The executing path already open a transaction and it seems that the item is already persisted, but the transaction is not committed, yet.

If I try to get the same Item again from the database with flexible search, it returns the already updated object.

The documentation => https://help.sap.com/viewer/bad9b0b66bac476f8a4a5c4a08e4ab6b/v2005/en-US/028a2af06880407cb4b1c0624693dadd.html defines that one should not open transactions or create new threads.

But if it is not allowed, how is it possible to get the old version of the Model in a PrePersistHook to perform a validation or perform other check before the changes are persisted?

In our case, we want to create a new version of the OrderModel, but the persisted old version ever already have the old values. We see no opportunity to get the old version in a clean way.

Many standard prepersist hooks always fetch the item again from the database and return Option.empty() in the hook

Best Regards, Michael.


Solution

  • I found a solution that works for me. I override the DefaultModelEntityService and read the data before it's updated.