@Override public final Object getImplementation(SharedSessionContractImplementor s) throws HibernateException { final EntityKey entityKey = generateEntityKeyOrNull( getIdentifier(), s, getEntityName() ); return (entityKey == null ? null : s.getPersistenceContext().getEntity( entityKey )); }
@Override public final Object getImplementation() { initialize(); return target; }
@Override public final void initialize() throws HibernateException { if ( !initialized ) { if ( allowLoadOutsideTransaction ) { permissiveInitialization(); } else if ( session == null ) { throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - no Session" ); } else if ( !session.isOpenOrWaitingForAutoClose() ) { throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - the owning Session was closed" ); } else if ( !session.isConnected() ) { throw new LazyInitializationException( "could not initialize proxy [" + entityName + "#" + id + "] - the owning Session is disconnected" ); } else { target = session.immediateLoad( entityName, id ); initialized = true; checkTargetState(session); } } else { checkTargetState(session); } }
@Override public final Serializable getIdentifier() { if ( isUninitialized() && isInitializeProxyWhenAccessingIdentifier() ) { initialize(); } return id; }
@Override public final void setReadOnly(boolean readOnly) { errorIfReadOnlySettingNotAvailable(); // only update if readOnly is different from current setting if ( this.readOnly != readOnly ) { final EntityPersister persister = session.getFactory().getEntityPersister( entityName ); if ( !persister.isMutable() && !readOnly ) { throw new IllegalStateException( "cannot make proxies [" + entityName + "#" + id + "] for immutable entities modifiable" ); } this.readOnly = readOnly; if ( initialized ) { EntityKey key = generateEntityKeyOrNull( getIdentifier(), session, getEntityName() ); if ( key != null && session.getPersistenceContext().containsEntity( key ) ) { session.getPersistenceContext().setReadOnly( target, readOnly ); } } } }
unsetSession(); else if ( isConnectedToSession() ) { setReadOnly( s.getPersistenceContext().isDefaultReadOnly() || !persister.isMutable() ); setReadOnly( readOnlyBeforeAttachedToSession ); readOnlyBeforeAttachedToSession = null;
/** * Attempt to initialize the proxy without loading anything from the database. * * This will only have any effect if the proxy is still attached to a session, * and the entity being proxied has been loaded and added to the persistence context * of that session since the proxy was created. */ public final void initializeWithoutLoadIfPossible() { if ( !initialized && session != null && session.isOpenOrWaitingForAutoClose() ) { final EntityKey key = session.generateEntityKey( getIdentifier(), session.getFactory().getMetamodel().entityPersister( getEntityName() ) ); final Object entity = session.getPersistenceContext().getEntity( key ); if ( entity != null ) { setImplementation( entity ); } } }
/** * Return the underlying persistent object in the given <tt>Session</tt>, or null, * do not initialize the proxy */ public final Object getImplementation(SessionImplementor s) throws HibernateException { return s.getPersistenceContext().getEntity( new EntityKey( getIdentifier(), s.getFactory().getEntityPersister( getEntityName() ), s.getEntityMode() ) ); }
/** * Get the read-only/modifiable setting that should be put in affect when it is * attached to a session. * <p/> * This method should only be called during serialization when read-only/modifiable setting * is not available (i.e., isReadOnlySettingAvailable() == false) * * @return null, if the default setting should be used; * true, for read-only; * false, for modifiable * * @throws IllegalStateException if isReadOnlySettingAvailable() == true */ public final Boolean isReadOnlyBeforeAttachedToSession() { if ( isReadOnlySettingAvailable() ) { throw new IllegalStateException( "Cannot call isReadOnlyBeforeAttachedToSession when isReadOnlySettingAvailable == true [" + entityName + "#" + id + "]" ); } return readOnlyBeforeAttachedToSession; }
target = session.immediateLoad( entityName, id ); initialized = true; checkTargetState(session); target = session.immediateLoad( entityName, id ); initialized = true; checkTargetState(session);
/** * Getter for property 'connectedToSession'. * * @return Value for property 'connectedToSession'. */ protected final boolean isConnectedToSession() { return getProxyOrNull() != null; }
@Override public final boolean isReadOnly() { errorIfReadOnlySettingNotAvailable(); return readOnly; }
public final void setSession(SessionImplementor s) throws HibernateException { if (s!=session) { if ( isConnectedToSession() ) { //TODO: perhaps this should be some other RuntimeException... throw new HibernateException("illegally attempted to associate a proxy with two open Sessions"); } else { session = s; } } }
@Override public final void setReadOnly(boolean readOnly) { errorIfReadOnlySettingNotAvailable(); // only update if readOnly is different from current setting if ( this.readOnly != readOnly ) { final EntityPersister persister = session.getFactory().getEntityPersister( entityName ); if ( ! persister.isMutable() && ! readOnly ) { throw new IllegalStateException( "cannot make proxies for immutable entities modifiable"); } this.readOnly = readOnly; if ( initialized ) { EntityKey key = generateEntityKeyOrNull( getIdentifier(), session, getEntityName() ); if ( key != null && session.getPersistenceContext().containsEntity( key ) ) { session.getPersistenceContext().setReadOnly( target, readOnly ); } } } }
@Override public final void setSession(SessionImplementor s) throws HibernateException { if ( s != session ) { // check for s == null first, since it is least expensive if ( s == null ){ unsetSession(); } else if ( isConnectedToSession() ) { //TODO: perhaps this should be some other RuntimeException... throw new HibernateException("illegally attempted to associate a proxy with two open Sessions"); } else { // s != null session = s; if ( readOnlyBeforeAttachedToSession == null ) { // use the default read-only/modifiable setting final EntityPersister persister = s.getFactory().getEntityPersister( entityName ); setReadOnly( s.getPersistenceContext().isDefaultReadOnly() || ! persister.isMutable() ); } else { // use the read-only/modifiable setting indicated during deserialization setReadOnly( readOnlyBeforeAttachedToSession.booleanValue() ); readOnlyBeforeAttachedToSession = null; } } } }
/** * Attempt to initialize the proxy without loading anything from the database. * * This will only have any effect if the proxy is still attached to a session, * and the entity being proxied has been loaded and added to the persistence context * of that session since the proxy was created. */ public final void initializeWithoutLoadIfPossible() { if ( !initialized && session != null && session.isOpen() ) { final EntityKey key = session.generateEntityKey( getIdentifier(), session.getFactory().getMetamodel().entity( getEntityName() ) ); final Object entity = session.getPersistenceContext().getEntity( key ); if ( entity != null ) { setImplementation( entity ); } } }
/** * Return the underlying persistent object in the given <tt>Session</tt>, or null, * do not initialize the proxy */ public final Object getImplementation(SessionImplementor s) throws HibernateException { final EntityKey entityKey = new EntityKey( getIdentifier(), s.getFactory().getEntityPersister( getEntityName() ), s.getEntityMode() ); return s.getPersistenceContext().getEntity( entityKey ); }
@Override public final Object getIdentifier() { if ( isUninitialized() && isInitializeProxyWhenAccessingIdentifier() ) { initialize(); } return id; }
/** * Restore settings that are not passed to the constructor, * but are still preserved during serialization. * * This method should only be called during deserialization, before associating * the proxy with a session. * * @param readOnlyBeforeAttachedToSession, the read-only/modifiable setting to use when * associated with a session; null indicates that the default should be used. * @param sessionFactoryUuid the session factory uuid, to be used if {@code allowLoadOutsideTransaction} is {@code true}. * @param allowLoadOutsideTransaction whether the proxy can load data even * if it's not attached to a session with an ongoing transaction. * * @throws IllegalStateException if isReadOnlySettingAvailable() == true */ /* package-private */ final void afterDeserialization(Boolean readOnlyBeforeAttachedToSession, String sessionFactoryUuid, boolean allowLoadOutsideTransaction) { if ( isReadOnlySettingAvailable() ) { throw new IllegalStateException( "Cannot call afterDeserialization when isReadOnlySettingAvailable == true [" + entityName + "#" + id + "]" ); } this.readOnlyBeforeAttachedToSession = readOnlyBeforeAttachedToSession; this.sessionFactoryUuid = sessionFactoryUuid; this.allowLoadOutsideTransaction = allowLoadOutsideTransaction; }
public final void initialize() throws HibernateException { if (!initialized) { if ( session==null ) { throw new LazyInitializationException("could not initialize proxy - no Session"); } else if ( !session.isOpen() ) { throw new LazyInitializationException("could not initialize proxy - the owning Session was closed"); } else if ( !session.isConnected() ) { throw new LazyInitializationException("could not initialize proxy - the owning Session is disconnected"); } else { target = session.immediateLoad(entityName, id); initialized = true; checkTargetState(); } } else { checkTargetState(); } }