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

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

Introduction

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

Prototype

boolean isMutable();

Source Link

Document

Determine whether instances of this entity are considered mutable.

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();
    }/* w w  w.j a  va 2 s  .c om*/

    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;
}

From source file:org.emonocot.model.BaseTest.java

License:Open Source License

/**
 * Another problem with//from   w w w  . ja  v  a  2  s. c om
 * http://build.e-monocot.org/bugzilla/show_bug.cgi?id=262 Unexpected Taxon
 * Exception in DwC Harvesting even though the taxon is expected. Comparing
 * HibernateProxies with non-proxies means you can't use o1.getClass() ==
 * o2.getClass().
 * @throws Exception if there is a problem
 */
@Test
public final void testEqualsWithHibernateProxies() throws Exception {
    b2.setIdentifier("test");
    b2.setId(1L);
    b1.setIdentifier("test");
    b1.setId(1L);
    SessionImplementor sessionImplementor = EasyMock.createMock(SessionImplementor.class);
    SessionFactoryImplementor sessionFactoryImplementor = EasyMock.createMock(SessionFactoryImplementor.class);
    EntityPersister entityPersister = EasyMock.createMock(EntityPersister.class);
    PersistenceContext persistenceContext = EasyMock.createMock(PersistenceContext.class);
    EasyMock.expect(sessionImplementor.getFactory()).andReturn(sessionFactoryImplementor).anyTimes();
    EasyMock.expect(sessionFactoryImplementor.getEntityPersister((String) EasyMock.eq("Annotation")))
            .andReturn(entityPersister).anyTimes();
    EasyMock.expect(sessionImplementor.getPersistenceContext()).andReturn(persistenceContext);
    EasyMock.expect(persistenceContext.isDefaultReadOnly()).andReturn(Boolean.TRUE).anyTimes();
    EasyMock.expect(entityPersister.isMutable()).andReturn(Boolean.TRUE).anyTimes();
    EasyMock.expect(sessionImplementor.isClosed()).andReturn(Boolean.FALSE).anyTimes();
    EasyMock.expect(sessionImplementor.isOpen()).andReturn(Boolean.TRUE).anyTimes();
    EasyMock.expect(sessionImplementor.isConnected()).andReturn(Boolean.TRUE).anyTimes();
    EasyMock.expect(sessionImplementor.immediateLoad(EasyMock.eq("Annotation"), EasyMock.eq(1L))).andReturn(b2)
            .anyTimes();

    EasyMock.replay(sessionImplementor, sessionFactoryImplementor, entityPersister, persistenceContext);

    JavassistProxyFactory javassistProxyFactory = new JavassistProxyFactory();
    Set interfaces = new HashSet();
    interfaces.add(HibernateProxy.class);
    interfaces.add(Serializable.class);
    interfaces.add(Identifiable.class);
    interfaces.add(SecuredObject.class);

    javassistProxyFactory.postInstantiate("Annotation", Annotation.class, interfaces,
            Annotation.class.getDeclaredMethod("getId"),
            Annotation.class.getDeclaredMethod("setId", Long.class), null);

    b3 = javassistProxyFactory.getProxy(1L, sessionImplementor);

    EasyMock.verify(sessionImplementor, sessionFactoryImplementor, entityPersister, persistenceContext);
    assertTrue("Equals should return true", b1.equals(b3));
}