Search in sources :

Example 1 with Nullability

use of org.hibernate.engine.internal.Nullability in project hibernate-orm by hibernate.

the class DefaultFlushEntityEventListener method scheduleUpdate.

private boolean scheduleUpdate(final FlushEntityEvent event) {
    final EntityEntry entry = event.getEntityEntry();
    final EventSource session = event.getSession();
    final Object entity = event.getEntity();
    final Status status = entry.getStatus();
    final EntityPersister persister = entry.getPersister();
    final Object[] values = event.getPropertyValues();
    if (LOG.isTraceEnabled()) {
        if (status == Status.DELETED) {
            if (!persister.isMutable()) {
                LOG.tracev("Updating immutable, deleted entity: {0}", MessageHelper.infoString(persister, entry.getId(), session.getFactory()));
            } else if (!entry.isModifiableEntity()) {
                LOG.tracev("Updating non-modifiable, deleted entity: {0}", MessageHelper.infoString(persister, entry.getId(), session.getFactory()));
            } else {
                LOG.tracev("Updating deleted entity: ", MessageHelper.infoString(persister, entry.getId(), session.getFactory()));
            }
        } else {
            LOG.tracev("Updating entity: {0}", MessageHelper.infoString(persister, entry.getId(), session.getFactory()));
        }
    }
    final boolean intercepted = !entry.isBeingReplicated() && handleInterception(event);
    // increment the version number (if necessary)
    final Object nextVersion = getNextVersion(event);
    // if it was dirtied by a collection only
    int[] dirtyProperties = event.getDirtyProperties();
    if (event.isDirtyCheckPossible() && dirtyProperties == null) {
        if (!intercepted && !event.hasDirtyCollection()) {
            throw new AssertionFailure("dirty, but no dirty properties");
        }
        dirtyProperties = ArrayHelper.EMPTY_INT_ARRAY;
    }
    // check nullability but do not doAfterTransactionCompletion command execute
    // we'll use scheduled updates for that.
    new Nullability(session).checkNullability(values, persister, true);
    // schedule the update
    // note that we intentionally do _not_ pass in currentPersistentState!
    session.getActionQueue().addAction(new EntityUpdateAction(entry.getId(), values, dirtyProperties, event.hasDirtyCollection(), (status == Status.DELETED && !entry.isModifiableEntity() ? persister.getPropertyValues(entity) : entry.getLoadedState()), entry.getVersion(), nextVersion, entity, entry.getRowId(), persister, session));
    return intercepted;
}
Also used : Status(org.hibernate.engine.spi.Status) EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEntry(org.hibernate.engine.spi.EntityEntry) EventSource(org.hibernate.event.spi.EventSource) AssertionFailure(org.hibernate.AssertionFailure) EntityUpdateAction(org.hibernate.action.internal.EntityUpdateAction) Nullability(org.hibernate.engine.internal.Nullability)

Example 2 with Nullability

use of org.hibernate.engine.internal.Nullability in project hibernate-orm by hibernate.

the class DefaultDeleteEventListener method deleteEntity.

/**
 * Perform the entity deletion.  Well, as with most operations, does not
 * really perform it; just schedules an action/execution with the
 * {@link org.hibernate.engine.spi.ActionQueue} for execution during flush.
 *
 * @param session The originating session
 * @param entity The entity to delete
 * @param entityEntry The entity's entry in the {@link PersistenceContext}
 * @param isCascadeDeleteEnabled Is delete cascading enabled?
 * @param persister The entity persister.
 * @param transientEntities A cache of already deleted entities.
 */
