-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: [Howto] Intercept update action, compare old and new state ?
PostPosted: Tue Sep 27, 2005 6:29 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 27, 2005 12:15 pm 
Beginner
Beginner

Joined: Wed Apr 13, 2005 10:34 am
Posts: 38
Hibernate does not keep shadow copies of loaded entities.

The problem with your first attempt is that Session.load() returns the already loaded instance from the session cache. You'd have to read the original state from a different session.

But i think your trying to delegate business logic down to the persistence layer. You should do this kind of stuff before actually updating the availability property. ;)

Kind regards,

Heiko


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.