Search in sources :

Example 21 with CollectionEntry

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

the class StatefulPersistenceContext method addUninitializedDetachedCollection.

@Override
public void addUninitializedDetachedCollection(CollectionPersister persister, PersistentCollection collection) {
    final CollectionEntry ce = new CollectionEntry(persister, collection.getKey());
    addCollection(collection, ce, collection.getKey());
    if (persister.getBatchSize() > 1) {
        getBatchFetchQueue().addBatchLoadableCollection(collection, ce);
    }
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Example 22 with CollectionEntry

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

the class StatefulPersistenceContext method serialize.

/**
	 * Used by the owning session to explicitly control serialization of the
	 * persistence context.
	 *
	 * @param oos The stream to which the persistence context should get written
	 * @throws IOException serialization errors.
	 */
public void serialize(ObjectOutputStream oos) throws IOException {
    final boolean tracing = LOG.isTraceEnabled();
    if (tracing) {
        LOG.trace("Serializing persisatence-context");
    }
    oos.writeBoolean(defaultReadOnly);
    oos.writeBoolean(hasNonReadOnlyEntities);
    oos.writeInt(entitiesByKey.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + entitiesByKey.size() + "] entitiesByKey entries");
    }
    for (Map.Entry<EntityKey, Object> entry : entitiesByKey.entrySet()) {
        entry.getKey().serialize(oos);
        oos.writeObject(entry.getValue());
    }
    oos.writeInt(entitiesByUniqueKey.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + entitiesByUniqueKey.size() + "] entitiesByUniqueKey entries");
    }
    for (Map.Entry<EntityUniqueKey, Object> entry : entitiesByUniqueKey.entrySet()) {
        entry.getKey().serialize(oos);
        oos.writeObject(entry.getValue());
    }
    oos.writeInt(proxiesByKey.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + proxiesByKey.size() + "] proxiesByKey entries");
    }
    for (Map.Entry<EntityKey, Object> entry : proxiesByKey.entrySet()) {
        entry.getKey().serialize(oos);
        oos.writeObject(entry.getValue());
    }
    oos.writeInt(entitySnapshotsByKey.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + entitySnapshotsByKey.size() + "] entitySnapshotsByKey entries");
    }
    for (Map.Entry<EntityKey, Object> entry : entitySnapshotsByKey.entrySet()) {
        entry.getKey().serialize(oos);
        oos.writeObject(entry.getValue());
    }
    entityEntryContext.serialize(oos);
    oos.writeInt(collectionsByKey.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + collectionsByKey.size() + "] collectionsByKey entries");
    }
    for (Map.Entry<CollectionKey, PersistentCollection> entry : collectionsByKey.entrySet()) {
        entry.getKey().serialize(oos);
        oos.writeObject(entry.getValue());
    }
    oos.writeInt(collectionEntries.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + collectionEntries.size() + "] collectionEntries entries");
    }
    for (Map.Entry<PersistentCollection, CollectionEntry> entry : collectionEntries.entrySet()) {
        oos.writeObject(entry.getKey());
        entry.getValue().serialize(oos);
    }
    oos.writeInt(arrayHolders.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + arrayHolders.size() + "] arrayHolders entries");
    }
    for (Map.Entry<Object, PersistentCollection> entry : arrayHolders.entrySet()) {
        oos.writeObject(entry.getKey());
        oos.writeObject(entry.getValue());
    }
    oos.writeInt(nullifiableEntityKeys.size());
    if (tracing) {
        LOG.trace("Starting serialization of [" + nullifiableEntityKeys.size() + "] nullifiableEntityKey entries");
    }
    for (EntityKey entry : nullifiableEntityKeys) {
        entry.serialize(oos);
    }
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) CollectionKey(org.hibernate.engine.spi.CollectionKey) EntityKey(org.hibernate.engine.spi.EntityKey) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EntityUniqueKey(org.hibernate.engine.spi.EntityUniqueKey) ConcurrentReferenceHashMap(org.hibernate.internal.util.collections.ConcurrentReferenceHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) IdentityMap(org.hibernate.internal.util.collections.IdentityMap)

Example 23 with CollectionEntry

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

the class StatefulPersistenceContext method clear.

