-->
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.  [ 4 posts ] 
Author Message
 Post subject: Interceptor.postFlush: how to modify state after persistence
PostPosted: Wed Oct 10, 2007 10:31 am 
Newbie

Joined: Thu Jul 22, 2004 7:24 am
Posts: 10
Hibernate version: 3.2.2

I'm using a session-scoped Interceptor to convert all timestamp attributes to UTC before persisting (in onSave, onFlushDirty methods). Works fine.

However, the in memory object after persistence needs to reflect the original timestamp values (in their original timezone). To achieve this I tried the following in postFlush:

Code:
  public void postFlush(Iterator entities)
  {
    logger.debug("postFlush: reverting in-memory state of modified entities..");

    while (entities.hasNext())
    {
      Object nextEntity = entities.next();
      Boolean modified = modifiedState.get(nextEntity);
      if (modified != null && modified.booleanValue())
      {
        modifiedState.put(nextEntity, Boolean.FALSE);

        // revert the in memory state of this entity from pre converted values
        revertConvertedState(nextEntity);
      }
    }
  }

  private void revertConvertedState(Object entity)
  {
    SessionImpl sessionImpl = (SessionImpl)HibernateUtils.currentSession();
    EntityPersister entityPersister = sessionImpl.getEntityPersister(sessionImpl.getEntityName(entity), entity);

    Object[] preConversionState = (Object[])preConversionStateMap.get(entity); // state saved before conversion during onFlushDirty

    if (preConversionState != null)
    {
      logger.debug("reverting state for object [" + entity + "], property values [" + preConversionState + "]");
      entityPersister.setPropertyValues(entity, preConversionState, EntityMode.POJO);
    }
  }


But setting the property values using the EntityPersister does not seem to work. And I end up with timestamp values in UTC.

Is this the right approach? Does hibernate ignore any changes to in-memory state in postFlush?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 12:16 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Wouldn't it be better to do that stuff in your pojo instead of in an interceptor? If your API property is called XTime, then define four methods getXTime, setXTime, getXTimeUTC, setXTimeUTC. Map only the XTimeUTC property: the non-UTC versions just do this:
Code:
public YourDateClass getXTime()
{
  return convertFromUTC( getXTimeUTC() );
}

public void setXTime( YourDateClass date )
{
  setXTimeUTC( convertToUTC( date ));
}
This is the pattern that I see most commonly, and it is very effective. It also adds richer functionality to your own code, allowing access to either version of the time.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Re: Interceptor.postFlush: how to modify state after persist
PostPosted: Thu Oct 11, 2007 1:03 am 
Newbie

Joined: Thu Jul 22, 2004 7:24 am
Posts: 10
Thanks tenwit. You are right. Implementing the get/set would work in our case. There's even no need to change the type of the property, as we still need a java.util.Date.

Also, the client is not really bothered about this conversion to UTC - which is primarily to implement uniformity in the data layer. So there's no need to provide both times.

I was thinking of using a custom UserType for performing this implicit conversion (better than an Interceptor). But I think doing it inside the POJO would be the best.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 1:07 am 
Newbie

Joined: Thu Jul 22, 2004 7:24 am
Posts: 10
tenwit, sorry about the credit rating. I'll be sure to mark it properly next time.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.