Java 类org.hibernate.event.spi.SaveOrUpdateEvent 实例源码

项目:lams    文件:DefaultUpdateEventListener.java   
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) {
    // this implementation is supposed to tolerate incorrect unsaved-value
    // mappings, for the purpose of backward-compatibility
    EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() );
    if ( entry!=null ) {
        if ( entry.getStatus()== Status.DELETED ) {
            throw new ObjectDeletedException( "deleted instance passed to update()", null, event.getEntityName() );
        }
        else {
            return entityIsPersistent(event);
        }
    }
    else {
        entityIsDetached(event);
        return null;
    }
}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) {
    EntityState entityState = getEntityState(
            event.getEntity(),
            event.getEntityName(),
            event.getEntry(),
            event.getSession()
    );

    switch ( entityState ) {
        case DETACHED:
            entityIsDetached( event );
            return null;
        case PERSISTENT:
            return entityIsPersistent( event );
        default: //TRANSIENT or DELETED
            return entityIsTransient( event );
    }
}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
/**
 * The given save-update event named a transient entity.
 * <p/>
 * Here, we will perform the save processing.
 *
 * @param event The save event to be handled.
 *
 * @return The entity's identifier after saving.
 */
protected Serializable entityIsTransient(SaveOrUpdateEvent event) {

    LOG.trace( "Saving transient instance" );

    final EventSource source = event.getSession();

    EntityEntry entityEntry = event.getEntry();
    if ( entityEntry != null ) {
        if ( entityEntry.getStatus() == Status.DELETED ) {
            source.forceFlush( entityEntry );
        }
        else {
            throw new AssertionFailure( "entity was persistent" );
        }
    }

    Serializable id = saveWithGeneratedOrRequestedId( event );

    source.getPersistenceContext().reassociateProxy( event.getObject(), id );

    return id;
}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
/**
 * The given save-update event named a detached entity.
 * <p/>
 * Here, we will perform the update processing.
 *
 * @param event The update event to be handled.
 */
protected void entityIsDetached(SaveOrUpdateEvent event) {

    LOG.trace( "Updating detached instance" );

    if ( event.getSession().getPersistenceContext().isEntryFor( event.getEntity() ) ) {
        //TODO: assertion only, could be optimized away
        throw new AssertionFailure( "entity was persistent" );
    }

    Object entity = event.getEntity();

    EntityPersister persister = event.getSession().getEntityPersister( event.getEntityName(), entity );

    event.setRequestedId(
            getUpdateId(
                    entity, persister, event.getRequestedId(), event.getSession()
            )
    );

    performUpdate( event, entity, persister );

}
项目:openeos    文件:DAOListenerManager.java   
@SuppressWarnings("unchecked")
private void fireSaveOrUpdateEvent(SaveOrUpdateEvent event, boolean before) {
    org.openeos.dao.SaveOrUpdateEvent<Object> ourEvent = new org.openeos.dao.SaveOrUpdateEvent<Object>(
            event.getSession(), event.getObject(), event.getResultId());
    try {
        for (SaveOrUpdateListener<Object> listener : getListenerByObject(SaveOrUpdateListener.class, event.getObject())) {
            LOG.debug(MessageFormat.format("Calling SaveOrUpdateListener. Object: {0} Before: {1} Listener: {2}", event
                    .getObject().toString(), Boolean.toString(before), listener.toString()));
            if (before) {
                listener.beforeSaveOrUpdate(ourEvent);
            } else {
                listener.afterSaveOrUpdate(ourEvent);
            }
        }
    } catch (UserException ex) {
        // TODO
        throw new HibernateException(ex);
    }

}
项目:lams    文件:SessionImpl.java   
private void fireSaveOrUpdate(SaveOrUpdateEvent event) {
    errorIfClosed();
    checkTransactionSynchStatus();
    checkNoUnresolvedActionsBeforeOperation();
    for ( SaveOrUpdateEventListener listener : listeners( EventType.SAVE_UPDATE ) ) {
        listener.onSaveOrUpdate( event );
    }
    checkNoUnresolvedActionsAfterOperation();
}
项目:lams    文件:SessionImpl.java   
private Serializable fireSave(SaveOrUpdateEvent event) {
    errorIfClosed();
    checkTransactionSynchStatus();
    checkNoUnresolvedActionsBeforeOperation();
    for ( SaveOrUpdateEventListener listener : listeners( EventType.SAVE ) ) {
        listener.onSaveOrUpdate( event );
    }
    checkNoUnresolvedActionsAfterOperation();
    return event.getResultId();
}
项目:lams    文件:SessionImpl.java   
private void fireUpdate(SaveOrUpdateEvent event) {
    errorIfClosed();
    checkTransactionSynchStatus();
    checkNoUnresolvedActionsBeforeOperation();
    for ( SaveOrUpdateEventListener listener : listeners( EventType.UPDATE ) ) {
        listener.onSaveOrUpdate( event );
    }
    checkNoUnresolvedActionsAfterOperation();
}
项目:lams    文件:DefaultSaveEventListener.java   
protected Serializable performSaveOrUpdate(SaveOrUpdateEvent event) {
    // this implementation is supposed to tolerate incorrect unsaved-value
    // mappings, for the purpose of backward-compatibility
    EntityEntry entry = event.getSession().getPersistenceContext().getEntry( event.getEntity() );
    if ( entry!=null && entry.getStatus() != Status.DELETED ) {
        return entityIsPersistent(event);
    }
    else {
        return entityIsTransient(event);
    }
}
项目:lams    文件:DefaultSaveEventListener.java   
protected Serializable saveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) {
    if ( event.getRequestedId() == null ) {
        return super.saveWithGeneratedOrRequestedId(event);
    }
    else {
        return saveWithRequestedId( 
                event.getEntity(), 
                event.getRequestedId(), 
                event.getEntityName(), 
                null, 
                event.getSession() 
            );
    }

}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
/**
 * Handle the given update event.
 *
 * @param event The update event to be handled.
 */
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
    final SessionImplementor source = event.getSession();
    final Object object = event.getObject();
    final Serializable requestedId = event.getRequestedId();

    if ( requestedId != null ) {
        //assign the requested id to the proxy, *before*
        //reassociating the proxy
        if ( object instanceof HibernateProxy ) {
            ( (HibernateProxy) object ).getHibernateLazyInitializer().setIdentifier( requestedId );
        }
    }

    // For an uninitialized proxy, noop, don't even need to return an id, since it is never a save()
    if ( reassociateIfUninitializedProxy( object, source ) ) {
        LOG.trace( "Reassociated uninitialized proxy" );
    }
    else {
        //initialize properties of the event:
        final Object entity = source.getPersistenceContext().unproxyAndReassociate( object );
        event.setEntity( entity );
        event.setEntry( source.getPersistenceContext().getEntry( entity ) );
        //return the id in the event object
        event.setResultId( performSaveOrUpdate( event ) );
    }

}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
/**
 * Save the transient instance, assigning the right identifier
 *
 * @param event The initiating event.
 *
 * @return The entity's identifier value after saving.
 */
