Search in sources :

Example 41 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class ParentChildTest method testReplicate.

@Test
public void testReplicate() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    Container baz = new Container();
    Contained f = new Contained();
    List list = new ArrayList();
    list.add(baz);
    f.setBag(list);
    List list2 = new ArrayList();
    list2.add(f);
    baz.setBag(list2);
    s.save(f);
    s.save(baz);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.replicate(baz, ReplicationMode.OVERWRITE);
    // HHH-2378
    SessionImpl x = (SessionImpl) s;
    EntityEntry entry = x.getPersistenceContext().getEntry(baz);
    assertNull(entry.getVersion());
    // ~~~~~~~
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.replicate(baz, ReplicationMode.IGNORE);
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    s.delete(baz);
    s.delete(f);
    s.getTransaction().commit();
    s.close();
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SessionImpl(org.hibernate.internal.SessionImpl) Session(org.hibernate.Session) Test(org.junit.Test)

Example 42 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class EvictAuditDataAfterCommitTest method checkEmptyAuditSessionCache.

private void checkEmptyAuditSessionCache(Session session, String... auditEntityNames) {
    List<String> entityNames = Arrays.asList(auditEntityNames);
    PersistenceContext persistenceContext = ((SessionImplementor) session).getPersistenceContext();
    for (Map.Entry<Object, EntityEntry> entrySet : persistenceContext.reentrantSafeEntityEntries()) {
        final EntityEntry entityEntry = entrySet.getValue();
        if (entityNames.contains(entityEntry.getEntityName())) {
            assert false : "Audit data shall not be stored in the session level cache. This causes performance issues.";
        }
        Assert.assertFalse("Revision entity shall not be stored in the session level cache. This causes performance issues.", SequenceIdRevisionEntity.class.getName().equals(entityEntry.getEntityName()));
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Map(java.util.Map)

Example 43 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class DefaultPersistEventListener method onPersist.

/**
	 * 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) {
                //NOTE EARLY EXIT!
                return;
            } else {
                throw new PersistentObjectException("uninitialized proxy passed to persist()");
            }
        }
        entity = li.getImplementation();
    } else {
        entity = object;
    }
    final String entityName;
    if (event.getEntityName() != null) {
        entityName = event.getEntityName();
    } else {
        entityName = source.bestGuessEntityName(entity);
        event.setEntityName(entityName);
    }
    final EntityEntry entityEntry = source.getPersistenceContext().getEntry(entity);
    EntityState entityState = getEntityState(entity, entityName, entityEntry, source);
    if (entityState == EntityState.DETACHED) {
        // JPA 2, in its version of a "foreign generated", allows the id attribute value
        // to be manually set by the user, even though this manual value is irrelevant.
        // The issue is that this causes problems with the Hibernate unsaved-value strategy
        // which comes into play here in determining detached/transient state.
        //
        // Detect if we have this situation and if so null out the id value and calculate the
        // entity state again.
        // NOTE: entityEntry must be null to get here, so we cannot use any of its values
        EntityPersister persister = source.getFactory().getEntityPersister(entityName);
        if (ForeignGenerator.class.isInstance(persister.getIdentifierGenerator())) {
            if (LOG.isDebugEnabled() && persister.getIdentifier(entity, source) != null) {
                LOG.debug("Resetting entity id attribute to null for foreign generator");
            }
            persister.setIdentifier(entity, null, source);
            entityState = getEntityState(entity, entityName, entityEntry, 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;
            }
        case DELETED:
            {
                entityEntry.setStatus(Status.MANAGED);
                entityEntry.setDeletedState(null);
                event.getSession().getActionQueue().unScheduleDeletion(entityEntry, event.getObject());
                entityIsDeleted(event, createCache);
                break;
            }
        default:
            {
                throw new ObjectDeletedException("deleted entity passed to persist", null, getLoggableName(event.getEntityName(), entity));
            }
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityEntry(org.hibernate.engine.spi.EntityEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ObjectDeletedException(org.hibernate.ObjectDeletedException) PersistentObjectException(org.hibernate.PersistentObjectException) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 44 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class DefaultFlushEntityEventListener method handleInterception.

protected boolean handleInterception(FlushEntityEvent event) {
    SessionImplementor session = event.getSession();
    EntityEntry entry = event.getEntityEntry();
    EntityPersister persister = entry.getPersister();
    Object entity = event.getEntity();
    //give the Interceptor a chance to modify property values
    final Object[] values = event.getPropertyValues();
    final boolean intercepted = invokeInterceptor(session, entity, entry, values, persister);
    //now we might need to recalculate the dirtyProperties array
    if (intercepted && event.isDirtyCheckPossible() && !event.isDirtyCheckHandledByInterceptor()) {
        int[] dirtyProperties;
        if (event.hasDatabaseSnapshot()) {
            dirtyProperties = persister.findModified(event.getDatabaseSnapshot(), values, entity, session);
        } else {
            dirtyProperties = persister.findDirty(values, entry.getLoadedState(), entity, session);
        }
        event.setDirtyProperties(dirtyProperties);
    }
    return intercepted;
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEntry(org.hibernate.engine.spi.EntityEntry) SessionImplementor(org.hibernate.engine.spi.SessionImplementor)

Example 45 with EntityEntry

use of org.hibernate.engine.spi.EntityEntry in project hibernate-orm by hibernate.

the class AbstractFlushingEventListener method prepareEntityFlushes.

/**
	 * process cascade save/update at the start of a flush to discover
	 * any newly referenced entity that must be passed to saveOrUpdate(),
	 * and also apply orphan delete
	 */
private void prepareEntityFlushes(EventSource session, PersistenceContext persistenceContext) throws HibernateException {
    LOG.debug("Processing flush-time cascades");
    final Object anything = getAnything();
    //safe from concurrent modification because of how concurrentEntries() is implemented on IdentityMap
    for (Map.Entry<Object, EntityEntry> me : persistenceContext.reentrantSafeEntityEntries()) {
        //		for ( Map.Entry me : IdentityMap.concurrentEntries( persistenceContext.getEntityEntries() ) ) {
        EntityEntry entry = (EntityEntry) me.getValue();
        Status status = entry.getStatus();
        if (status == Status.MANAGED || status == Status.SAVING || status == Status.READ_ONLY) {
            cascadeOnFlush(session, entry.getPersister(), me.getKey(), anything);
        }
    }
}
Also used : Status(org.hibernate.engine.spi.Status) EntityEntry(org.hibernate.engine.spi.EntityEntry) Map(java.util.Map) IdentityMap(org.hibernate.internal.util.collections.IdentityMap)

Aggregations

EntityEntry (org.hibernate.engine.spi.EntityEntry)79 Serializable (java.io.Serializable)22 EntityPersister (org.hibernate.persister.entity.EntityPersister)21 Session (org.hibernate.Session)18 Test (org.junit.Test)17 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)16 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)12 EntityKey (org.hibernate.engine.spi.EntityKey)12 TestForIssue (org.hibernate.testing.TestForIssue)12 AssertionFailure (org.hibernate.AssertionFailure)11 HibernateProxy (org.hibernate.proxy.HibernateProxy)11 HibernateException (org.hibernate.HibernateException)10 EventSource (org.hibernate.event.spi.EventSource)9 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)8 TransientObjectException (org.hibernate.TransientObjectException)7 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 Status (org.hibernate.engine.spi.Status)6 LazyInitializer (org.hibernate.proxy.LazyInitializer)6 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)5