*
* @param event The refresh event to be handled.
*/
public void onRefresh(RefreshEvent event, Map refreshedAlready) {
final EventSource source = event.getSession();
boolean isTransient = !source.contains( event.getObject() );
if ( source.getPersistenceContext().reassociateIfUninitializedProxy( event.getObject() ) ) {
if ( isTransient ) {
source.setReadOnly( event.getObject(), source.isDefaultReadOnly() );
}
return;
}
final Object object = source.getPersistenceContext().unproxyAndReassociate( event.getObject() );
if ( refreshedAlready.containsKey( object ) ) {
LOG.trace( "Already refreshed" );
return;
}
final EntityEntry e = source.getPersistenceContext().getEntry( object );
final EntityPersister persister;
final Serializable id;
if ( e == null ) {
persister = source.getEntityPersister(
event.getEntityName(),
object
); //refresh() does not pass an entityName
id = persister.getIdentifier( object, event.getSession() );
if ( LOG.isTraceEnabled() ) {
LOG.tracev(
"Refreshing transient {0}", MessageHelper.infoString(
persister,
id,
source.getFactory()
)
);
}
final EntityKey key = source.generateEntityKey( id, persister );
if ( source.getPersistenceContext().getEntry( key ) != null ) {
throw new PersistentObjectException(
"attempted to refresh transient instance when persistent instance was already associated with the Session: " +
MessageHelper.infoString( persister, id, source.getFactory() )
);
}
}
else {
if ( LOG.isTraceEnabled() ) {
LOG.tracev(
"Refreshing ", MessageHelper.infoString(
e.getPersister(),
e.getId(),
source.getFactory()
)
);
}
if ( !e.isExistsInDatabase() ) {
throw new UnresolvableObjectException(
e.getId(),
"this instance does not yet exist as a row in the database"
);
}
persister = e.getPersister();
id = e.getId();
}
// cascade the refresh prior to refreshing this entity
refreshedAlready.put( object, object );
new Cascade( CascadingActions.REFRESH, CascadePoint.BEFORE_REFRESH, source ).cascade(
persister,
object,
refreshedAlready
);
if ( e != null ) {
final EntityKey key = source.generateEntityKey( id, persister );
source.getPersistenceContext().removeEntity( key );
if ( persister.hasCollections() ) {
new EvictVisitor( source ).process( object, persister );
}
}
if ( persister.hasCache() ) {
final CacheKey ck = source.generateCacheKey(
id,
persister.getIdentifierType(),
persister.getRootEntityName()
);
persister.getCacheAccessStrategy().evict( ck );
}
evictCachedCollections( persister, id, source.getFactory() );
String previousFetchProfile = source.getLoadQueryInfluencers().getInternalFetchProfile();
source.getLoadQueryInfluencers().setInternalFetchProfile( "refresh" );
Object result = persister.load( id, object, event.getLockOptions(), source );
// Keep the same read-only/modifiable setting for the entity that it had before refreshing;
// If it was transient, then set it to the default for the source.
if ( result != null ) {
if ( !persister.isMutable() ) {
// this is probably redundant; it should already be read-only
source.setReadOnly( result, true );
}
else {
source.setReadOnly( result, ( e == null ? source.isDefaultReadOnly() : e.isReadOnly() ) );
}
}
source.getLoadQueryInfluencers().setInternalFetchProfile( previousFetchProfile );
UnresolvableObjectException.throwIfNull( result, id, persister.getEntityName() );
}