protected Serializable saveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) {
    return saveWithGeneratedId(
            event.getEntity(),
            event.getEntityName(),
            null,
            event.getSession(),
            true
    );
}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
/**
 * Handles the calls needed to perform cascades as part of an update request
 * for the given entity.
 *
 * @param event The event currently being processed.
 * @param persister The defined persister for the entity being updated.
 * @param entity The entity being updated.
 */
private void cascadeOnUpdate(SaveOrUpdateEvent event, EntityPersister persister, Object entity) {
    final EventSource source = event.getSession();
    source.getPersistenceContext().incrementCascadeLevel();
    try {
        new Cascade( CascadingActions.SAVE_UPDATE, CascadePoint.AFTER_UPDATE, source ).cascade( persister, entity );
    }
    finally {
        source.getPersistenceContext().decrementCascadeLevel();
    }
}
项目:lams    文件:SessionImpl.java   
@Override
public void saveOrUpdate(String entityName, Object obj) throws HibernateException {
    fireSaveOrUpdate( new SaveOrUpdateEvent( entityName, obj, this ) );
}
项目:lams    文件:SessionImpl.java   
@Override
public Serializable save(String entityName, Object object) throws HibernateException {
    return fireSave( new SaveOrUpdateEvent( entityName, object, this ) );
}
项目:lams    文件:SessionImpl.java   
@Override
public void update(String entityName, Object object) throws HibernateException {
    fireUpdate( new SaveOrUpdateEvent( entityName, object, this ) );
}
项目: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;

    }
}
项目:lams    文件:DefaultSaveOrUpdateEventListener.java   
protected void performUpdate(
        SaveOrUpdateEvent event,
        Object entity,
        EntityPersister persister) throws HibernateException {

    final boolean traceEnabled = LOG.isTraceEnabled();
    if ( traceEnabled && !persister.isMutable() ) {
        LOG.trace( "Immutable instance passed to performUpdate()" );
    }

    if ( traceEnabled ) {
        LOG.tracev(
                "Updating {0}",
                MessageHelper.infoString( persister, event.getRequestedId(), event.getSession().getFactory() )
        );
    }

    final EventSource source = event.getSession();
    final EntityKey key = source.generateEntityKey( event.getRequestedId(), persister );

    source.getPersistenceContext().checkUniqueness( key, entity );

    if ( invokeUpdateLifecycle( entity, persister, source ) ) {
        reassociate( event, event.getObject(), event.getRequestedId(), persister );
        return;
    }

    // this is a transient object with existing persistent state not loaded by the session

    new OnUpdateVisitor( source, event.getRequestedId(), entity ).process( entity, persister );

    // TODO: put this stuff back in to read snapshot from
    // the second-level cache (needs some extra work)
    /*Object[] cachedState = null;

       if ( persister.hasCache() ) {
        CacheEntry entry = (CacheEntry) persister.getCache()
                .get( event.getRequestedId(), source.getTimestamp() );
           cachedState = entry==null ?
                null :
                entry.getState(); //TODO: half-assemble this stuff
       }*/

    source.getPersistenceContext().addEntity(
            entity,
            ( persister.isMutable() ? Status.MANAGED : Status.READ_ONLY ),
            null, // cachedState,
            key,
            persister.getVersion( entity ),
            LockMode.NONE,
            true,
            persister,
            false,
            true // assume true, since we don't really know, and it doesn't matter
    );

    persister.afterReassociate( entity, source );

    if ( traceEnabled ) {
        LOG.tracev(
                "Updating {0}", MessageHelper.infoString(
                persister,
                event.getRequestedId(),
                source.getFactory()
        )
        );
    }

    cascadeOnUpdate( event, persister, entity );
}
项目:CodesOfLightweightJavaEE    文件:MySaveListener.java   
public Serializable performSaveOrUpdate(SaveOrUpdateEvent event)
{
    System.out.println("�Զ����save�¼�");
    System.out.println(event.getObject());
    return super.performSaveOrUpdate(event);
}
项目:openeos    文件:DAOListenerManager.java   
@Override
public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
    fireSaveOrUpdateEvent(event, true);
}
项目:openeos    文件:DAOListenerManager.java   
@Override
public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
    fireSaveOrUpdateEvent(event, false);
}