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

@ -32,39 +32,43 @@ import java.io.Serializable;
* @version $Revision$ * @version $Revision$
*/ */
public interface IdentifierLoadAccess<T> { public interface IdentifierLoadAccess<T> {
/** /**
* Set the {@link LockOptions} to use when retrieving the entity. * Set the {@link LockOptions} to use when retrieving the entity.
*/ */
public IdentifierLoadAccess<T> with(LockOptions lockOptions); public IdentifierLoadAccess<T> with(LockOptions lockOptions);
/** /**
* Return the persistent instance of the given entity class with the given identifier, * 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 * assuming that the instance exists. This method might return a proxied instance that
* is initialized on-demand, when a non-identifier method is accessed. * is initialized on-demand, when a non-identifier method is accessed. <br>
* <br><br> * <br>
* You should not use this method to determine if an instance exists (use <tt>get()</tt> * You should not use this method to determine if an instance exists (use <tt>get()</tt> instead). Use this only to
* instead). Use this only to retrieve an instance that you assume exists, where non-existence * retrieve an instance that you assume exists, where non-existence
* would be an actual error. * would be an actual error. <br>
* <br><br> * <br>
* Due to the nature of the proxy functionality the return type of this method cannot use * Due to the nature of the proxy functionality the return type of this method cannot use
* the generic type. * the generic type.
* *
* @param theClass a persistent class * @param theClass
* @param id a valid identifier of an existing persistent instance of the class * a persistent class
* @return the persistent instance or proxy * @param id
* @throws HibernateException * a valid identifier of an existing persistent instance of the class
*/ * @return the persistent instance or proxy
public Object getReference(Serializable id); * @throws HibernateException
*/
public Object getReference(Serializable id);
/** /**
* Return the persistent instance of the given entity class with the given identifier, * Return the persistent instance of the given entity class with the given identifier,
* or null if there is no such persistent instance. (If the instance is already associated * 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.) * with the session, return that instance. This method never returns an uninitialized instance.)
* *
* @param clazz a persistent class * @param clazz
* @param id an identifier * a persistent class
* @return a persistent instance or null * @param id
* @throws HibernateException * an identifier
*/ * @return a persistent instance or null
public Object load(Serializable id); * @throws HibernateException
*/
public Object load(Serializable id);
} }

View File

@ -23,41 +23,42 @@
*/ */
package org.hibernate; package org.hibernate;
/** /**
* Loads an entity by its natural identifier * Loads an entity by its natural identifier
* *
* @author Eric Dalquist * @author Eric Dalquist
* @version $Revision$
* @see org.hibernate.annotations.NaturalId * @see org.hibernate.annotations.NaturalId
*/ */
public interface NaturalIdLoadAccess<T> { public interface NaturalIdLoadAccess<T> {
/** /**
* Set the {@link LockOptions} to use when retrieving the entity. * Set the {@link LockOptions} to use when retrieving the entity.
*/ */
public NaturalIdLoadAccess<T> with(LockOptions lockOptions); public NaturalIdLoadAccess<T> with(LockOptions lockOptions);
/** /**
* Add a NaturalId attribute value. * Add a NaturalId attribute value.
* *
* @param attributeName The entity attribute name that is marked as a NaturalId * @param attributeName
* @param value The value of the attribute * The entity attribute name that is marked as a NaturalId
*/ * @param value
public NaturalIdLoadAccess<T> using(String attributeName, Object value); * The value of the attribute
*/
public NaturalIdLoadAccess<T> using(String attributeName, Object value);
/** /**
* Same behavior as {@link Session#load(Class, java.io.Serializable)} * Same behavior as {@link Session#load(Class, java.io.Serializable)}
* *
* @return The entity * @return The entity
* @throws HibernateException if the entity does not exist * @throws HibernateException
*/ * if the entity does not exist
public Object getReference(); */
public Object getReference();
/** /**
* Same behavior as {@link Session#get(Class, java.io.Serializable)} * Same behavior as {@link Session#get(Class, java.io.Serializable)}
* *
* @return The entity or null if it does not exist * @return The entity or null if it does not exist
*/ */
public Object load(); public Object load();
} }

View File

