I'm working with a PrePersistenceHook in SAP Commerce (Hybris), where I need to access the old values of a ProductModel before saving updates. My goal is to compare the old and new values of specific attributes (e.g., name and description) before persisting changes.
What I've Tried: Fetching the old product using productService:
ProductModel oldProduct = productService.getProductForCode(productToInsert.getCode());
However, this returns the new values instead of the old ones, possibly due to caching.
Using modelService.refresh(oldProduct):
modelService.refresh(oldProduct);
This results in unexpected behavior: both productToInsert and oldProduct now contain the old values. It seems like they share the same reference.
Detaching the oldProduct before Refreshing and Creating a temporary instance before refreshing:
//ProductModel tempProduct = (ProductModel) item; modelService.detach(oldProduct); modelService.refresh(oldProduct);
However, assigning tempProduct to productToInsert leads to a "duplicate primary key" error.
public Optional<ItemModel> execute(ItemModel item, PersistenceContext context) {
if (item instanceof ProductModel productToInsert) {
ProductModel oldProduct = productService.getProductForCode(productToInsert.getCode());
productToInsert);
modelService.refresh(oldProduct); // Force fetch old values from DB
if (oldProduct != null) {
if ((StringUtils.isNotBlank(productToInsert.getName())
&& oldProduct.getName().equals(cleanNameOrDesc(productToInsert.getName())))
|| (StringUtils.isNotBlank(productToInsert.getDescription())
&& oldProduct.getDescription().equals(cleanNameOrDesc(productToInsert.getDescription())))) {
productToInsert.setApprovalStatus(ArticleApprovalStatus.CHECK);
}
}
}
}
Question: I want to retrieve the old values of the product from the database and compare them with the new values before saving. How can I correctly retrieve the old values of a ProductModel before persisting updates in a PrePersistenceHook? Is there a way to bypass the cache and fetch a detached version of the entity?
I'm working with a PrePersistenceHook in SAP Commerce (Hybris), where I need to access the old values of a ProductModel before saving updates. My goal is to compare the old and new values of specific attributes (e.g., name and description) before persisting changes.
What I've Tried: Fetching the old product using productService:
ProductModel oldProduct = productService.getProductForCode(productToInsert.getCode());
However, this returns the new values instead of the old ones, possibly due to caching.
Using modelService.refresh(oldProduct):
modelService.refresh(oldProduct);
This results in unexpected behavior: both productToInsert and oldProduct now contain the old values. It seems like they share the same reference.
Detaching the oldProduct before Refreshing and Creating a temporary instance before refreshing:
//ProductModel tempProduct = (ProductModel) item; modelService.detach(oldProduct); modelService.refresh(oldProduct);
However, assigning tempProduct to productToInsert leads to a "duplicate primary key" error.
public Optional<ItemModel> execute(ItemModel item, PersistenceContext context) {
if (item instanceof ProductModel productToInsert) {
ProductModel oldProduct = productService.getProductForCode(productToInsert.getCode());
productToInsert);
modelService.refresh(oldProduct); // Force fetch old values from DB
if (oldProduct != null) {
if ((StringUtils.isNotBlank(productToInsert.getName())
&& oldProduct.getName().equals(cleanNameOrDesc(productToInsert.getName())))
|| (StringUtils.isNotBlank(productToInsert.getDescription())
&& oldProduct.getDescription().equals(cleanNameOrDesc(productToInsert.getDescription())))) {
productToInsert.setApprovalStatus(ArticleApprovalStatus.CHECK);
}
}
}
}
Question: I want to retrieve the old values of the product from the database and compare them with the new values before saving. How can I correctly retrieve the old values of a ProductModel before persisting updates in a PrePersistenceHook? Is there a way to bypass the cache and fetch a detached version of the entity?
Share Improve this question edited Feb 5 at 17:09 Drew Reese 203k17 gold badges236 silver badges268 bronze badges asked Feb 5 at 12:39 aanssaienaanssaien 31 silver badge1 bronze badge1 Answer
Reset to default 1The correct way to get the previous values of a modified ItemModel in SAP Commerce (Hybris) is to use the ItemModelContext like this:
item.getItemModelContext().getOriginalValue("attribute")
Or if you need to get the value for a specific locale instead of the session one:
item.getItemModelContext().getOriginalValue("attribute", locale)
Keep in mind the previous method will throw an IllegalStateException for new items (that have never been saved to the database). So you can check if the item is not new like this:
!item.getItemModelContext().isNew()
For new items, you can interpret the previous values as null.