@Override
public void clear() {
    for (Object o : proxiesByKey.values()) {
        if (o == null) {
            //entry may be GCd
            continue;
        }
        ((HibernateProxy) o).getHibernateLazyInitializer().unsetSession();
    }
    for (Entry<Object, EntityEntry> objectEntityEntryEntry : entityEntryContext.reentrantSafeEntityEntries()) {
        // todo : I dont think this need be reentrant safe
        if (objectEntityEntryEntry.getKey() instanceof PersistentAttributeInterceptable) {
            final PersistentAttributeInterceptor interceptor = ((PersistentAttributeInterceptable) objectEntityEntryEntry.getKey()).$$_hibernate_getInterceptor();
            if (interceptor instanceof LazyAttributeLoadingInterceptor) {
                ((LazyAttributeLoadingInterceptor) interceptor).unsetSession();
            }
        }
    }
    for (Map.Entry<PersistentCollection, CollectionEntry> aCollectionEntryArray : IdentityMap.concurrentEntries(collectionEntries)) {
        aCollectionEntryArray.getKey().unsetSession(getSession());
    }
    arrayHolders.clear();
    entitiesByKey.clear();
    entitiesByUniqueKey.clear();
    entityEntryContext.clear();
    //		entityEntries.clear();
    parentsByChild.clear();
    entitySnapshotsByKey.clear();
    collectionsByKey.clear();
    collectionEntries.clear();
    if (unownedCollections != null) {
        unownedCollections.clear();
    }
    proxiesByKey.clear();
    nullifiableEntityKeys.clear();
    if (batchFetchQueue != null) {
        batchFetchQueue.clear();
    }
    // defaultReadOnly is unaffected by clear()
    hasNonReadOnlyEntities = false;
    if (loadContexts != null) {
        loadContexts.cleanup();
    }
    naturalIdXrefDelegate.clear();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) EntityEntry(org.hibernate.engine.spi.EntityEntry) LazyAttributeLoadingInterceptor(org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) PersistentAttributeInterceptor(org.hibernate.engine.spi.PersistentAttributeInterceptor) PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) ConcurrentReferenceHashMap(org.hibernate.internal.util.collections.ConcurrentReferenceHashMap) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) IdentityMap(org.hibernate.internal.util.collections.IdentityMap)

Example 24 with CollectionEntry

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

the class StatefulPersistenceContext method deserialize.

/**
	 * Used by the owning session to explicitly control deserialization of the persistence context.
	 *
	 * @param ois The stream from which the persistence context should be read
	 * @param session The owning session
	 *
	 * @return The deserialized StatefulPersistenceContext
	 *
	 * @throws IOException deserialization errors.
	 * @throws ClassNotFoundException deserialization errors.
	 */
