HHH-4704 - Pass session into EntityTuplizer#setIdentifier

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18697 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-02-04 21:05:38 +00:00
parent b84e1fc5fc
commit 80f250fca5
16 changed files with 238 additions and 39 deletions

View File

@ -74,7 +74,7 @@ public final class EntityIdentityInsertAction extends EntityAction {
}
//need to do that here rather than in the save event listener to let
//the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
persister.setIdentifier( instance, generatedId, session.getEntityMode() );
persister.setIdentifier( instance, generatedId, session );
}

View File

@ -191,7 +191,7 @@ public abstract class AbstractSaveEventListener extends AbstractReassociateEvent
throw new NonUniqueObjectException( id, persister.getEntityName() );
}
}
persister.setIdentifier( entity, id, source.getEntityMode() );
persister.setIdentifier( entity, id, source );
}
else {
key = null;

View File

@ -163,7 +163,7 @@ public class DefaultDeleteEventListener implements DeleteEventListener {
deleteEntity( source, entity, entityEntry, event.isCascadeDeleteEnabled(), persister, transientEntities );
if ( source.getFactory().getSettings().isIdentifierRollbackEnabled() ) {
persister.resetIdentifier( entity, id, version, source.getEntityMode() );
persister.resetIdentifier( entity, id, version, source );
}
}

View File

@ -221,7 +221,7 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i
MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() )
);
}
persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession().getEntityMode() );
persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession() );
}
Object entity = doLoad(event, persister, keyToLoad, options);

View File

@ -292,10 +292,10 @@ public class DefaultMergeEventListener extends AbstractSaveEventListener
persister.getIdentifier( entity, source.getEntityMode() ) :
null;
if ( copyCache.containsKey( entity ) ) {
persister.setIdentifier( copyCache.get( entity ), id, source.getEntityMode() );
persister.setIdentifier( copyCache.get( entity ), id, source );
}
else {
( ( EventCache ) copyCache ).put( entity, persister.instantiate( id, source.getEntityMode() ), true ); //before cascade!
( ( EventCache ) copyCache ).put( entity, persister.instantiate( id, source ), true ); //before cascade!
//TODO: should this be Session.instantiate(Persister, ...)?
}
final Object copy = copyCache.get( entity );

View File

