Example usage for org.hibernate.persister.entity EntityPersister getPropertyValuesToInsert

List of usage examples for org.hibernate.persister.entity EntityPersister getPropertyValuesToInsert

Introduction

In this page you can find the example usage for org.hibernate.persister.entity EntityPersister getPropertyValuesToInsert.

Prototype

Object[] getPropertyValuesToInsert(Object object, Map mergeMap, SharedSessionContractImplementor session)
        throws HibernateException;

Source Link

Document

Return the values of the insertable properties of the object (including backrefs)

Usage

From source file:org.codehaus.groovy.grails.orm.hibernate.support.ClosureEventTriggeringInterceptor.java

License:Apache License

@Override
protected Serializable performSaveOrReplicate(Object entity, EntityKey key, EntityPersister persister,
        boolean useIdentityColumn, Object anything, EventSource source, boolean requiresImmediateIdAccess) {
    validate(entity, persister, source);

    Serializable id = key == null ? null : key.getIdentifier();

    boolean inTxn = source.getJDBCContext().isTransactionInProgress();
    boolean shouldDelayIdentityInserts = !inTxn && !requiresImmediateIdAccess;

    // Put a placeholder in entries, so we don't recurse back and try to save() the
    // same object again. QUESTION: should this be done before onSave() is called?
    // likewise, should it be done before onUpdate()?
    source.getPersistenceContext().addEntry(entity, Status.SAVING, null, null, id, null, LockMode.WRITE,
            useIdentityColumn, persister, false, false);

    cascadeBeforeSave(source, persister, entity, anything);

    if (useIdentityColumn && !shouldDelayIdentityInserts) {
        log.trace("executing insertions");
        source.getActionQueue().executeInserts();
    }/*from   w  ww .j  a v a 2 s .co m*/

    Object[] values = persister.getPropertyValuesToInsert(entity, getMergeMap(anything), source);
    Type[] types = persister.getPropertyTypes();

    boolean substitute = substituteValuesIfNecessary(entity, id, values, persister, source);

    if (persister.hasCollections()) {
        substitute = substitute || visitCollectionsBeforeSave(entity, id, values, types, source);
    }

    if (substitute) {
        persister.setPropertyValues(entity, values, source.getEntityMode());
    }

    TypeHelper.deepCopy(values, types, persister.getPropertyUpdateability(), values, source);

    new ForeignKeys.Nullifier(entity, false, useIdentityColumn, source).nullifyTransientReferences(values,
            types);
    new Nullability(source).checkNullability(values, persister, false);

    if (useIdentityColumn) {
        EntityIdentityInsertAction insert = new EntityIdentityInsertAction(values, entity, persister, source,
                shouldDelayIdentityInserts);
        if (!shouldDelayIdentityInserts) {
            log.debug("executing identity-insert immediately");
            source.getActionQueue().execute(insert);
            id = insert.getGeneratedId();
            if (id != null) {
                // As of HHH-3904, if the id is null the operation was vetoed so we bail
                key = new EntityKey(id, persister, source.getEntityMode());
                source.getPersistenceContext().checkUniqueness(key, entity);
            }
        } else {
            log.debug("delaying identity-insert due to no transaction in progress");
            source.getActionQueue().addAction(insert);
            key = insert.getDelayedEntityKey();
        }
    }

    if (key != null) {
        Object version = Versioning.getVersion(values, persister);
        source.getPersistenceContext().addEntity(entity,
                (persister.isMutable() ? Status.MANAGED : Status.READ_ONLY), values, key, version,
                LockMode.WRITE, useIdentityColumn, persister, isVersionIncrementDisabled(), false);
        //source.getPersistenceContext().removeNonExist(new EntityKey(id, persister, source.getEntityMode()));

        if (!useIdentityColumn) {
            source.getActionQueue()
                    .addAction(new EntityInsertAction(id, values, entity, version, persister, source));
        }

        cascadeAfterSave(source, persister, entity, anything);
        // Very unfortunate code, but markInterceptorDirty is private. Once HHH-3904 is resolved remove this overridden method!
        if (markInterceptorDirtyMethod != null) {
            ReflectionUtils.invokeMethod(markInterceptorDirtyMethod, this, entity, persister, source);
        }
    }

    return id;
}