protected final void deleteEntity(final EventSource session, final Object entity, final EntityEntry entityEntry, final boolean isCascadeDeleteEnabled, final boolean isOrphanRemovalBeforeUpdates, final EntityPersister persister, final Set transientEntities) {
    if (LOG.isTraceEnabled()) {
        LOG.tracev("Deleting {0}", MessageHelper.infoString(persister, entityEntry.getId(), session.getFactory()));
    }
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final Type[] propTypes = persister.getPropertyTypes();
    final Object version = entityEntry.getVersion();
    final Object[] currentState;
    if (entityEntry.getLoadedState() == null) {
        // ie. the entity came in from update()
        currentState = persister.getPropertyValues(entity);
    } else {
        currentState = entityEntry.getLoadedState();
    }
    final Object[] deletedState = createDeletedState(persister, currentState, session);
    entityEntry.setDeletedState(deletedState);
    session.getInterceptor().onDelete(entity, entityEntry.getId(), deletedState, persister.getPropertyNames(), propTypes);
    // before any callbacks, etc, so subdeletions see that this deletion happened first
    persistenceContext.setEntryStatus(entityEntry, Status.DELETED);
    final EntityKey key = session.generateEntityKey(entityEntry.getId(), persister);
    cascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities);
    new ForeignKeys.Nullifier(entity, true, false, session).nullifyTransientReferences(entityEntry.getDeletedState(), propTypes);
    new Nullability(session).checkNullability(entityEntry.getDeletedState(), persister, Nullability.NullabilityCheckType.DELETE);
    persistenceContext.getNullifiableEntityKeys().add(key);
    if (isOrphanRemovalBeforeUpdates) {
        // TODO: The removeOrphan concept is a temporary "hack" for HHH-6484.  This should be removed once action/task
        // ordering is improved.
        session.getActionQueue().addAction(new OrphanRemovalAction(entityEntry.getId(), deletedState, version, entity, persister, isCascadeDeleteEnabled, session));
    } else {
        // Ensures that containing deletions happen before sub-deletions
        session.getActionQueue().addAction(new EntityDeleteAction(entityEntry.getId(), deletedState, version, entity, persister, isCascadeDeleteEnabled, session));
    }
    cascadeAfterDelete(session, persister, entity, transientEntities);
// the entry will be removed after the flush, and will no longer
// override the stale snapshot
// This is now handled by removeEntity() in EntityDeleteAction
// persistenceContext.removeDatabaseSnapshot(key);
}
Also used : EntityKey(org.hibernate.engine.spi.EntityKey) Type(org.hibernate.type.Type) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) ForeignKeys(org.hibernate.engine.internal.ForeignKeys) Nullability(org.hibernate.engine.internal.Nullability) OrphanRemovalAction(org.hibernate.action.internal.OrphanRemovalAction) EntityDeleteAction(org.hibernate.action.internal.EntityDeleteAction)

Example 3 with Nullability

use of org.hibernate.engine.internal.Nullability in project hibernate-orm by hibernate.

the class AbstractEntityInsertAction method nullifyTransientReferencesIfNotAlready.

/**
 * Nullifies any references to transient entities in the entity state
 * maintained by this action. References to transient entities
 * should be nullified when an entity is made "managed" or when this
 * action is executed, whichever is first.
 * <p/>
 * References will only be nullified the first time this method is
 * called for a this object, so it can safely be called both when
 * the entity is made "managed" and when this action is executed.
 *
 * @see {@link #makeEntityManaged() }
 */
protected final void nullifyTransientReferencesIfNotAlready() {
    if (!areTransientReferencesNullified) {
        new ForeignKeys.Nullifier(getInstance(), false, isEarlyInsert(), getSession()).nullifyTransientReferences(getState(), getPersister().getPropertyTypes());
        new Nullability(getSession()).checkNullability(getState(), getPersister(), false);
        areTransientReferencesNullified = true;
    }
}
Also used : ForeignKeys(org.hibernate.engine.internal.ForeignKeys) Nullability(org.hibernate.engine.internal.Nullability)

Aggregations

Nullability (org.hibernate.engine.internal.Nullability)3 ForeignKeys (org.hibernate.engine.internal.ForeignKeys)2 AssertionFailure (org.hibernate.AssertionFailure)1 EntityDeleteAction (org.hibernate.action.internal.EntityDeleteAction)1 EntityUpdateAction (org.hibernate.action.internal.EntityUpdateAction)1 OrphanRemovalAction (org.hibernate.action.internal.OrphanRemovalAction)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 Status (org.hibernate.engine.spi.Status)1 EventSource (org.hibernate.event.spi.EventSource)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 Type (org.hibernate.type.Type)1