cancel
Showing results for 
Search instead for 
Did you mean: 

Inbound Persistence Hooks = >PrePersistHook: Getting old instance

mwoelm
Participant
0 Kudos

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/028a2af06880407cb4b1c062469...
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.

Accepted Solutions (1)

Accepted Solutions (1)

mwoelm
Participant
0 Kudos

I found a solution that works for me. I override the DefaultModelEntityService and read the data before it's updated. This won't solve every use case, but it was sufficient for mine.

romitchhabra
Participant
0 Kudos

May be if you use validate interceptor, then you may get the old model from the database.

mwoelm
Participant
0 Kudos

I haven't it tested explicitly, but when I think about it, I didn't want to add a new version whenever the model service tries to validate a model save is performed. At least in my use case I try to check only if something is changed via the Integrated Objects API.

However, of course, your suggestion could be a solution for other use cases.

Answers (0)