@ -239,7 +239,7 @@ public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener
event.setRequestedId(
getUpdateId(
entity, persister, event.getRequestedId(), event.getSession().getEntityMode()
entity, persister, event.getRequestedId(), event.getSession()
)
);
@ -253,7 +253,7 @@ public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener
* @param entity The entity.
* @param persister The entity persister
* @param requestedId The requested identifier
* @param entityMode The entity mode.
* @param session The session
*
* @return The id.
*
@ -263,9 +263,9 @@ public class DefaultSaveOrUpdateEventListener extends AbstractSaveEventListener
Object entity,
EntityPersister persister,
Serializable requestedId,
EntityMode entityMode) {
SessionImplementor session) {
// use the id assigned to the instance
Serializable id = persister.getIdentifier( entity, entityMode );
Serializable id = persister.getIdentifier( entity, session.getEntityMode() );
if ( id == null ) {
// assume this is a newly instantiated transient object
// which should be saved rather than updated

View File

@ -30,6 +30,7 @@ import org.hibernate.HibernateException;
import org.hibernate.ObjectDeletedException;
import org.hibernate.EntityMode;
import org.hibernate.engine.EntityEntry;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.Status;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.persister.entity.EntityPersister;
@ -62,14 +63,16 @@ public class DefaultUpdateEventListener extends DefaultSaveOrUpdateEventListener
* If the user specified an id, assign it to the instance and use that,
* otherwise use the id already assigned to the instance
*/
protected Serializable getUpdateId(Object entity, EntityPersister persister, Serializable requestedId, EntityMode entityMode)
throws HibernateException {
if ( requestedId==null ) {
return super.getUpdateId(entity, persister, requestedId, entityMode);
protected Serializable getUpdateId(
Object entity,
EntityPersister persister,
Serializable requestedId,
SessionImplementor session) throws HibernateException {
if ( requestedId == null ) {
return super.getUpdateId( entity, persister, requestedId, session );
}
else {
persister.setIdentifier(entity, requestedId, entityMode);
persister.setIdentifier( entity, requestedId, session );
return requestedId;
}
}

View File

@ -1409,7 +1409,7 @@ public final class SessionImpl extends AbstractSessionImpl
checkTransactionSynchStatus();
Object result = interceptor.instantiate( persister.getEntityName(), entityMode, id );
if ( result == null ) {
result = persister.instantiate( id, entityMode );
result = persister.instantiate( id, this );
}
return result;
}

View File

@ -114,7 +114,7 @@ public class StatelessSessionImpl extends AbstractSessionImpl
else {
persister.insert(id, state, entity, this);
}
persister.setIdentifier(entity, id, EntityMode.POJO);
persister.setIdentifier( entity, id, this );
return id;
}
@ -253,7 +253,7 @@ public class StatelessSessionImpl extends AbstractSessionImpl
Serializable id) throws HibernateException {
errorIfClosed();
return getFactory().getEntityPersister( entityName )
.instantiate( id, EntityMode.POJO );
.instantiate( id, this );
}
public Object internalLoad(

View File

@ -152,9 +152,22 @@ public interface ClassMetadata {
/**
* Create a class instance initialized with the given identifier
*
* @deprecated Use {@link #instantiate(Serializable, SessionImplementor)} instead
* @noinspection JavaDoc
*/
public Object instantiate(Serializable id, EntityMode entityMode) throws HibernateException;
/**
* Create a class instance initialized with the given identifier
*
* @param id The identifier value to use (may be null to represent no value)
* @param session The session from which the request originated.
*
* @return The instantiated entity.
*/
public Object instantiate(Serializable id, SessionImplementor session);
/**
* Get the value of a particular (named) property
*/
@ -186,9 +199,28 @@ public interface ClassMetadata {
public Serializable getIdentifier(Object entity, EntityMode entityMode) throws HibernateException;
/**
* Set the identifier of an instance (or do nothing if no identifier property)
* Inject the identifier value into the given entity.
* </p>
* Has no effect if the entity does not define an identifier property
*
* @param entity The entity to inject with the identifier value.
* @param id The value to be injected as the identifier.
* @param entityMode The entity mode
*
* @deprecated Use {@link #setIdentifier(Object, Serializable, SessionImplementor)} instead.
* @noinspection JavaDoc
*/
public void setIdentifier(Object object, Serializable id, EntityMode entityMode) throws HibernateException;
public void setIdentifier(Object entity, Serializable id, EntityMode entityMode) throws HibernateException;
/**
* Inject the identifier value into the given entity.
*
* @param entity The entity to inject with the identifier value.
* @param id The value to be injected as the identifier.
* @param session The session from which is requests originates
*/
public void setIdentifier(Object entity, Serializable id, SessionImplementor session);
/**
* Does the class implement the <tt>Lifecycle</tt> interface?

View File

@ -3801,9 +3801,19 @@ public abstract class AbstractEntityPersister
return getTuplizer( entityMode ).getIdentifier( object );
}
public void setIdentifier(Object object, Serializable id, EntityMode entityMode)
/**
* {@inheritDoc}
*/
public void setIdentifier(Object entity, Serializable id, EntityMode entityMode)
throws HibernateException {
getTuplizer( entityMode ).setIdentifier( object, id );
getTuplizer( entityMode ).setIdentifier( entity, id, null );
}
/**
* {@inheritDoc}
*/
public void setIdentifier(Object entity, Serializable id, SessionImplementor session) {
getTuplizer( session ).setIdentifier( entity, id, session );
}
public Object getVersion(Object object, EntityMode entityMode)
@ -3811,9 +3821,20 @@ public abstract class AbstractEntityPersister
return getTuplizer( entityMode ).getVersion( object );
}
/**
* {@inheritDoc}
*/
public Object instantiate(Serializable id, EntityMode entityMode)
throws HibernateException {
return getTuplizer( entityMode ).instantiate( id );
return getTuplizer( entityMode ).instantiate( id, null );
}
/**
* {@inheritDoc}
*/
public Object instantiate(Serializable id, SessionImplementor session)
throws HibernateException {
return getTuplizer( session ).instantiate( id, session );
}
public boolean isInstance(Object object, EntityMode entityMode) {
@ -3825,9 +3846,16 @@ public abstract class AbstractEntityPersister
}
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, EntityMode entityMode) {
getTuplizer( entityMode ).resetIdentifier( entity, currentId, currentVersion );
getTuplizer( entityMode ).resetIdentifier( entity, currentId, currentVersion, null );
}
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, SessionImplementor session) {
getTuplizer( session ).resetIdentifier( entity, currentId, currentVersion, session );
}
/**
* {@inheritDoc}
*/
public EntityPersister getSubclassEntityPersister(
Object instance,
SessionFactoryImplementor factory,

View File

@ -655,10 +655,28 @@ public interface EntityPersister extends OptimisticCacheSource {
*/
public Serializable getIdentifier(Object object, EntityMode entityMode) throws HibernateException;
/**
* Set the identifier of an instance (or do nothing if no identifier property)
*/
public void setIdentifier(Object object, Serializable id, EntityMode entityMode) throws HibernateException;
/**
* Inject the identifier value into the given entity.
* </p>
* Has no effect if the entity does not define an identifier property
*
* @param entity The entity to inject with the identifier value.
* @param id The value to be injected as the identifier.
* @param entityMode The entity mode
*
* @deprecated Use {@link #setIdentifier(Object, Serializable, SessionImplementor)} instead.
* @noinspection JavaDoc
*/
public void setIdentifier(Object entity, Serializable id, EntityMode entityMode) throws HibernateException;
/**
* Inject the identifier value into the given entity.
*
* @param entity The entity to inject with the identifier value.
* @param id The value to be injected as the identifier.
* @param session The session from which is requests originates
*/
public void setIdentifier(Object entity, Serializable id, SessionImplementor session);
/**
* Get the version number (or timestamp) from the object's version property (or return null if not versioned)
@ -667,9 +685,22 @@ public interface EntityPersister extends OptimisticCacheSource {
/**
* Create a class instance initialized with the given identifier
*
* @deprecated Use {@link #instantiate(Serializable, SessionImplementor)} instead
* @noinspection JavaDoc
*/
public Object instantiate(Serializable id, EntityMode entityMode) throws HibernateException;
/**
* Create a class instance initialized with the given identifier
*
* @param id The identifier value to use (may be null to represent no value)
* @param session The session from which the request originated.
*
* @return The instantiated entity.
*/
public Object instantiate(Serializable id, SessionImplementor session);
/**
* Is the given object an instance of this entity?
*/
@ -687,9 +718,21 @@ public interface EntityPersister extends OptimisticCacheSource {
* @param currentId The currently assigned identifier value.
* @param currentVersion The currently assigned version value.
* @param entityMode The entity mode represented by the entity instance.
*
* @deprecated Use {@link #resetIdentifier(Object, Serializable, Object, SessionImplementor)} instead
*/
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, EntityMode entityMode);
/**
* Set the identifier and version of the given instance back to its "unsaved" value.
*
* @param entity The entity instance
* @param currentId The currently assigned identifier value.
* @param currentVersion The currently assigned version value.
* @param session The session from which the request originated.
*/
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, SessionImplementor session);
/**
* A request has already identified the entity-name of this persister as the mapping for the given instance.
* However, we still need to account for possible subclassing and potentially re-route to the more appropriate

View File

@ -236,8 +236,19 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer {
}
}
/**
* {@inheritDoc}
*/
public void setIdentifier(Object entity, Serializable id) throws HibernateException {
// 99% of the time the session is not needed. Its only needed for certain brain-dead
// interpretations of JPA 2 "derived identity" support
setIdentifier( entity, id, null );
}
/**
* {@inheritDoc}
*/
public void setIdentifier(Object entity, Serializable id, SessionImplementor session) {
if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {
if ( entity != id ) {
AbstractComponentType copier = (AbstractComponentType) entityMetamodel.getIdentifierProperty().getType();
@ -265,16 +276,31 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer {
}
}
/**
* {@inheritDoc}
*/
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion) {
// 99% of the time the session is not needed. Its only needed for certain brain-dead
// interpretations of JPA 2 "derived identity" support
resetIdentifier( entity, currentId, currentVersion, null );
}
/**
* {@inheritDoc}
*/
public void resetIdentifier(
Object entity,
Serializable currentId,
Object currentVersion,
SessionImplementor session) {
if ( entityMetamodel.getIdentifierProperty().getIdentifierGenerator() instanceof Assigned ) {
//return currentId;
}
else {
//reset the id
Serializable result = entityMetamodel.getIdentifierProperty()
.getUnsavedValue()
.getDefaultValue( currentId );
setIdentifier( entity, result );
setIdentifier( entity, result, session );
//reset the version
VersionProperty versionProperty = entityMetamodel.getVersionProperty();
if ( entityMetamodel.isVersioned() ) {
@ -282,10 +308,8 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer {
entity,
entityMetamodel.getVersionPropertyIndex(),
versionProperty.getUnsavedValue().getDefaultValue( currentVersion )
);
);
}
//return the id, so we can use it to reset the proxy id
//return result;
}
}
@ -421,15 +445,21 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer {
}
public final Object instantiate(Serializable id) throws HibernateException {
// 99% of the time the session is not needed. Its only needed for certain brain-dead
// interpretations of JPA 2 "derived identity" support
return instantiate( id, null );
}
public final Object instantiate(Serializable id, SessionImplementor session) {
Object result = getInstantiator().instantiate( id );
if ( id != null ) {
setIdentifier( result, id );
setIdentifier( result, id, session );
}
return result;
}
public final Object instantiate() throws HibernateException {
return instantiate( null );
return instantiate( null, null );
}
public void afterInitialize(Object entity, boolean lazyPropertiesAreUnfetched, SessionImplementor session) {}

View File

@ -59,9 +59,22 @@ public interface EntityTuplizer extends Tuplizer {
* @param id The identifier value for the entity to be instantiated.
* @return The instantiated entity.
* @throws HibernateException
*
* @deprecated Use {@link #instantiate(Serializable, SessionImplementor)} instead.
* @noinspection JavaDoc
*/
public Object instantiate(Serializable id) throws HibernateException;
/**
* Create an entity instance initialized with the given identifier.
*
* @param id The identifier value for the entity to be instantiated.
* @param session The session from which is requests originates
*
* @return The instantiated entity.
*/
public Object instantiate(Serializable id, SessionImplementor session);
/**
* Extract the identifier value from the given entity.
*
@ -79,19 +92,46 @@ public interface EntityTuplizer extends Tuplizer {
*
* @param entity The entity to inject with the identifier value.
* @param id The value to be injected as the identifier.
* @throws HibernateException
*
* @deprecated Use {@link #setIdentifier(Object, Serializable, SessionImplementor)} instead.
* @noinspection JavaDoc
*/
public void setIdentifier(Object entity, Serializable id) throws HibernateException;
/**
* Inject the identifier value into the given entity.
* </p>
* Has no effect if the entity does not define an identifier property
*
* @param entity The entity to inject with the identifier value.
* @param id The value to be injected as the identifier.
* @param session The session from which is requests originates
*/
public void setIdentifier(Object entity, Serializable id, SessionImplementor session);
/**
* Inject the given identifier and version into the entity, in order to
* "roll back" to their original values.
*
* @param entity The entity for which to reset the id/version values
* @param currentId The identifier value to inject into the entity.
* @param currentVersion The version value to inject into the entity.
*
* @deprecated Use {@link #resetIdentifier(Object, Serializable, Object, SessionImplementor)} instead
*/
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion);
/**
* Inject the given identifier and version into the entity, in order to
* "roll back" to their original values.
*
* @param entity The entity for which to reset the id/version values
* @param currentId The identifier value to inject into the entity.
* @param currentVersion The version value to inject into the entity.
* @param session The session from which the request originated
*/
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, SessionImplementor session);
/**
* Extract the value of the version property from the given entity.
*

View File

@ -276,7 +276,7 @@ public abstract class EntityType extends AbstractType implements AssociationType
if ( session.getContextEntityIdentifier( original ) == null &&
ForeignKeys.isTransient( associatedEntityName, original, Boolean.FALSE, session ) ) {
final Object copy = session.getFactory().getEntityPersister( associatedEntityName )
.instantiate( null, session.getEntityMode() );
.instantiate( null, session );
//TODO: should this be Session.instantiate(Persister, ...)?
copyCache.put( original, copy );
return copy;

View File

@ -62,6 +62,10 @@ public class CustomPersister implements EntityPersister {
}
}
private void checkEntityMode(SessionImplementor session) {
checkEntityMode( session.getEntityMode() );
}
public boolean isInherited() {
return false;
}
@ -183,6 +187,11 @@ public class CustomPersister implements EntityPersister {
( (Custom) object ).id = (String) id;
}
public void setIdentifier(Object entity, Serializable id, SessionImplementor session) {
checkEntityMode( session );
( (Custom) entity ).id = (String) id;
}
public Object getVersion(Object object, EntityMode entityMode) throws HibernateException {
checkEntityMode( entityMode );
return null;
@ -190,11 +199,20 @@ public class CustomPersister implements EntityPersister {
public Object instantiate(Serializable id, EntityMode entityMode) throws HibernateException {
checkEntityMode( entityMode );
return instantiate( id );
}
private Object instantiate(Serializable id) {
Custom c = new Custom();
c.id = (String) id;
return c;
}
public Object instantiate(Serializable id, SessionImplementor session) {
checkEntityMode( session );
return instantiate( id );
}
public boolean isInstance(Object object, EntityMode entityMode) {
checkEntityMode( entityMode );
return object instanceof Custom;
@ -210,6 +228,11 @@ public class CustomPersister implements EntityPersister {
( ( Custom ) entity ).id = ( String ) currentId;
}
public void resetIdentifier(Object entity, Serializable currentId, Object currentVersion, SessionImplementor session) {
checkEntityMode( session );
( ( Custom ) entity ).id = ( String ) currentId;
}
public EntityPersister getSubclassEntityPersister(Object instance, SessionFactoryImplementor factory, EntityMode entityMode) {
checkEntityMode( entityMode );
return this;