@ -775,37 +775,45 @@ public interface Session extends SharedSessionContract {
* Create an {@link IdentifierLoadAccess} instance to retrieve the specified entity by * Create an {@link IdentifierLoadAccess} instance to retrieve the specified entity by
* primary key. * primary key.
* *
* @param entityName The name of the entity that will be retrieved * @param entityName
* @throws HibernateException If the specified entity name is not found * 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); public IdentifierLoadAccess<Object> byId(String entityName);
/** /**
* Create an {@link IdentifierLoadAccess} instance to retrieve the specified entity by * Create an {@link IdentifierLoadAccess} instance to retrieve the specified entity by
* primary key. * primary key.
* *
* @param entityClass The type of the entity that will be retrieved * @param entityClass
* @throws HibernateException If the specified Class is not an entity * The type of the entity that will be retrieved
*/ * @throws HibernateException
public <T> IdentifierLoadAccess<T> byId(Class<T> entityClass); * If the specified Class is not an entity
*/
/** public <T> IdentifierLoadAccess<T> byId(Class<T> entityClass);
* Create an {@link NaturalIdLoadAccess} instance to retrieve the specified entity by
* its natural id. /**
* * Create an {@link NaturalIdLoadAccess} instance to retrieve the specified entity by
* @param entityName The name of the entity that will be retrieved * its natural id.
* @throws HibernateException If the specified entity name is not found or if the entity does not have a natural id specified *
*/ * @param entityName
public NaturalIdLoadAccess<Object> byNaturalId(String 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
* Create an {@link NaturalIdLoadAccess} instance to retrieve the specified entity by */
* its natural id. public NaturalIdLoadAccess<Object> byNaturalId(String entityName);
*
* @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 * Create an {@link NaturalIdLoadAccess} instance to retrieve the specified entity by
*/ * its natural id.
public <T> NaturalIdLoadAccess<T> byNaturalId(Class<T> entityClass); *
* @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);
/** /**
* Enable the named filter for this current session. * Enable the named filter for this current session.

View File

@ -1,7 +1,7 @@
/* /*
* Hibernate, Relational Persistence for Idiomatic Java * 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 * indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are * statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc. * distributed under license by Red Hat Inc.
@ -28,598 +28,248 @@ import java.util.Map;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.LockMode; 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.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.ResolveNaturalIdEvent;
import org.hibernate.event.spi.ResolveNaturalIdEventListener; import org.hibernate.event.spi.ResolveNaturalIdEventListener;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper; import org.hibernate.pretty.MessageHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.tuple.StandardProperty; import org.hibernate.tuple.StandardProperty;
import org.hibernate.tuple.entity.EntityMetamodel; 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; import org.jboss.logging.Logger;
/** /**
* Defines the default load event listeners used by hibernate for loading entities * Defines the default load event listeners used by hibernate for loading entities
* in response to generated load events. * 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 REMOVED_ENTITY_MARKER = new Object();
public static final Object INCONSISTENT_RTN_CLASS_MARKER = new Object(); public static final Object INCONSISTENT_RTN_CLASS_MARKER = new Object();
public static final LockMode DEFAULT_LOCK_MODE = LockMode.NONE; public static final LockMode DEFAULT_LOCK_MODE = LockMode.NONE;
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class,
DefaultResolveNaturalIdEventListener.class.getName()); DefaultResolveNaturalIdEventListener.class.getName() );
/*
/* (non-Javadoc) * (non-Javadoc)
* @see org.hibernate.event.spi.ResolveNaturalIdEventListener#onResolveNaturalId(org.hibernate.event.spi.ResolveNaturalIdEvent) *
*/ * @see org.hibernate.event.spi.ResolveNaturalIdEventListener#onResolveNaturalId(org.hibernate.event.spi.
@Override * ResolveNaturalIdEvent)
public void onResolveNaturalId(ResolveNaturalIdEvent event) throws HibernateException { */
@Override
public void onResolveNaturalId(ResolveNaturalIdEvent event) throws HibernateException {
final SessionImplementor source = event.getSession(); final SessionImplementor source = event.getSession();
EntityPersister persister = source.getFactory().getEntityPersister( event.getEntityClassName() ); EntityPersister persister = source.getFactory().getEntityPersister( event.getEntityClassName() );
if ( persister == null ) { if ( persister == null ) {
throw new HibernateException( throw new HibernateException( "Unable to locate persister: " + event.getEntityClassName() );
"Unable to locate persister: " +
event.getEntityClassName()
);
} }
//Verify that the entity has a natural id and that the properties match up with the event. // Verify that the entity has a natural id and that the properties match up with the event.
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel(); final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final int[] naturalIdentifierProperties = entityMetamodel.getNaturalIdentifierProperties(); final int[] naturalIdentifierProperties = entityMetamodel.getNaturalIdentifierProperties();
if (naturalIdentifierProperties == null || naturalIdentifierProperties.length == 0) { if ( naturalIdentifierProperties == null || naturalIdentifierProperties.length == 0 ) {
throw new HibernateException(event.getEntityClassName() + " does not have a natural id"); throw new HibernateException( event.getEntityClassName() + " does not have a natural id" );
} }
final Map<String, Object> naturalIdParams = event.getNaturalId(); final Map<String, Object> naturalIdParams = event.getNaturalId();
if (naturalIdentifierProperties.length != naturalIdParams.size()) { 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(); final StandardProperty[] properties = entityMetamodel.getProperties();
for (int idPropIdx = 0; idPropIdx < naturalIdentifierProperties.length; idPropIdx++) { for ( int idPropIdx = 0; idPropIdx < naturalIdentifierProperties.length; idPropIdx++ ) {
final StandardProperty property = properties[naturalIdentifierProperties[idPropIdx]]; final StandardProperty property = properties[naturalIdentifierProperties[idPropIdx]];
final String name = property.getName(); final String name = property.getName();
if (!naturalIdParams.containsKey(name)) { 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 );
}
} }
final Serializable entityId = doResolveNaturalId(event, persister);
event.setEntityId(entityId);
}
final Serializable entityId = doResolveNaturalId( event, persister );
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.
* @return The loaded entity, or null.
*/
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() ) );
/** Serializable entityId = loadFromSessionCache( event, persister );
* Coordinates the efforts to load a given entity. First, an attempt is if ( entityId == REMOVED_ENTITY_MARKER ) {
* made to load the entity from the session-level cache. If not found there, LOG.debugf( "Load request found matching entity in context, but it is scheduled for removal; returning null" );
* an attempt is made to locate it in second-level cache. Lastly, an return null;
* attempt is made to load it directly from the datasource. }
* if ( entityId == INCONSISTENT_RTN_CLASS_MARKER ) {
* @param event The load event LOG.debugf( "Load request found matching entity in context, but the matched entity was of an inconsistent return type; returning null" );
* @param persister The persister for the entity being requested for load return null;
* @param keyToLoad The EntityKey representing the entity to be loaded. }
* @param options The load options. if ( entityId != null ) {
* @return The loaded entity, or null. if ( LOG.isTraceEnabled() )
*/ LOG.trace( "Resolved object in session cache: "
protected Serializable doResolveNaturalId( + MessageHelper.infoString( persister, event.getNaturalId(), event.getSession().getFactory() ) );
final ResolveNaturalIdEvent event, return entityId;
final EntityPersister persister) { }
if (LOG.isTraceEnabled()) LOG.trace("Attempting to resolve: " entityId = loadFromSecondLevelCache( event, persister );
+ MessageHelper.infoString(persister, if ( entityId != null ) {
event.getNaturalId(), if ( LOG.isTraceEnabled() )
event.getSession().getFactory())); LOG.trace( "Resolved object in second-level cache: "
+ MessageHelper.infoString( persister, event.getNaturalId(), event.getSession().getFactory() ) );
return entityId;
}
Serializable entityId = loadFromSessionCache(event, persister); if ( LOG.isTraceEnabled() )
if ( entityId == REMOVED_ENTITY_MARKER ) { LOG.trace( "Object not resolved in any cache: "
LOG.debugf("Load request found matching entity in context, but it is scheduled for removal; returning null"); + MessageHelper.infoString( persister, event.getNaturalId(), event.getSession().getFactory() ) );
return null;
}
if ( entityId == INCONSISTENT_RTN_CLASS_MARKER ) {
LOG.debugf("Load request found matching entity in context, but the matched entity was of an inconsistent return type; returning null");
return null;
}
if ( entityId != null ) {
if (LOG.isTraceEnabled()) LOG.trace("Resolved object in session cache: "
+ MessageHelper.infoString(persister,
event.getNaturalId(),
event.getSession().getFactory()));
return entityId;
}
entityId = loadFromSecondLevelCache(event, persister); return loadFromDatasource( event, persister );
if ( entityId != null ) { }
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, * Attempts to locate the entity in the session-level cache.
event.getNaturalId(), * <p/>
event.getSession().getFactory())); * 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.
*
* @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.
*/
protected Serializable loadFromSessionCache(final ResolveNaturalIdEvent event, final EntityPersister persister)
throws HibernateException {
// SessionImplementor session = event.getSession();
// Object old = session.getEntityUsingInterceptor( keyToLoad );
//
// if ( old != null ) {
// // this object was already loaded
// EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
// if ( options.isCheckDeleted() ) {
// Status status = oldEntry.getStatus();
// if ( status == Status.DELETED || status == Status.GONE ) {
// return REMOVED_ENTITY_MARKER;
// }
// }
// if ( options.isAllowNulls() ) {
// final EntityPersister persister = event.getSession().getFactory().getEntityPersister(
// keyToLoad.getEntityName() );
// if ( ! persister.isInstance( old ) ) {
// return INCONSISTENT_RTN_CLASS_MARKER;
// }
// }
// upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() );
// }
return loadFromDatasource(event, persister); return null;
} }
/** /**
* Attempts to locate the entity in the session-level cache. * Attempts to load the entity from the second-level cache.
* <p/> *
* If allowed to return nulls, then if the entity happens to be found in * @param event
* the session cache, we check the entity type for proper handling * The load event
* of entity hierarchies. * @param persister
* <p/> * The persister for the entity being requested for load
* If checkDeleted was set to true, then if the entity is found in the * @param options
* session-level cache, it's current status within the session cache * The load options.
* is checked to see if it has previously been scheduled for deletion. * @return The entity from the second-level cache, or null.
* */
* @param event The load event protected Serializable loadFromSecondLevelCache(final ResolveNaturalIdEvent event, final EntityPersister persister) {
* @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.
*/
protected Serializable loadFromSessionCache(
final ResolveNaturalIdEvent event,
final EntityPersister persister) throws HibernateException {
// SessionImplementor session = event.getSession();
// Object old = session.getEntityUsingInterceptor( keyToLoad );
//
// if ( old != null ) {
// // this object was already loaded
// EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
// if ( options.isCheckDeleted() ) {
// Status status = oldEntry.getStatus();
// if ( status == Status.DELETED || status == Status.GONE ) {
// return REMOVED_ENTITY_MARKER;
// }
// }
// if ( options.isAllowNulls() ) {
// final EntityPersister persister = event.getSession().getFactory().getEntityPersister( keyToLoad.getEntityName() );
// if ( ! persister.isInstance( old ) ) {
// return INCONSISTENT_RTN_CLASS_MARKER;
// }
// }
// upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() );
// }
return null; // final SessionImplementor source = event.getSession();
} //
// final boolean useCache = persister.hasCache()
// && source.getCacheMode().isGetEnabled();
//
// if ( useCache ) {
//
// final SessionFactoryImplementor factory = source.getFactory();
//
// final CacheKey ck = source.generateCacheKey(
// event.getNaturalId(),
// persister.getIdentifierType(),
// persister.getRootEntityName()
// );
// Object ce = persister.getCacheAccessStrategy().get( ck, source.getTimestamp() );
// if ( factory.getStatistics().isStatisticsEnabled() ) {
// if ( ce == null ) {
// factory.getStatisticsImplementor().secondLevelCacheMiss(
// persister.getCacheAccessStrategy().getRegion().getName()
// );
// }
// else {
// factory.getStatisticsImplementor().secondLevelCacheHit(
// persister.getCacheAccessStrategy().getRegion().getName()
// );
// }
// }
//
// if ( ce != null ) {
// CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( ce, factory );
//
// // Entity was found in second-level cache...
// return assembleCacheEntry(
// entry,
// event.getEntityId(),
// persister,
// event
// );
// }
// }
/** return null;
* 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.
* @return The entity from the second-level cache, or null.
*/
protected Serializable loadFromSecondLevelCache(
final ResolveNaturalIdEvent event,
final EntityPersister persister) {
// final SessionImplementor source = event.getSession(); /**
// * Performs the process of loading an entity from the configured
// final boolean useCache = persister.hasCache() * underlying datasource.
// && source.getCacheMode().isGetEnabled(); *
// * @param event
// if ( useCache ) { * The load event
// * @param persister
// final SessionFactoryImplementor factory = source.getFactory(); * The persister for the entity being requested for load
// * @param keyToLoad
// final CacheKey ck = source.generateCacheKey( * The EntityKey representing the entity to be loaded.
// event.getNaturalId(), * @param options
// persister.getIdentifierType(), * The load options.
// persister.getRootEntityName() * @return The object loaded from the datasource, or null if not found.
// ); */
// Object ce = persister.getCacheAccessStrategy().get( ck, source.getTimestamp() ); protected Serializable loadFromDatasource(final ResolveNaturalIdEvent event, final EntityPersister persister) {
// if ( factory.getStatistics().isStatisticsEnabled() ) { final SessionImplementor source = event.getSession();
// if ( ce == null ) {
// factory.getStatisticsImplementor().secondLevelCacheMiss(
// persister.getCacheAccessStrategy().getRegion().getName()
// );
// }
// else {
// factory.getStatisticsImplementor().secondLevelCacheHit(
// persister.getCacheAccessStrategy().getRegion().getName()
// );
// }
// }
//
// if ( ce != null ) {
// CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( ce, factory );
//
// // Entity was found in second-level cache...
// return assembleCacheEntry(
// entry,
// event.getEntityId(),
// persister,
// event
// );
// }
// }
return null; return persister.loadEntityIdByNaturalId( event.getNaturalId(), event.getLockOptions(), event.getSession() );
} }
/**
* 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.
* @return The object loaded from the datasource, or null if not found.
*/
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 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