public static StatefulPersistenceContext deserialize(ObjectInputStream ois, SessionImplementor session) throws IOException, ClassNotFoundException {
    final boolean tracing = LOG.isTraceEnabled();
    if (tracing) {
        LOG.trace("Serializing persistent-context");
    }
    final StatefulPersistenceContext rtn = new StatefulPersistenceContext(session);
    SessionFactoryImplementor sfi = session.getFactory();
    try {
        rtn.defaultReadOnly = ois.readBoolean();
        // todo : we can actually just determine this from the incoming EntityEntry-s
        rtn.hasNonReadOnlyEntities = ois.readBoolean();
        int count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] entitiesByKey entries");
        }
        rtn.entitiesByKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
        for (int i = 0; i < count; i++) {
            rtn.entitiesByKey.put(EntityKey.deserialize(ois, sfi), ois.readObject());
        }
        count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] entitiesByUniqueKey entries");
        }
        rtn.entitiesByUniqueKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
        for (int i = 0; i < count; i++) {
            rtn.entitiesByUniqueKey.put(EntityUniqueKey.deserialize(ois, session), ois.readObject());
        }
        count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] proxiesByKey entries");
        }
        //noinspection unchecked
        rtn.proxiesByKey = new ConcurrentReferenceHashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count, .75f, 1, ConcurrentReferenceHashMap.ReferenceType.STRONG, ConcurrentReferenceHashMap.ReferenceType.WEAK, null);
        for (int i = 0; i < count; i++) {
            final EntityKey ek = EntityKey.deserialize(ois, sfi);
            final Object proxy = ois.readObject();
            if (proxy instanceof HibernateProxy) {
                ((HibernateProxy) proxy).getHibernateLazyInitializer().setSession(session);
                rtn.proxiesByKey.put(ek, proxy);
            } else {
                // otherwise, the proxy was pruned during the serialization process
                if (tracing) {
                    LOG.trace("Encountered pruned proxy");
                }
            }
        }
        count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] entitySnapshotsByKey entries");
        }
        rtn.entitySnapshotsByKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
        for (int i = 0; i < count; i++) {
            rtn.entitySnapshotsByKey.put(EntityKey.deserialize(ois, sfi), ois.readObject());
        }
        rtn.entityEntryContext = EntityEntryContext.deserialize(ois, rtn);
        count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] collectionsByKey entries");
        }
        rtn.collectionsByKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
        for (int i = 0; i < count; i++) {
            rtn.collectionsByKey.put(CollectionKey.deserialize(ois, session), (PersistentCollection) ois.readObject());
        }
        count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] collectionEntries entries");
        }
        rtn.collectionEntries = IdentityMap.instantiateSequenced(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
        for (int i = 0; i < count; i++) {
            final PersistentCollection pc = (PersistentCollection) ois.readObject();
            final CollectionEntry ce = CollectionEntry.deserialize(ois, session);
            pc.setCurrentSession(session);
            rtn.collectionEntries.put(pc, ce);
        }
        count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] arrayHolders entries");
        }
        rtn.arrayHolders = new IdentityHashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
        for (int i = 0; i < count; i++) {
            rtn.arrayHolders.put(ois.readObject(), (PersistentCollection) ois.readObject());
        }
        count = ois.readInt();
        if (tracing) {
            LOG.trace("Starting deserialization of [" + count + "] nullifiableEntityKey entries");
        }
        rtn.nullifiableEntityKeys = new HashSet<>();
        for (int i = 0; i < count; i++) {
            rtn.nullifiableEntityKeys.add(EntityKey.deserialize(ois, sfi));
        }
    } catch (HibernateException he) {
        throw new InvalidObjectException(he.getMessage());
    }
    return rtn;
}
Also used : CollectionEntry(org.hibernate.engine.spi.CollectionEntry) HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) HibernateProxy(org.hibernate.proxy.HibernateProxy) EntityKey(org.hibernate.engine.spi.EntityKey) PersistentCollection(org.hibernate.collection.spi.PersistentCollection) InvalidObjectException(java.io.InvalidObjectException)

Example 25 with CollectionEntry

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

the class StatefulPersistenceContext method getLoadedCollectionOwnerOrNull.

@Override
public Object getLoadedCollectionOwnerOrNull(PersistentCollection collection) {
    final CollectionEntry ce = getCollectionEntry(collection);
    if (ce.getLoadedPersister() == null) {
        return null;
    }
    Object loadedOwner = null;
    // TODO: an alternative is to check if the owner has changed; if it hasn't then
    // return collection.getOwner()
    final Serializable entityId = getLoadedCollectionOwnerIdOrNull(ce);
    if (entityId != null) {
        loadedOwner = getCollectionOwner(entityId, ce.getLoadedPersister());
    }
    return loadedOwner;
}
Also used : Serializable(java.io.Serializable) CollectionEntry(org.hibernate.engine.spi.CollectionEntry)

Aggregations

CollectionEntry (org.hibernate.engine.spi.CollectionEntry)42 Session (org.hibernate.Session)19 TestForIssue (org.hibernate.testing.TestForIssue)19 Test (org.junit.Test)19 EntityEntry (org.hibernate.engine.spi.EntityEntry)16 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)12 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)11 Serializable (java.io.Serializable)6 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 Map (java.util.Map)5 Triggerable (org.hibernate.testing.logger.Triggerable)5 HibernateException (org.hibernate.HibernateException)4 IdentityMap (org.hibernate.internal.util.collections.IdentityMap)4 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 HashMap (java.util.HashMap)3 EntityKey (org.hibernate.engine.spi.EntityKey)3 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)3 IdentityHashMap (java.util.IdentityHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 CollectionKey (org.hibernate.engine.spi.CollectionKey)2