Hi
Is it possible to intercept object's update action , comparing old and new state , and do something?
Suppose an Item class , with AVAILABLE , UNAVAILABLE states ...
When an item is updated , I want to check if the state is changed from AVAILABLE to UNAVAILABLE, and do something else ... (deleting item from shoppingCarts ...etc)
I tried this first :
Code:
ItemDaoImpl.java
@Override
public void update(Item item)
{
Item originalItem = (Item) getSession().load(Item.class , item.getId());
if ((originalItem.getState() == ItemState.AVAILABLE && item.getState() == ItemState.UNAVAILABLE )
{
shoppingCartDao.deleteShoppingCart(item);
}
super.update(item);
}
I found this not works ... and try another way : DefaultUpdateEventListener
Code:
public class ItemUpdateListener extends DefaultUpdateEventListener
{
@Override
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent saveOrUpdateEvent)
{
if (saveOrUpdateEvent.getEntity() instanceof Item)
{
// I don't know how to get the old/new item state here...
}
return super.performSaveOrUpdate(saveOrUpdateEvent);
}
}
Here is the problem:
I don't know how to get the old/new item state here, so , I can't compare the old and new state of the item...
Is there any way to solve this ?
Hibernate version:3.0.5
Name and version of the database you are using: MySQL 3.23