Java 类org.hibernate.PersistentObjectException 实例源码

项目:lams    文件:StatefulPersistenceContext.java   
@Override
public Object unproxy(Object maybeProxy) throws HibernateException {
    if ( maybeProxy instanceof ElementWrapper ) {
        maybeProxy = ( (ElementWrapper) maybeProxy ).getElement();
    }

    if ( maybeProxy instanceof HibernateProxy ) {
        final HibernateProxy proxy = (HibernateProxy) maybeProxy;
        final LazyInitializer li = proxy.getHibernateLazyInitializer();
        if ( li.isUninitialized() ) {
            throw new PersistentObjectException(
                    "object was an uninitialized proxy for " + li.getEntityName()
            );
        }
        //unwrap the object and return
        return li.getImplementation();
    }
    else {
        return maybeProxy;
    }
}
项目:cacheonix-core    文件:StatefulPersistenceContext.java   
/**
 * Get the entity instance underlying the given proxy, throwing
 * an exception if the proxy is uninitialized. If the given object
 * is not a proxy, simply return the argument.
 */
public Object unproxy(Object maybeProxy) throws HibernateException {
    if ( maybeProxy instanceof ElementWrapper ) {
        maybeProxy = ( (ElementWrapper) maybeProxy ).getElement();
    }

    if ( maybeProxy instanceof HibernateProxy ) {
        HibernateProxy proxy = (HibernateProxy) maybeProxy;
        LazyInitializer li = proxy.getHibernateLazyInitializer();
        if ( li.isUninitialized() ) {
            throw new PersistentObjectException(
                    "object was an uninitialized proxy for " +
                    li.getEntityName()
            );
        }
        return li.getImplementation(); //unwrap the object
    }
    else {
        return maybeProxy;
    }
}
项目:lams    文件:DefaultSaveEventListener.java   
protected boolean reassociateIfUninitializedProxy(Object object, SessionImplementor source) {
    if ( !Hibernate.isInitialized(object) ) {
        throw new PersistentObjectException("uninitialized proxy passed to save()");
    }
    else {
        return false;
    }
}
项目:lams    文件:DefaultLoadEventListener.java   
/**
 * 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;
}
项目:cacheonix-core    文件:DefaultSaveEventListener.java   
protected boolean reassociateIfUninitializedProxy(Object object, SessionImplementor source) {
    if ( !Hibernate.isInitialized(object) ) {
        throw new PersistentObjectException("uninitialized proxy passed to save()");
    }
    else {
        return false;
    }
}
项目:cacheonix-core    文件:DefaultSaveOrUpdateEventListener.java   
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException {
    log.trace( "ignoring persistent instance" );

    EntityEntry entityEntry = event.getEntry();
    if ( entityEntry == null ) {
        throw new AssertionFailure( "entity was transient or detached" );
    }
    else {

        if ( entityEntry.getStatus() == Status.DELETED ) {
            throw new AssertionFailure( "entity was deleted" );
        }

        final SessionFactoryImplementor factory = event.getSession().getFactory();

        Serializable requestedId = event.getRequestedId();

        Serializable savedId;
        if ( requestedId == null ) {
            savedId = entityEntry.getId();
        }
        else {

            final boolean isEqual = !entityEntry.getPersister().getIdentifierType()
                    .isEqual( requestedId, entityEntry.getId(), event.getSession().getEntityMode(), factory );

            if ( isEqual ) {
                throw new PersistentObjectException(
                        "object passed to save() was already persistent: " +
                                MessageHelper.infoString( entityEntry.getPersister(), requestedId, factory )
                );
            }

            savedId = requestedId;

        }

        if ( log.isTraceEnabled() ) {
            log.trace(
                    "object already associated with session: " +
                            MessageHelper.infoString( entityEntry.getPersister(), savedId, factory )
            );
        }

        return savedId;

    }
}
项目:cacheonix-core    文件:DefaultLoadEventListener.java   
/**
 * Perfoms the load of an entity.
 *
 * @return The loaded entity.
 * @throws HibernateException
 */
protected Object load(
    final LoadEvent event, 
    final EntityPersister persister, 
    final EntityKey keyToLoad, 
    final LoadEventListener.LoadType options)
throws HibernateException {

    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().getEntityMode() );
    }

    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;
}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException {
    final boolean traceEnabled = LOG.isTraceEnabled();
    if ( traceEnabled ) {
        LOG.trace( "Ignoring persistent instance" );
    }
    EntityEntry entityEntry = event.getEntry();
    if ( entityEntry == null ) {
        throw new AssertionFailure( "entity was transient or detached" );
    }
    else {

        if ( entityEntry.getStatus() == Status.DELETED ) {
            throw new AssertionFailure( "entity was deleted" );
        }

        final SessionFactoryImplementor factory = event.getSession().getFactory();

        Serializable requestedId = event.getRequestedId();

        Serializable savedId;
        if ( requestedId == null ) {
            savedId = entityEntry.getId();
        }
        else {

            final boolean isEqual = !entityEntry.getPersister().getIdentifierType()
                    .isEqual( requestedId, entityEntry.getId(), factory );

            if ( isEqual ) {
                throw new PersistentObjectException(
                        "object passed to save() was already persistent: " +
                                MessageHelper.infoString( entityEntry.getPersister(), requestedId, factory )
                );
            }

            savedId = requestedId;

        }

        if ( traceEnabled ) {
            LOG.tracev(
                    "Object already associated with session: {0}",
                    MessageHelper.infoString( entityEntry.getPersister(), savedId, factory )
            );
        }

        return savedId;

    }
}
项目:cacheonix-core    文件:DefaultPersistEventListener.java   
/** 
 * Handle the given create event.
 *
 * @param event The create event to be handled.
 * @throws HibernateException
 */
public void onPersist(PersistEvent event, Map createCache) throws HibernateException {

    final SessionImplementor source = event.getSession();
    final Object object = event.getObject();

    final Object entity;
    if (object instanceof HibernateProxy) {
        LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
        if ( li.isUninitialized() ) {
            if ( li.getSession()==source ) {
                return; //NOTE EARLY EXIT!
            }
            else {
                throw new PersistentObjectException("uninitialized proxy passed to persist()");
            }
        }
        entity = li.getImplementation();
    }
    else {
        entity = object;
    }

    int entityState = getEntityState( 
            entity, 
            event.getEntityName(), 
            source.getPersistenceContext().getEntry(entity), 
            source 
        );

    switch (entityState) {
        case DETACHED: 
            throw new PersistentObjectException( 
                    "detached entity passed to persist: " + 
                    getLoggableName( event.getEntityName(), entity ) 
                );
        case PERSISTENT:
            entityIsPersistent(event, createCache);
            break;
        case TRANSIENT:
            entityIsTransient(event, createCache);
            break;
        default: 
            throw new ObjectDeletedException( 
                    "deleted entity passed to persist", 
                    null, 
                    getLoggableName( event.getEntityName(), entity )
                );
    }

}