HHH-2879 Apply hibernate code templates and formatting

This commit is contained in:
edalquist 2012-01-11 10:09:12 -06:00 committed by Steve Ebersole
parent 8de0f76df1
commit f74c5a7fa5
15 changed files with 645 additions and 1029 deletions

View File

@ -40,17 +40,19 @@ public interface IdentifierLoadAccess<T> {
/**
* Return the persistent instance of the given entity class with the given identifier,
* assuming that the instance exists. This method might return a proxied instance that
* is initialized on-demand, when a non-identifier method is accessed.
* <br><br>
* You should not use this method to determine if an instance exists (use <tt>get()</tt>
* instead). Use this only to retrieve an instance that you assume exists, where non-existence
* would be an actual error.
* <br><br>
* is initialized on-demand, when a non-identifier method is accessed. <br>
* <br>
* You should not use this method to determine if an instance exists (use <tt>get()</tt> instead). Use this only to
* retrieve an instance that you assume exists, where non-existence
* would be an actual error. <br>
* <br>
* Due to the nature of the proxy functionality the return type of this method cannot use
* the generic type.
*
* @param theClass a persistent class
* @param id a valid identifier of an existing persistent instance of the class
* @param theClass
* a persistent class
* @param id
* a valid identifier of an existing persistent instance of the class
* @return the persistent instance or proxy
* @throws HibernateException
*/
@ -61,8 +63,10 @@ public interface IdentifierLoadAccess<T> {
* or null if there is no such persistent instance. (If the instance is already associated
* with the session, return that instance. This method never returns an uninitialized instance.)
*
* @param clazz a persistent class
* @param id an identifier
* @param clazz
* a persistent class
* @param id
* an identifier
* @return a persistent instance or null
* @throws HibernateException
*/

View File

@ -23,12 +23,10 @@
*/
package org.hibernate;
/**
* Loads an entity by its natural identifier
*
* @author Eric Dalquist
* @version $Revision$
* @see org.hibernate.annotations.NaturalId
*/
public interface NaturalIdLoadAccess<T> {
@ -40,8 +38,10 @@ public interface NaturalIdLoadAccess<T> {
/**
* Add a NaturalId attribute value.
*
* @param attributeName The entity attribute name that is marked as a NaturalId
* @param value The value of the attribute
* @param attributeName
* The entity attribute name that is marked as a NaturalId
* @param value
* The value of the attribute
*/
public NaturalIdLoadAccess<T> using(String attributeName, Object value);
@ -49,7 +49,8 @@ public interface NaturalIdLoadAccess<T> {
* Same behavior as {@link Session#load(Class, java.io.Serializable)}
*
* @return The entity
* @throws HibernateException if the entity does not exist
* @throws HibernateException
* if the entity does not exist
*/
public Object getReference();

View File

@ -775,8 +775,10 @@ public interface Session extends SharedSessionContract {
* Create an {@link IdentifierLoadAccess} instance to retrieve the specified entity by
* primary key.
*
* @param entityName The name of the entity that will be retrieved
* @throws HibernateException If the specified entity name is not found
* @param entityName
* The name of the entity that will be retrieved
* @throws HibernateException
* If the specified entity name is not found
*/
public IdentifierLoadAccess<Object> byId(String entityName);
@ -784,8 +786,10 @@ public interface Session extends SharedSessionContract {
* Create an {@link IdentifierLoadAccess} instance to retrieve the specified entity by
* primary key.
*
* @param entityClass The type of the entity that will be retrieved
* @throws HibernateException If the specified Class is not an entity
* @param entityClass
* The type of the entity that will be retrieved
* @throws HibernateException
* If the specified Class is not an entity
*/
public <T> IdentifierLoadAccess<T> byId(Class<T> entityClass);
@ -793,8 +797,10 @@ public interface Session extends SharedSessionContract {
* Create an {@link NaturalIdLoadAccess} instance to retrieve the specified entity by
* its natural id.
*
* @param entityName The name of the entity that will be retrieved
* @throws HibernateException If the specified entity name is not found or if the entity does not have a natural id specified
* @param entityName
* The name of the entity that will be retrieved
* @throws HibernateException
* If the specified entity name is not found or if the entity does not have a natural id specified
*/
public NaturalIdLoadAccess<Object> byNaturalId(String entityName);
@ -802,8 +808,10 @@ public interface Session extends SharedSessionContract {
* Create an {@link NaturalIdLoadAccess} instance to retrieve the specified entity by
* its natural id.
*
* @param entityClass The type of the entity that will be retrieved
* @throws HibernateException If the specified Class is not an entity or if the entity does not have a natural id specified
* @param entityClass
* The type of the entity that will be retrieved
* @throws HibernateException
* If the specified Class is not an entity or if the entity does not have a natural id specified
*/
public <T> NaturalIdLoadAccess<T> byNaturalId(Class<T> entityClass);

View File

@ -1,7 +1,7 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
@ -28,48 +28,24 @@ import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.NonUniqueObjectException;
import org.hibernate.PersistentObjectException;
import org.hibernate.cache.spi.CacheKey;
import org.hibernate.cache.spi.access.SoftLock;
import org.hibernate.cache.spi.entry.CacheEntry;
import org.hibernate.engine.internal.TwoPhaseLoad;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.Status;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventSource;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.LoadEvent;
import org.hibernate.event.spi.LoadEventListener;
import org.hibernate.event.spi.PostLoadEvent;
import org.hibernate.event.spi.PostLoadEventListener;
import org.hibernate.event.spi.ResolveNaturalIdEvent;
import org.hibernate.event.spi.ResolveNaturalIdEventListener;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.tuple.StandardProperty;
import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.type.EmbeddedComponentType;
import org.hibernate.type.Type;
import org.hibernate.type.TypeHelper;
import org.jboss.logging.Logger;
/**
* Defines the default load event listeners used by hibernate for loading entities
* in response to generated load events.
*
* @author Steve Ebersole
* @author Eric Dalquist
*/
public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEventListener implements ResolveNaturalIdEventListener {
public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEventListener implements
ResolveNaturalIdEventListener {
public static final Object REMOVED_ENTITY_MARKER = new Object();
public static final Object INCONSISTENT_RTN_CLASS_MARKER = new Object();
@ -78,9 +54,11 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class,
DefaultResolveNaturalIdEventListener.class.getName() );
/* (non-Javadoc)
* @see org.hibernate.event.spi.ResolveNaturalIdEventListener#onResolveNaturalId(org.hibernate.event.spi.ResolveNaturalIdEvent)
/*
* (non-Javadoc)
*
* @see org.hibernate.event.spi.ResolveNaturalIdEventListener#onResolveNaturalId(org.hibernate.event.spi.
* ResolveNaturalIdEvent)
*/
@Override
public void onResolveNaturalId(ResolveNaturalIdEvent event) throws HibernateException {
@ -88,10 +66,7 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
EntityPersister persister = source.getFactory().getEntityPersister( event.getEntityClassName() );
if ( persister == null ) {
throw new HibernateException(
"Unable to locate persister: " +
event.getEntityClassName()
);
throw new HibernateException( "Unable to locate persister: " + event.getEntityClassName() );
}
// Verify that the entity has a natural id and that the properties match up with the event.
@ -103,7 +78,9 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
final Map<String, Object> naturalIdParams = event.getNaturalId();
if ( naturalIdentifierProperties.length != naturalIdParams.size() ) {
throw new HibernateException(event.getEntityClassName() + " has " + naturalIdentifierProperties.length + " properties in its natural id but " + naturalIdParams.size() + " properties were specified: " + naturalIdParams);
throw new HibernateException( event.getEntityClassName() + " has " + naturalIdentifierProperties.length
+ " properties in its natural id but " + naturalIdParams.size() + " properties were specified: "
+ naturalIdParams );
}
final StandardProperty[] properties = entityMetamodel.getProperties();
@ -111,7 +88,8 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
final StandardProperty property = properties[naturalIdentifierProperties[idPropIdx]];
final String name = property.getName();
if ( !naturalIdParams.containsKey( name ) ) {
throw new HibernateException(event.getEntityClassName() + " natural id property " + name + " is missing from the map of natural id parameters: " + naturalIdParams);
throw new HibernateException( event.getEntityClassName() + " natural id property " + name
+ " is missing from the map of natural id parameters: " + naturalIdParams );
}
}
@ -119,29 +97,27 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
event.setEntityId( entityId );
}
/**
* Coordinates the efforts to load a given entity. First, an attempt is
* made to load the entity from the session-level cache. If not found there,
* an attempt is made to locate it in second-level cache. Lastly, an
* attempt is made to load it directly from the datasource.
*
* @param event The load event
* @param persister The persister for the entity being requested for load
* @param keyToLoad The EntityKey representing the entity to be loaded.
* @param options The load options.
* @param event
* The load event
* @param persister
* The persister for the entity being requested for load
* @param keyToLoad
* The EntityKey representing the entity to be loaded.
* @param options
* The load options.
* @return The loaded entity, or null.
*/
protected Serializable doResolveNaturalId(
final ResolveNaturalIdEvent event,
final EntityPersister persister) {
protected Serializable doResolveNaturalId(final ResolveNaturalIdEvent event, final EntityPersister persister) {
if (LOG.isTraceEnabled()) LOG.trace("Attempting to resolve: "
+ MessageHelper.infoString(persister,
event.getNaturalId(),
event.getSession().getFactory()));
if ( LOG.isTraceEnabled() )
LOG.trace( "Attempting to resolve: "
+ MessageHelper.infoString( persister, event.getNaturalId(), event.getSession().getFactory() ) );
Serializable entityId = loadFromSessionCache( event, persister );
if ( entityId == REMOVED_ENTITY_MARKER ) {
@ -153,26 +129,23 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
return null;
}
if ( entityId != null ) {
if (LOG.isTraceEnabled()) LOG.trace("Resolved object in session cache: "
+ MessageHelper.infoString(persister,
event.getNaturalId(),
event.getSession().getFactory()));
if ( LOG.isTraceEnabled() )
LOG.trace( "Resolved object in session cache: "
+ MessageHelper.infoString( persister, event.getNaturalId(), event.getSession().getFactory() ) );
return entityId;
}
entityId = loadFromSecondLevelCache( event, persister );
if ( entityId != null ) {
if (LOG.isTraceEnabled()) LOG.trace("Resolved object in second-level cache: "
+ MessageHelper.infoString(persister,
event.getNaturalId(),
event.getSession().getFactory()));
if ( LOG.isTraceEnabled() )
LOG.trace( "Resolved object in second-level cache: "
+ MessageHelper.infoString( persister, event.getNaturalId(), event.getSession().getFactory() ) );
return entityId;
}
if (LOG.isTraceEnabled()) LOG.trace("Object not resolved in any cache: "
+ MessageHelper.infoString(persister,
event.getNaturalId(),
event.getSession().getFactory()));
if ( LOG.isTraceEnabled() )
LOG.trace( "Object not resolved in any cache: "
+ MessageHelper.infoString( persister, event.getNaturalId(), event.getSession().getFactory() ) );
return loadFromDatasource( event, persister );
}
@ -180,23 +153,24 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
/**
* Attempts to locate the entity in the session-level cache.
* <p/>
* If allowed to return nulls, then if the entity happens to be found in
* the session cache, we check the entity type for proper handling
* of entity hierarchies.
* If allowed to return nulls, then if the entity happens to be found in the session cache, we check the entity type
* for proper handling of entity hierarchies.
* <p/>
* If checkDeleted was set to true, then if the entity is found in the
* session-level cache, it's current status within the session cache
* is checked to see if it has previously been scheduled for deletion.
* If checkDeleted was set to true, then if the entity is found in the session-level cache, it's current status
* within the session cache is checked to see if it has previously been scheduled for deletion.
*
* @param event The load event
* @param keyToLoad The EntityKey representing the entity to be loaded.
* @param options The load options.
* @param event
* The load event
* @param keyToLoad
* The EntityKey representing the entity to be loaded.
* @param options
* The load options.
* @return The entity from the session-level cache, or null.
* @throws HibernateException Generally indicates problems applying a lock-mode.
* @throws HibernateException
* Generally indicates problems applying a lock-mode.
*/
protected Serializable loadFromSessionCache(
final ResolveNaturalIdEvent event,
final EntityPersister persister) throws HibernateException {
protected Serializable loadFromSessionCache(final ResolveNaturalIdEvent event, final EntityPersister persister)
throws HibernateException {
// SessionImplementor session = event.getSession();
// Object old = session.getEntityUsingInterceptor( keyToLoad );
//
@ -210,7 +184,8 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
// }
// }
// if ( options.isAllowNulls() ) {
// final EntityPersister persister = event.getSession().getFactory().getEntityPersister( keyToLoad.getEntityName() );
// final EntityPersister persister = event.getSession().getFactory().getEntityPersister(
// keyToLoad.getEntityName() );
// if ( ! persister.isInstance( old ) ) {
// return INCONSISTENT_RTN_CLASS_MARKER;
// }
@ -224,14 +199,15 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
/**
* Attempts to load the entity from the second-level cache.
*
* @param event The load event
* @param persister The persister for the entity being requested for load
* @param options The load options.
* @param event
* The load event
* @param persister
* The persister for the entity being requested for load
* @param options
* The load options.
* @return The entity from the second-level cache, or null.
*/
protected Serializable loadFromSecondLevelCache(
final ResolveNaturalIdEvent event,
final EntityPersister persister) {
protected Serializable loadFromSecondLevelCache(final ResolveNaturalIdEvent event, final EntityPersister persister) {
// final SessionImplementor source = event.getSession();
//
@ -277,349 +253,23 @@ public class DefaultResolveNaturalIdEventListener extends AbstractLockUpgradeEve
return null;
}
/**
* Performs the process of loading an entity from the configured
* underlying datasource.
*
* @param event The load event
* @param persister The persister for the entity being requested for load
* @param keyToLoad The EntityKey representing the entity to be loaded.
* @param options The load options.
* @param event
* The load event
* @param persister
* The persister for the entity being requested for load
* @param keyToLoad
* The EntityKey representing the entity to be loaded.
* @param options
* The load options.
* @return The object loaded from the datasource, or null if not found.
*/
protected Serializable loadFromDatasource(
final ResolveNaturalIdEvent event,
final EntityPersister persister) {
protected Serializable loadFromDatasource(final ResolveNaturalIdEvent event, final EntityPersister persister) {
final SessionImplementor source = event.getSession();
return persister.loadEntityIdByNaturalId(
event.getNaturalId(),
event.getLockOptions(),
event.getSession());
/*
Object entity = persister.load(
event.getEntityId(),
event.getInstanceToLoad(),
event.getLockOptions(),
source
);
if ( event.isAssociationFetch() && source.getFactory().getStatistics().isStatisticsEnabled() ) {
source.getFactory().getStatisticsImplementor().fetchEntity( event.getEntityClassName() );
return persister.loadEntityIdByNaturalId( event.getNaturalId(), event.getLockOptions(), event.getSession() );
}
return entity;
*/
}
// private void loadByDerivedIdentitySimplePkValue(
// LoadEvent event,
// LoadEventListener.LoadType options,
// EntityPersister dependentPersister,
// EmbeddedComponentType dependentIdType,
// EntityPersister parentPersister) {
// final EntityKey parentEntityKey = event.getSession().generateEntityKey( event.getEntityId(), parentPersister );
// final Object parent = doLoad( event, parentPersister, parentEntityKey, options );
//
// final Serializable dependent = (Serializable) dependentIdType.instantiate( parent, event.getSession() );
// dependentIdType.setPropertyValues( dependent, new Object[] {parent}, dependentPersister.getEntityMode() );
// final EntityKey dependentEntityKey = event.getSession().generateEntityKey( dependent, dependentPersister );
// event.setEntityId( dependent );
//
// event.setResult( doLoad( event, dependentPersister, dependentEntityKey, options ) );
// }
//
// /**
// * Performs the load of an entity.
// *
// * @param event The initiating load request event
// * @param persister The persister corresponding to the entity to be loaded
// * @param keyToLoad The key of the entity to be loaded
// * @param options The defined load options
// * @return The loaded entity.
// * @throws HibernateException
// */
// protected Object load(
// final LoadEvent event,
// final EntityPersister persister,
// final EntityKey keyToLoad,
// final LoadEventListener.LoadType options) {
//
// if ( event.getInstanceToLoad() != null ) {
// if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) {
// throw new PersistentObjectException(
// "attempted to load into an instance that was already associated with the session: " +
// MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() )
// );
// }
// persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession() );
// }
//
// Object entity = doLoad(event, persister, keyToLoad, options);
//
// boolean isOptionalInstance = event.getInstanceToLoad() != null;
//
// if ( !options.isAllowNulls() || isOptionalInstance ) {
// if ( entity == null ) {
// event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
// }
// }
//
// if ( isOptionalInstance && entity != event.getInstanceToLoad() ) {
// throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
// }
//
// return entity;
// }
//
// /**
// * Based on configured options, will either return a pre-existing proxy,
// * generate a new proxy, or perform an actual load.
// *
// * @param event The initiating load request event
// * @param persister The persister corresponding to the entity to be loaded
// * @param keyToLoad The key of the entity to be loaded
// * @param options The defined load options
// * @return The result of the proxy/load operation.
// */
// protected Object proxyOrLoad(
// final LoadEvent event,
// final EntityPersister persister,
// final EntityKey keyToLoad,
// final LoadEventListener.LoadType options) {
//
// if (LOG.isTraceEnabled()) LOG.trace("Loading entity: "
// + MessageHelper.infoString(persister,
// event.getEntityId(),
// event.getSession().getFactory()));
//
// // this class has no proxies (so do a shortcut)
// if (!persister.hasProxy()) return load(event, persister, keyToLoad, options);
// final PersistenceContext persistenceContext = event.getSession().getPersistenceContext();
//
// // look for a proxy
// Object proxy = persistenceContext.getProxy(keyToLoad);
// if (proxy != null) return returnNarrowedProxy(event, persister, keyToLoad, options, persistenceContext, proxy);
// if (options.isAllowProxyCreation()) return createProxyIfNecessary(event, persister, keyToLoad, options, persistenceContext);
// // return a newly loaded object
// return load(event, persister, keyToLoad, options);
// }
//
// /**
// * Given a proxy, initialize it and/or narrow it provided either
// * is necessary.
// *
// * @param event The initiating load request event
// * @param persister The persister corresponding to the entity to be loaded
// * @param keyToLoad The key of the entity to be loaded
// * @param options The defined load options
// * @param persistenceContext The originating session
// * @param proxy The proxy to narrow
// * @return The created/existing proxy
// */
// private Object returnNarrowedProxy(
// final LoadEvent event,
// final EntityPersister persister,
// final EntityKey keyToLoad,
// final LoadEventListener.LoadType options,
// final PersistenceContext persistenceContext,
// final Object proxy) {
// LOG.trace("Entity proxy found in session cache");
// LazyInitializer li = ( (HibernateProxy) proxy ).getHibernateLazyInitializer();
// if ( li.isUnwrap() ) {
// return li.getImplementation();
// }
// Object impl = null;
// if ( !options.isAllowProxyCreation() ) {
// impl = load( event, persister, keyToLoad, options );
// if ( impl == null ) {
// event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( persister.getEntityName(), keyToLoad.getIdentifier());
// }
// }
// return persistenceContext.narrowProxy( proxy, persister, keyToLoad, impl );
// }
//
// /**
// * If there is already a corresponding proxy associated with the
// * persistence context, return it; otherwise create a proxy, associate it
// * with the persistence context, and return the just-created proxy.
// *
// * @param event The initiating load request event
// * @param persister The persister corresponding to the entity to be loaded
// * @param keyToLoad The key of the entity to be loaded
// * @param options The defined load options
// * @param persistenceContext The originating session
// * @return The created/existing proxy
// */
// private Object createProxyIfNecessary(
// final LoadEvent event,
// final EntityPersister persister,
// final EntityKey keyToLoad,
// final LoadEventListener.LoadType options,
// final PersistenceContext persistenceContext) {
// Object existing = persistenceContext.getEntity( keyToLoad );
// if ( existing != null ) {
// // return existing object or initialized proxy (unless deleted)
// LOG.trace("Entity found in session cache");
// if ( options.isCheckDeleted() ) {
// EntityEntry entry = persistenceContext.getEntry( existing );
// Status status = entry.getStatus();
// if ( status == Status.DELETED || status == Status.GONE ) {
// return null;
// }
// }
// return existing;
// }
// LOG.trace("Creating new proxy for entity");
// // return new uninitialized proxy
// Object proxy = persister.createProxy(event.getEntityId(), event.getSession());
// persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey(keyToLoad);
// persistenceContext.addProxy(keyToLoad, proxy);
// return proxy;
// }
//
// /**
// * If the class to be loaded has been configured with a cache, then lock
// * given id in that cache and then perform the load.
// *
// * @param event The initiating load request event
// * @param persister The persister corresponding to the entity to be loaded
// * @param keyToLoad The key of the entity to be loaded
// * @param options The defined load options
// * @param source The originating session
// * @return The loaded entity
// * @throws HibernateException
// */
// protected Object lockAndLoad(
// final LoadEvent event,
// final EntityPersister persister,
// final EntityKey keyToLoad,
// final LoadEventListener.LoadType options,
// final SessionImplementor source) {
// SoftLock lock = null;
// final CacheKey ck;
// if ( persister.hasCache() ) {
// ck = source.generateCacheKey(
// event.getEntityId(),
// persister.getIdentifierType(),
// persister.getRootEntityName()
// );
// lock = persister.getCacheAccessStrategy().lockItem( ck, null );
// }
// else {
// ck = null;
// }
//
// Object entity;
// try {
// entity = load(event, persister, keyToLoad, options);
// }
// finally {
// if ( persister.hasCache() ) {
// persister.getCacheAccessStrategy().unlockItem( ck, lock );
// }
// }
//
// return event.getSession().getPersistenceContext().proxyFor( persister, keyToLoad, entity );
// }
//
// private Object assembleCacheEntry(
// final CacheEntry entry,
// final Serializable id,
// final EntityPersister persister,
// final LoadEvent event) throws HibernateException {
//
// final Object optionalObject = event.getInstanceToLoad();
// final EventSource session = event.getSession();
// final SessionFactoryImplementor factory = session.getFactory();
//
// if (LOG.isTraceEnabled()) LOG.trace("Assembling entity from second-level cache: "
// + MessageHelper.infoString(persister, id, factory));
//
// EntityPersister subclassPersister = factory.getEntityPersister( entry.getSubclass() );
// Object result = optionalObject == null ?
// session.instantiate( subclassPersister, id ) : optionalObject;
//
// // make it circular-reference safe
// final EntityKey entityKey = session.generateEntityKey( id, subclassPersister );
// TwoPhaseLoad.addUninitializedCachedEntity(
// entityKey,
// result,
// subclassPersister,
// LockMode.NONE,
// entry.areLazyPropertiesUnfetched(),
// entry.getVersion(),
// session
// );
//
// Type[] types = subclassPersister.getPropertyTypes();
// Object[] values = entry.assemble( result, id, subclassPersister, session.getInterceptor(), session ); // intializes result by side-effect
// TypeHelper.deepCopy(
// values,
// types,
// subclassPersister.getPropertyUpdateability(),
// values,
// session
// );
//
// Object version = Versioning.getVersion( values, subclassPersister );
// if (LOG.isTraceEnabled()) LOG.trace("Cached Version: " + version);
//
// final PersistenceContext persistenceContext = session.getPersistenceContext();
// boolean isReadOnly = session.isDefaultReadOnly();
// if ( persister.isMutable() ) {
// Object proxy = persistenceContext.getProxy( entityKey );
// if ( proxy != null ) {
// // there is already a proxy for this impl
// // only set the status to read-only if the proxy is read-only
// isReadOnly = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().isReadOnly();
// }
// }
// else {
// isReadOnly = true;
// }
// persistenceContext.addEntry(
// result,
// ( isReadOnly ? Status.READ_ONLY : Status.MANAGED ),
// values,
// null,
// id,
// version,
// LockMode.NONE,
// true,
// subclassPersister,
// false,
// entry.areLazyPropertiesUnfetched()
// );
// subclassPersister.afterInitialize( result, entry.areLazyPropertiesUnfetched(), session );
// persistenceContext.initializeNonLazyCollections();
// // upgrade the lock if necessary:
// //lock(result, lockMode);
//
// //PostLoad is needed for EJB3
// //TODO: reuse the PostLoadEvent...
// PostLoadEvent postLoadEvent = new PostLoadEvent( session )
// .setEntity( result )
// .setId( id )
// .setPersister( persister );
//
// for ( PostLoadEventListener listener : postLoadEventListeners( session ) ) {
// listener.onPostLoad( postLoadEvent );
// }
//
// return result;
// }
//
// private Iterable<PostLoadEventListener> postLoadEventListeners(EventSource session) {
// return session
// .getFactory()
// .getServiceRegistry()
// .getService( EventListenerRegistry.class )
// .getEventListenerGroup( EventType.POST_LOAD )
// .listeners();
// }
}

View File

@ -33,7 +33,7 @@ import org.hibernate.LockOptions;
/**
* Defines an event class for the resolving of an entity id from the entity's natural-id
*
* @author Steve Ebersole
* @author Eric Dalquist
*/
public class ResolveNaturalIdEvent extends AbstractEvent {
public static final LockMode DEFAULT_LOCK_MODE = LockMode.NONE;
@ -47,7 +47,8 @@ public class ResolveNaturalIdEvent extends AbstractEvent {
this( naturalId, entityClassName, new LockOptions(), source );
}
public ResolveNaturalIdEvent(Map<String, Object> naturalId, String entityClassName, LockOptions lockOptions, EventSource source) {
public ResolveNaturalIdEvent(Map<String, Object> naturalId, String entityClassName, LockOptions lockOptions,
EventSource source) {
super( source );
if ( naturalId == null || naturalId.isEmpty() ) {

View File

@ -30,14 +30,15 @@ import org.hibernate.HibernateException;
/**
* Defines the contract for handling of resolve natural id events generated from a session.
*
* @author Steve Ebersole
* @author Eric Dalquist
*/
public interface ResolveNaturalIdEventListener extends Serializable {
/**
* Handle the given resolve natural id event.
*
* @param event The resolve natural id event to be handled.
* @param event
* The resolve natural id event to be handled.
* @throws HibernateException
*/
public void onResolveNaturalId(ResolveNaturalIdEvent event) throws HibernateException;

View File

@ -922,33 +922,21 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
return this.byId( entityName ).with( lockOptions ).load( id );
}
/* (non-Javadoc)
* @see org.hibernate.Session#byId(java.lang.String)
*/
@Override
public IdentifierLoadAccessImpl<Object> byId(String entityName) {
return new IdentifierLoadAccessImpl<Object>( entityName, Object.class );
}
/* (non-Javadoc)
* @see org.hibernate.Session#byId(java.lang.Class)
*/
@Override
public <T> IdentifierLoadAccessImpl<T> byId(Class<T> entityClass) {
return new IdentifierLoadAccessImpl<T>( entityClass.getName(), entityClass );
}
/* (non-Javadoc)
* @see org.hibernate.Session#byNaturalId(java.lang.String)
*/
@Override
public NaturalIdLoadAccess<Object> byNaturalId(String entityName) {
return new NaturalIdLoadAccessImpl<Object>( entityName, Object.class );
}
/* (non-Javadoc)
* @see org.hibernate.Session#byNaturalId(java.lang.Class)
*/
@Override
public <T> NaturalIdLoadAccess<T> byNaturalId(Class<T> entityClass) {
return new NaturalIdLoadAccessImpl<T>( entityClass.getName(), entityClass );
@ -2185,16 +2173,11 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
private final Class<T> entityClass;
private LockOptions lockOptions;
/**
*/
private IdentifierLoadAccessImpl(String entityName, Class<T> entityClass) {
this.entityName = entityName;
this.entityClass = entityClass;
}
/* (non-Javadoc)
* @see org.hibernate.IdentifierLoadAccess#with(org.hibernate.LockOptions)
*/
@Override
public final IdentifierLoadAccessImpl<T> with(LockOptions lockOptions) {
this.lockOptions = lockOptions;
@ -2212,9 +2195,6 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
return this;
}
/* (non-Javadoc)
* @see org.hibernate.IdentifierLoadAccess#getReference(java.io.Serializable)
*/
@Override
public final Object getReference(Serializable id) {
if ( this.lockOptions != null ) {
@ -2238,9 +2218,6 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
}
}
/* (non-Javadoc)
* @see org.hibernate.IdentifierLoadAccess#load(java.io.Serializable)
*/
@Override
public final Object load(Serializable id) {
if ( this.lockOptions != null ) {
@ -2268,27 +2245,17 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
private final Map<String, Object> naturalIdParameters = new LinkedHashMap<String, Object>();
private LockOptions lockOptions;
/**
* Note that the specified entity MUST be castable using {@link Class#cast(Object)}
* on the specified entityClass
*/
private NaturalIdLoadAccessImpl(String entityName, Class<T> entityClass) {
this.entityName = entityName;
this.entityClass = entityClass;
}
/* (non-Javadoc)
* @see org.hibernate.IdentifierLoadAccess#with(org.hibernate.LockOptions)
*/
@Override
public final NaturalIdLoadAccess<T> with(LockOptions lockOptions) {
this.lockOptions = lockOptions;
return this;
}
/* (non-Javadoc)
* @see org.hibernate.NaturalIdLoadAccess#using(java.lang.String, java.lang.Object)
*/
@Override
public NaturalIdLoadAccess<T> using(String attributeName, Object value) {
naturalIdParameters.put( attributeName, value );
@ -2296,22 +2263,21 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
}
protected Serializable resolveNaturalId() {
final ResolveNaturalIdEvent event = new ResolveNaturalIdEvent(naturalIdParameters, entityName, SessionImpl.this);
final ResolveNaturalIdEvent event = new ResolveNaturalIdEvent( naturalIdParameters, entityName,
SessionImpl.this );
fireResolveNaturalId( event );
return event.getEntityId();
}
protected IdentifierLoadAccess<T> getIdentifierLoadAccess() {
final IdentifierLoadAccessImpl<T> identifierLoadAccess = new SessionImpl.IdentifierLoadAccessImpl<T>(entityName, entityClass);
final IdentifierLoadAccessImpl<T> identifierLoadAccess = new SessionImpl.IdentifierLoadAccessImpl<T>(
entityName, entityClass );
if ( this.lockOptions != null ) {
identifierLoadAccess.with( lockOptions );
}
return identifierLoadAccess;
}
/* (non-Javadoc)
* @see org.hibernate.NaturalIdLoadAccess#getReference()
*/
@Override
public final Object getReference() {
final Serializable entityId = resolveNaturalId();
@ -2321,9 +2287,6 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
return this.getIdentifierLoadAccess().getReference( entityId );
}
/* (non-Javadoc)
* @see org.hibernate.NaturalIdLoadAccess#load()
*/
@Override
public final Object load() {
final Serializable entityId = resolveNaturalId();

View File

@ -4503,21 +4503,19 @@ public abstract class AbstractEntityPersister
}
}
/* (non-Javadoc)
* @see org.hibernate.persister.entity.EntityPersister#loadEntityIdByNaturalId(java.util.Map, org.hibernate.engine.spi.SessionImplementor)
*/
@Override
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions, SessionImplementor session) {
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
SessionImplementor session) {
if ( !hasNaturalIdentifier() ) {
throw new MappingException( "persistent class did not define a natural-id : " + MessageHelper.infoString( this ) );
throw new MappingException( "persistent class did not define a natural-id : "
+ MessageHelper.infoString( this ) );
}
if (LOG.isTraceEnabled()) LOG.trace("Getting entity id for natural-id for: "
if ( LOG.isTraceEnabled() )
LOG.trace( "Getting entity id for natural-id for: "
+ MessageHelper.infoString( this, naturalIdParameters, getFactory() ) );
try {
PreparedStatement ps = session.getTransactionCoordinator()
.getJdbcCoordinator()
.getStatementPreparer()
PreparedStatement ps = session.getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer()
.prepareStatement( sqlEntityIdByNaturalIdString );
try {
int positions = 1;
@ -4530,7 +4528,6 @@ public abstract class AbstractEntityPersister
final Object value = naturalIdParameters.get( property.getName() );
//TODO am I setting the positions var correctly here?
final Type propertyType = property.getType();
propertyType.nullSafeSet( ps, value, positions, session );
positions += propertyType.getColumnSpan( session.getFactory() );
@ -4556,15 +4553,12 @@ public abstract class AbstractEntityPersister
catch ( SQLException e ) {
throw getFactory().getSQLExceptionHelper().convert(
e,
"could not retrieve entity id: " + MessageHelper.infoString( this, naturalIdParameters, getFactory() ),
sqlEntityIdByNaturalIdString
);
"could not retrieve entity id: "
+ MessageHelper.infoString( this, naturalIdParameters, getFactory() ),
sqlEntityIdByNaturalIdString );
}
}
/**
* @return
*/
private String generateEntityIdByNaturalIdSql() {
Select select = new Select( getFactory().getDialect() );
if ( getFactory().getSettings().isCommentsEnabled() ) {
@ -4595,9 +4589,7 @@ public abstract class AbstractEntityPersister
whereClause.append( whereJoinFragment( getRootAlias(), true, false ) );
String sql = select.setOuterJoins( "", "" )
.setWhereClause( whereClause.toString() )
.toStatementString();
String sql = select.setOuterJoins( "", "" ).setWhereClause( whereClause.toString() ).toStatementString();
return sql;
}

View File

@ -324,7 +324,8 @@ public interface EntityPersister extends OptimisticCacheSource {
/**
* Load the id for the entity based on the natural id.
*/
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions, SessionImplementor session);
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
SessionImplementor session);
/**
* Load an instance of the persistent class.

View File

@ -106,8 +106,6 @@ public class ImmutableNaturalIdTest extends AbstractJPATest {
s.close();
}
@Test
public void testNaturalIdLoadAccessCache() {
Session s = openSession();
@ -118,7 +116,6 @@ public class ImmutableNaturalIdTest extends AbstractJPATest {
s.close();
sessionFactory().getStatistics().clear();
// sessionFactory().getStatistics().logSummary();
s = openSession();
s.beginTransaction();
@ -127,8 +124,6 @@ public class ImmutableNaturalIdTest extends AbstractJPATest {
s.getTransaction().commit();
s.close();
// sessionFactory().getStatistics().logSummary();
assertEquals( 1, sessionFactory().getStatistics().getEntityLoadCount() );
assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCacheMissCount() );
assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCacheHitCount() );