@ -247,12 +247,12 @@ public class EventListenerRegistryImpl implements EventListenerRegistry {
workMap workMap
); );
// resolve natural-id listeners // resolve natural-id listeners
prepareListeners( prepareListeners(
RESOLVE_NATURAL_ID, RESOLVE_NATURAL_ID,
new DefaultResolveNaturalIdEventListener(), new DefaultResolveNaturalIdEventListener(),
workMap workMap
); );
// load-collection listeners // load-collection listeners
prepareListeners( prepareListeners(

View File

@ -40,8 +40,8 @@ import org.hibernate.HibernateException;
public class EventType<T> { public class EventType<T> {
public static final EventType<LoadEventListener> LOAD public static final EventType<LoadEventListener> LOAD
= new EventType<LoadEventListener>( "load", LoadEventListener.class ); = new EventType<LoadEventListener>( "load", LoadEventListener.class );
public static final EventType<ResolveNaturalIdEventListener> RESOLVE_NATURAL_ID public static final EventType<ResolveNaturalIdEventListener> RESOLVE_NATURAL_ID
= new EventType<ResolveNaturalIdEventListener>( "resolve-natural-id", ResolveNaturalIdEventListener.class ); = new EventType<ResolveNaturalIdEventListener>( "resolve-natural-id", ResolveNaturalIdEventListener.class );
public static final EventType<InitializeCollectionEventListener> INIT_COLLECTION public static final EventType<InitializeCollectionEventListener> INIT_COLLECTION
= new EventType<InitializeCollectionEventListener>( "load-collection", InitializeCollectionEventListener.class ); = new EventType<InitializeCollectionEventListener>( "load-collection", InitializeCollectionEventListener.class );

View File

@ -31,9 +31,9 @@ import org.hibernate.LockMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
/** /**
* Defines an event class for the resolving of an entity id from the entity's natural-id * 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 class ResolveNaturalIdEvent extends AbstractEvent {
public static final LockMode DEFAULT_LOCK_MODE = LockMode.NONE; public static final LockMode DEFAULT_LOCK_MODE = LockMode.NONE;
@ -44,29 +44,30 @@ public class ResolveNaturalIdEvent extends AbstractEvent {
private Serializable entityId; private Serializable entityId;
public ResolveNaturalIdEvent(Map<String, Object> naturalId, String entityClassName, EventSource source) { public ResolveNaturalIdEvent(Map<String, Object> naturalId, String entityClassName, EventSource source) {
this(naturalId, entityClassName, new LockOptions(), source); 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,
super(source); EventSource source) {
super( source );
if ( naturalId == null || naturalId.isEmpty() ) { if ( naturalId == null || naturalId.isEmpty() ) {
throw new IllegalArgumentException("id to load is required for loading"); throw new IllegalArgumentException( "id to load is required for loading" );
} }
if ( lockOptions.getLockMode() == LockMode.WRITE ) { if ( lockOptions.getLockMode() == LockMode.WRITE ) {
throw new IllegalArgumentException("Invalid lock mode for loading"); throw new IllegalArgumentException( "Invalid lock mode for loading" );
} }
else if ( lockOptions.getLockMode() == null ) { else if ( lockOptions.getLockMode() == null ) {
lockOptions.setLockMode(DEFAULT_LOCK_MODE); lockOptions.setLockMode( DEFAULT_LOCK_MODE );
} }
this.naturalId = naturalId; this.naturalId = naturalId;
this.entityClassName = entityClassName; this.entityClassName = entityClassName;
} }
public Map<String, Object> getNaturalId() { public Map<String, Object> getNaturalId() {
return Collections.unmodifiableMap(naturalId); return Collections.unmodifiableMap( naturalId );
} }
public void setNaturalId(Map<String, Object> naturalId) { public void setNaturalId(Map<String, Object> naturalId) {
@ -89,11 +90,11 @@ public class ResolveNaturalIdEvent extends AbstractEvent {
this.entityId = entityId; this.entityId = entityId;
} }
public LockOptions getLockOptions() { public LockOptions getLockOptions() {
return lockOptions; return lockOptions;
} }
public void setLockOptions(LockOptions lockOptions) { public void setLockOptions(LockOptions lockOptions) {
this.lockOptions = lockOptions; this.lockOptions = lockOptions;
} }
} }

View File

@ -29,17 +29,18 @@ import org.hibernate.HibernateException;
/** /**
* Defines the contract for handling of resolve natural id events generated from a session. * 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 { public interface ResolveNaturalIdEventListener extends Serializable {
/** /**
* Handle the given resolve natural id event. * Handle the given resolve natural id event.
* *
* @param event The resolve natural id event to be handled. * @param event
* @throws HibernateException * The resolve natural id event to be handled.
*/ * @throws HibernateException
*/
public void onResolveNaturalId(ResolveNaturalIdEvent event) throws HibernateException; public void onResolveNaturalId(ResolveNaturalIdEvent event) throws HibernateException;
} }

View File

@ -844,11 +844,11 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
} }
public Object load(Class entityClass, Serializable id) throws HibernateException { public Object load(Class entityClass, Serializable id) throws HibernateException {
return this.byId(entityClass).getReference(id); return this.byId(entityClass).getReference(id);
} }
public Object load(String entityName, Serializable id) throws HibernateException { public Object load(String entityName, Serializable id) throws HibernateException {
return this.byId(entityName).getReference(id); return this.byId(entityName).getReference(id);
} }
public Object get(Class entityClass, Serializable id) throws HibernateException { public Object get(Class entityClass, Serializable id) throws HibernateException {
@ -891,70 +891,58 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
} }
public Object load(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException { public Object load(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException {
return this.byId(entityClass).with(lockMode).getReference(id); return this.byId( entityClass ).with( lockMode ).getReference( id );
} }
public Object load(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException { public Object load(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException {
return this.byId(entityClass).with(lockOptions).getReference(id); return this.byId( entityClass ).with( lockOptions ).getReference( id );
} }
public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException { public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException {
return this.byId(entityName).with(lockMode).getReference(id); return this.byId( entityName ).with( lockMode ).getReference( id );
} }
public Object load(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException { public Object load(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException {
return this.byId(entityName).with(lockOptions).getReference(id); return this.byId( entityName ).with( lockOptions ).getReference( id );
} }
public Object get(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException { public Object get(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException {
return this.byId(entityClass).with(lockMode).load(id); return this.byId( entityClass ).with( lockMode ).load( id );
} }
public Object get(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException { public Object get(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException {
return this.byId(entityClass).with(lockOptions).load(id); return this.byId( entityClass ).with( lockOptions ).load( id );
} }
public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException { public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException {
return this.byId(entityName).with(lockMode).load(id); return this.byId( entityName ).with( lockMode ).load( id );
} }
public Object get(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException { public Object get(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException {
return this.byId(entityName).with(lockOptions).load(id); return this.byId( entityName ).with( lockOptions ).load( id );
} }
/* (non-Javadoc) @Override
* @see org.hibernate.Session#byId(java.lang.String) public IdentifierLoadAccessImpl<Object> byId(String entityName) {
*/ return new IdentifierLoadAccessImpl<Object>( entityName, Object.class );
@Override }
public IdentifierLoadAccessImpl<Object> byId(String entityName) {
return new IdentifierLoadAccessImpl<Object>(entityName, Object.class);
}
/* (non-Javadoc) @Override
* @see org.hibernate.Session#byId(java.lang.Class) public <T> IdentifierLoadAccessImpl<T> byId(Class<T> entityClass) {
*/ return new IdentifierLoadAccessImpl<T>( entityClass.getName(), entityClass );
@Override }
public <T> IdentifierLoadAccessImpl<T> byId(Class<T> entityClass) {
return new IdentifierLoadAccessImpl<T>(entityClass.getName(), entityClass);
}
/* (non-Javadoc) @Override
* @see org.hibernate.Session#byNaturalId(java.lang.String) public NaturalIdLoadAccess<Object> byNaturalId(String entityName) {
*/ return new NaturalIdLoadAccessImpl<Object>( entityName, Object.class );
@Override }
public NaturalIdLoadAccess<Object> byNaturalId(String entityName) {
return new NaturalIdLoadAccessImpl<Object>(entityName, Object.class);
}
/* (non-Javadoc) @Override
* @see org.hibernate.Session#byNaturalId(java.lang.Class) public <T> NaturalIdLoadAccess<T> byNaturalId(Class<T> entityClass) {
*/ return new NaturalIdLoadAccessImpl<T>( entityClass.getName(), entityClass );
@Override }
public <T> NaturalIdLoadAccess<T> byNaturalId(Class<T> entityClass) {
return new NaturalIdLoadAccessImpl<T>(entityClass.getName(), entityClass);
}
private void fireLoad(LoadEvent event, LoadType loadType) { private void fireLoad(LoadEvent event, LoadType loadType) {
errorIfClosed(); errorIfClosed();
checkTransactionSynchStatus(); checkTransactionSynchStatus();
for ( LoadEventListener listener : listeners( EventType.LOAD ) ) { for ( LoadEventListener listener : listeners( EventType.LOAD ) ) {
@ -962,13 +950,13 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
} }
} }
private void fireResolveNaturalId(ResolveNaturalIdEvent event) { private void fireResolveNaturalId(ResolveNaturalIdEvent event) {
errorIfClosed(); errorIfClosed();
checkTransactionSynchStatus(); checkTransactionSynchStatus();
for ( ResolveNaturalIdEventListener listener : listeners( EventType.RESOLVE_NATURAL_ID ) ) { for ( ResolveNaturalIdEventListener listener : listeners( EventType.RESOLVE_NATURAL_ID ) ) {
listener.onResolveNaturalId(event); listener.onResolveNaturalId( event );
} }
} }
// refresh() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // refresh() operations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -2180,157 +2168,132 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
} }
} }
private class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T> { private class IdentifierLoadAccessImpl<T> implements IdentifierLoadAccess<T> {
private final String entityName; private final String entityName;
private final Class<T> entityClass; private final Class<T> entityClass;
private LockOptions lockOptions; private LockOptions lockOptions;
/**
*/
private IdentifierLoadAccessImpl(String entityName, Class<T> entityClass) {
this.entityName = entityName;
this.entityClass = entityClass;
}
/* (non-Javadoc) private IdentifierLoadAccessImpl(String entityName, Class<T> entityClass) {
* @see org.hibernate.IdentifierLoadAccess#with(org.hibernate.LockOptions) this.entityName = entityName;
*/ this.entityClass = entityClass;
@Override }
public final IdentifierLoadAccessImpl<T> with(LockOptions lockOptions) {
this.lockOptions = lockOptions;
return this;
}
/** @Override
public final IdentifierLoadAccessImpl<T> with(LockOptions lockOptions) {
this.lockOptions = lockOptions;
return this;
}
/**
* Support for legacy {@link Session#load(Class, Serializable, LockMode)} and {@link Session#load(String, Serializable, LockMode) * Support for legacy {@link Session#load(Class, Serializable, LockMode)} and {@link Session#load(String, Serializable, LockMode)
* @deprecated * @deprecated
*/ */
@Deprecated @Deprecated
public final IdentifierLoadAccessImpl<T> with(LockMode lockMode) { public final IdentifierLoadAccessImpl<T> with(LockMode lockMode) {
this.lockOptions = new LockOptions(); this.lockOptions = new LockOptions();
this.lockOptions.setLockMode(lockMode); this.lockOptions.setLockMode( lockMode );
return this; return this;
} }
/* (non-Javadoc) @Override
* @see org.hibernate.IdentifierLoadAccess#getReference(java.io.Serializable) public final Object getReference(Serializable id) {
*/ if ( this.lockOptions != null ) {
@Override LoadEvent event = new LoadEvent( id, entityName, lockOptions, SessionImpl.this );
public final Object getReference(Serializable id) { fireLoad( event, LoadEventListener.LOAD );
if (this.lockOptions != null) { return event.getResult();
LoadEvent event = new LoadEvent(id, entityName, lockOptions, SessionImpl.this); }
fireLoad( event, LoadEventListener.LOAD );
return event.getResult();
}
LoadEvent event = new LoadEvent(id, entityName, false, SessionImpl.this);
boolean success = false;
try {
fireLoad( event, LoadEventListener.LOAD );
if ( event.getResult() == null ) {
getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
}
success = true;
return event.getResult();
}
finally {
afterOperation(success);
}
}
/* (non-Javadoc) LoadEvent event = new LoadEvent( id, entityName, false, SessionImpl.this );
* @see org.hibernate.IdentifierLoadAccess#load(java.io.Serializable) boolean success = false;
*/ try {
@Override fireLoad( event, LoadEventListener.LOAD );
public final Object load(Serializable id) { if ( event.getResult() == null ) {
if (this.lockOptions != null) { getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id );
LoadEvent event = new LoadEvent(id, entityName, lockOptions, SessionImpl.this); }
fireLoad( event, LoadEventListener.GET ); success = true;
return event.getResult(); return event.getResult();
} }
finally {
LoadEvent event = new LoadEvent(id, entityName, false, SessionImpl.this); afterOperation( success );
boolean success = false; }
try { }
fireLoad(event, LoadEventListener.GET);
success = true; @Override
return event.getResult(); public final Object load(Serializable id) {
} if ( this.lockOptions != null ) {
finally { LoadEvent event = new LoadEvent( id, entityName, lockOptions, SessionImpl.this );
afterOperation(success); fireLoad( event, LoadEventListener.GET );
} return event.getResult();
} }
LoadEvent event = new LoadEvent( id, entityName, false, SessionImpl.this );
boolean success = false;
try {
fireLoad( event, LoadEventListener.GET );
success = true;
return event.getResult();
}
finally {
afterOperation( success );
}
}
} }
private class NaturalIdLoadAccessImpl<T> implements NaturalIdLoadAccess<T> {
private final String entityName;
private final Class<T> entityClass;
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) private class NaturalIdLoadAccessImpl<T> implements NaturalIdLoadAccess<T> {
* @see org.hibernate.IdentifierLoadAccess#with(org.hibernate.LockOptions) private final String entityName;
*/ private final Class<T> entityClass;
@Override private final Map<String, Object> naturalIdParameters = new LinkedHashMap<String, Object>();
public final NaturalIdLoadAccess<T> with(LockOptions lockOptions) { private 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);
return this;
}
protected Serializable resolveNaturalId() {
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);
if (this.lockOptions != null) {
identifierLoadAccess.with(lockOptions);
}
return identifierLoadAccess;
}
/* (non-Javadoc) private NaturalIdLoadAccessImpl(String entityName, Class<T> entityClass) {
* @see org.hibernate.NaturalIdLoadAccess#getReference() this.entityName = entityName;
*/ this.entityClass = entityClass;
@Override }
public final Object getReference() {
final Serializable entityId = resolveNaturalId();
if (entityId == null) {
return null;
}
return this.getIdentifierLoadAccess().getReference(entityId);
}
/* (non-Javadoc) @Override
* @see org.hibernate.NaturalIdLoadAccess#load() public final NaturalIdLoadAccess<T> with(LockOptions lockOptions) {
*/ this.lockOptions = lockOptions;
@Override return this;
public final Object load() { }
final Serializable entityId = resolveNaturalId();
if (entityId == null) { @Override
return null; public NaturalIdLoadAccess<T> using(String attributeName, Object value) {
} naturalIdParameters.put( attributeName, value );
return this.getIdentifierLoadAccess().load(entityId); return this;
} }
}
protected Serializable resolveNaturalId() {
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 );
if ( this.lockOptions != null ) {
identifierLoadAccess.with( lockOptions );
}
return identifierLoadAccess;
}
@Override
public final Object getReference() {
final Serializable entityId = resolveNaturalId();
if ( entityId == null ) {
return null;
}
return this.getIdentifierLoadAccess().getReference( entityId );
}
@Override
public final Object load() {
final Serializable entityId = resolveNaturalId();
if ( entityId == null ) {
return null;
}
return this.getIdentifierLoadAccess().load( entityId );
}
}
} }

