Search in sources :

Example 6 with CollectionKey

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

the class StatefulPersistenceContext method addCollection.

/**
	 * Add an collection to the cache, with a given collection entry.
	 *
	 * @param coll The collection for which we are adding an entry.
	 * @param entry The entry representing the collection.
	 * @param key The key of the collection's entry.
	 */
private void addCollection(PersistentCollection coll, CollectionEntry entry, Serializable key) {
    collectionEntries.put(coll, entry);
    final CollectionKey collectionKey = new CollectionKey(entry.getLoadedPersister(), key);
    final PersistentCollection old = collectionsByKey.put(collectionKey, coll);
    if (old != null) {
        if (old == coll) {
            throw new AssertionFailure("bug adding collection twice");
        }
        // or should it actually throw an exception?
        old.unsetSession(session);
        collectionEntries.remove(old);
    // watch out for a case where old is still referenced
    // somewhere in the object graph! (which is a user error)
    }
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AssertionFailure(org.hibernate.AssertionFailure) CollectionKey(org.hibernate.engine.spi.CollectionKey)

Example 7 with CollectionKey

use of org.hibernate.engine.spi.CollectionKey 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)

Aggregations

CollectionKey (org.hibernate.engine.spi.CollectionKey)7 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)6 Map (java.util.Map)2 AssertionFailure (org.hibernate.AssertionFailure)2 EntityMode (org.hibernate.EntityMode)2 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)2 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)2 IdentityMap (org.hibernate.internal.util.collections.IdentityMap)2 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)2 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 IdentityHashMap (java.util.IdentityHashMap)1 Iterator (java.util.Iterator)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 HibernateException (org.hibernate.HibernateException)1 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)1 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)1 StructuredCacheEntry (org.hibernate.cache.spi.entry.StructuredCacheEntry)1 UnstructuredCacheEntry (org.hibernate.cache.spi.entry.UnstructuredCacheEntry)1