View File

@ -3390,7 +3390,7 @@ public abstract class AbstractEntityPersister
if (sqlUpdateGeneratedValuesSelectString != null) LOG.debugf("Update-generated property select: %s", if (sqlUpdateGeneratedValuesSelectString != null) LOG.debugf("Update-generated property select: %s",
sqlUpdateGeneratedValuesSelectString); sqlUpdateGeneratedValuesSelectString);
if (sqlEntityIdByNaturalIdString != null) LOG.debugf("Id by Natural Id: %s", if (sqlEntityIdByNaturalIdString != null) LOG.debugf("Id by Natural Id: %s",
sqlEntityIdByNaturalIdString); sqlEntityIdByNaturalIdString);
} }
} }
@ -4503,103 +4503,95 @@ public abstract class AbstractEntityPersister
} }
} }
/* (non-Javadoc) @Override
* @see org.hibernate.persister.entity.EntityPersister#loadEntityIdByNaturalId(java.util.Map, org.hibernate.engine.spi.SessionImplementor) public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
*/ SessionImplementor session) {
@Override if ( !hasNaturalIdentifier() ) {
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions, SessionImplementor session) { throw new MappingException( "persistent class did not define a natural-id : "
if ( !hasNaturalIdentifier() ) { + MessageHelper.infoString( this ) );
throw new MappingException( "persistent class did not define a natural-id : " + MessageHelper.infoString( this ) ); }
} if ( LOG.isTraceEnabled() )
if (LOG.isTraceEnabled()) LOG.trace("Getting entity id for natural-id for: " LOG.trace( "Getting entity id for natural-id for: "
+ MessageHelper.infoString(this, naturalIdParameters, getFactory())); + MessageHelper.infoString( this, naturalIdParameters, getFactory() ) );
try { try {
PreparedStatement ps = session.getTransactionCoordinator() PreparedStatement ps = session.getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer()
.getJdbcCoordinator() .prepareStatement( sqlEntityIdByNaturalIdString );
.getStatementPreparer() try {
.prepareStatement( sqlEntityIdByNaturalIdString ); int positions = 1;
try { final int[] naturalIdPropertyIndexes = this.getNaturalIdentifierProperties();
int positions = 1; for ( int propIdx = 0; propIdx < naturalIdPropertyIndexes.length; propIdx++ ) {
final int[] naturalIdPropertyIndexes = this.getNaturalIdentifierProperties(); final int naturalIdIdx = naturalIdPropertyIndexes[propIdx];
for (int propIdx = 0; propIdx < naturalIdPropertyIndexes.length; propIdx++) {
final int naturalIdIdx = naturalIdPropertyIndexes[propIdx];
final StandardProperty[] properties = entityMetamodel.getProperties();
final StandardProperty property = properties[naturalIdIdx];
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());
}
ResultSet rs = ps.executeQuery();
try {
//if there is no resulting row, return null
if ( !rs.next() ) {
return null;
}
//entity ID has to be serializable right? final StandardProperty[] properties = entityMetamodel.getProperties();
return (Serializable) getIdentifierType().hydrate(rs, getIdentifierAliases(), session, null); final StandardProperty property = properties[naturalIdIdx];
}
finally {
rs.close();
}
}
finally {
ps.close();
}
}
catch ( SQLException e ) {
throw getFactory().getSQLExceptionHelper().convert(
e,
"could not retrieve entity id: " + MessageHelper.infoString( this, naturalIdParameters, getFactory() ),
sqlEntityIdByNaturalIdString
);
}
}
/** final Object value = naturalIdParameters.get( property.getName() );
* @return
*/ final Type propertyType = property.getType();
private String generateEntityIdByNaturalIdSql() { propertyType.nullSafeSet( ps, value, positions, session );
Select select = new Select( getFactory().getDialect() ); positions += propertyType.getColumnSpan( session.getFactory() );
if ( getFactory().getSettings().isCommentsEnabled() ) { }
select.setComment( "get current natural-id->entity-id state " + getEntityName() ); ResultSet rs = ps.executeQuery();
} try {
// if there is no resulting row, return null
final String rootAlias = getRootAlias(); if ( !rs.next() ) {
return null;
select.setSelectClause( identifierSelectFragment(rootAlias, "") ); }
select.setFromClause( fromTableFragment( rootAlias ) + fromJoinFragment( rootAlias, true, false ) );
// entity ID has to be serializable right?
final StringBuilder whereClause = new StringBuilder(); return (Serializable) getIdentifierType().hydrate( rs, getIdentifierAliases(), session, null );
final int[] propertyTableNumbers = getPropertyTableNumbers(); }
final int[] naturalIdPropertyIndexes = this.getNaturalIdentifierProperties(); finally {
for (int propIdx = 0; propIdx < naturalIdPropertyIndexes.length; propIdx++) { rs.close();
if (propIdx > 0) { }
whereClause.append(" and "); }
} finally {
ps.close();
final int naturalIdIdx = naturalIdPropertyIndexes[propIdx]; }
final String tableAlias = generateTableAlias( rootAlias, propertyTableNumbers[naturalIdIdx] ); }
catch ( SQLException e ) {
final String[] propertyColumnNames = getPropertyColumnNames(naturalIdIdx); throw getFactory().getSQLExceptionHelper().convert(
final String[] aliasedPropertyColumns = StringHelper.qualify( rootAlias, propertyColumnNames ); e,
"could not retrieve entity id: "
whereClause.append( StringHelper.join( "=? and ", aliasedPropertyColumns ) ).append( "=?" ); + MessageHelper.infoString( this, naturalIdParameters, getFactory() ),
} sqlEntityIdByNaturalIdString );
}
whereClause.append( whereJoinFragment( getRootAlias(), true, false ) ); }
String sql = select.setOuterJoins( "", "" ) private String generateEntityIdByNaturalIdSql() {
.setWhereClause( whereClause.toString() ) Select select = new Select( getFactory().getDialect() );
.toStatementString(); if ( getFactory().getSettings().isCommentsEnabled() ) {
return sql; select.setComment( "get current natural-id->entity-id state " + getEntityName() );
} }
final String rootAlias = getRootAlias();
select.setSelectClause( identifierSelectFragment( rootAlias, "" ) );
select.setFromClause( fromTableFragment( rootAlias ) + fromJoinFragment( rootAlias, true, false ) );
final StringBuilder whereClause = new StringBuilder();
final int[] propertyTableNumbers = getPropertyTableNumbers();
final int[] naturalIdPropertyIndexes = this.getNaturalIdentifierProperties();
for ( int propIdx = 0; propIdx < naturalIdPropertyIndexes.length; propIdx++ ) {
if ( propIdx > 0 ) {
whereClause.append( " and " );
}
final int naturalIdIdx = naturalIdPropertyIndexes[propIdx];
final String tableAlias = generateTableAlias( rootAlias, propertyTableNumbers[naturalIdIdx] );
final String[] propertyColumnNames = getPropertyColumnNames( naturalIdIdx );
final String[] aliasedPropertyColumns = StringHelper.qualify( rootAlias, propertyColumnNames );
whereClause.append( StringHelper.join( "=? and ", aliasedPropertyColumns ) ).append( "=?" );
}
whereClause.append( whereJoinFragment( getRootAlias(), true, false ) );
String sql = select.setOuterJoins( "", "" ).setWhereClause( whereClause.toString() ).toStatementString();
return sql;
}
protected String concretePropertySelectFragmentSansLeadingComma(String alias, boolean[] include) { protected String concretePropertySelectFragmentSansLeadingComma(String alias, boolean[] include) {
String concretePropertySelectFragment = concretePropertySelectFragment( alias, include ); String concretePropertySelectFragment = concretePropertySelectFragment( alias, include );

View File

@ -320,11 +320,12 @@ public interface EntityPersister extends OptimisticCacheSource {
* @return True if the entity has properties mapped as lazy; false otherwise. * @return True if the entity has properties mapped as lazy; false otherwise.
*/ */
public boolean hasLazyProperties(); public boolean hasLazyProperties();
/** /**
* Load the id for the entity based on the natural id. * 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. * Load an instance of the persistent class.

View File

@ -105,66 +105,61 @@ public class ImmutableNaturalIdTest extends AbstractJPATest {
t.commit(); t.commit();
s.close(); s.close();
} }
@Test
public void testNaturalIdLoadAccessCache() {
Session s = openSession();
s.beginTransaction();
User u = new User( "steve", "superSecret" );
s.persist( u );
s.getTransaction().commit();
s.close();
@Test sessionFactory().getStatistics().clear();
public void testNaturalIdLoadAccessCache() {
Session s = openSession();
s.beginTransaction();
User u = new User( "steve", "superSecret" );
s.persist( u );
s.getTransaction().commit();
s.close();
sessionFactory().getStatistics().clear(); s = openSession();
// sessionFactory().getStatistics().logSummary(); s.beginTransaction();
u = (User) s.byNaturalId( User.class ).using( "userName", "steve" ).load();
assertNotNull( u );
s.getTransaction().commit();
s.close();
s = openSession(); assertEquals( 1, sessionFactory().getStatistics().getEntityLoadCount() );
s.beginTransaction(); assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCacheMissCount() );
u = ( User )s.byNaturalId(User.class).using( "userName", "steve" ).load(); assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCacheHitCount() );
assertNotNull( u ); assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCachePutCount() );
s.getTransaction().commit(); assertEquals( 0, sessionFactory().getStatistics().getQueryExecutionCount() );
s.close(); assertEquals( 0, sessionFactory().getStatistics().getQueryCacheHitCount() );
assertEquals( 0, sessionFactory().getStatistics().getQueryCachePutCount() );
// sessionFactory().getStatistics().logSummary();
assertEquals( 1, sessionFactory().getStatistics().getEntityLoadCount() ); s = openSession();
assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCacheMissCount() ); s.beginTransaction();
assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCacheHitCount() ); User v = new User( "gavin", "supsup" );
assertEquals( 0, sessionFactory().getStatistics().getSecondLevelCachePutCount() ); s.persist( v );
assertEquals( 0, sessionFactory().getStatistics().getQueryExecutionCount() ); s.getTransaction().commit();
assertEquals( 0, sessionFactory().getStatistics().getQueryCacheHitCount() ); s.close();
assertEquals( 0, sessionFactory().getStatistics().getQueryCachePutCount() );
s = openSession(); sessionFactory().getStatistics().clear();
s.beginTransaction();
User v = new User( "gavin", "supsup" );
s.persist( v );
s.getTransaction().commit();
s.close();
sessionFactory().getStatistics().clear(); s = openSession();
s.beginTransaction();
u = (User) s.byNaturalId( User.class ).using( "userName", "steve" ).load();
assertNotNull( u );
assertEquals( sessionFactory().getStatistics().getQueryExecutionCount(), 0 );
assertEquals( sessionFactory().getStatistics().getQueryCacheHitCount(), 0 );
u = (User) s.byNaturalId( User.class ).using( "userName", "steve" ).load();
assertNotNull( u );
assertEquals( sessionFactory().getStatistics().getQueryExecutionCount(), 0 );
assertEquals( sessionFactory().getStatistics().getQueryCacheHitCount(), 0 );
s.getTransaction().commit();
s.close();
s = openSession(); s = openSession();
s.beginTransaction(); s.beginTransaction();
u = ( User )s.byNaturalId(User.class).using( "userName", "steve" ).load(); s.createQuery( "delete User" ).executeUpdate();
assertNotNull( u ); s.getTransaction().commit();
assertEquals( sessionFactory().getStatistics().getQueryExecutionCount(), 0 ); s.close();
assertEquals( sessionFactory().getStatistics().getQueryCacheHitCount(), 0 ); }
u = ( User )s.byNaturalId(User.class).using( "userName", "steve" ).load();
assertNotNull( u );
assertEquals( sessionFactory().getStatistics().getQueryExecutionCount(), 0 );
assertEquals( sessionFactory().getStatistics().getQueryCacheHitCount(), 0 );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
s.createQuery( "delete User" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
@Test @Test
public void testNaturalIdCache() { public void testNaturalIdCache() {

View File

@ -238,14 +238,14 @@ public class GoofyPersisterClassProvider implements PersisterClassResolver {
public Object[] getNaturalIdentifierSnapshot(Serializable id, SessionImplementor session) { public Object[] getNaturalIdentifierSnapshot(Serializable id, SessionImplementor session) {
return new Object[0]; return new Object[0];
} }
@Override
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
SessionImplementor session) {
return null;
}
@Override @Override
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
SessionImplementor session) {
return null;
}
@Override
public IdentifierGenerator getIdentifierGenerator() { public IdentifierGenerator getIdentifierGenerator() {
return null; return null;
} }

View File

@ -601,14 +601,14 @@ public class CustomPersister implements EntityPersister {
public Object[] getNaturalIdentifierSnapshot(Serializable id, SessionImplementor session) throws HibernateException { public Object[] getNaturalIdentifierSnapshot(Serializable id, SessionImplementor session) throws HibernateException {
return null; return null;
} }
@Override
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
SessionImplementor session) {
return null;
}
@Override @Override
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
SessionImplementor session) {
return null;
}
@Override
public Comparator getVersionComparator() { public Comparator getVersionComparator() {
return null; return null;
} }

View File

@ -260,12 +260,12 @@ public class PersisterClassProviderTest {
public Object[] getNaturalIdentifierSnapshot(Serializable id, SessionImplementor session) { public Object[] getNaturalIdentifierSnapshot(Serializable id, SessionImplementor session) {
return new Object[0]; return new Object[0];
} }
@Override @Override
public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions, public Serializable loadEntityIdByNaturalId(Map<String, ?> naturalIdParameters, LockOptions lockOptions,
SessionImplementor session) { SessionImplementor session) {
return null; return null;
} }
@Override @Override
public IdentifierGenerator getIdentifierGenerator() { public IdentifierGenerator getIdentifierGenerator() {