use of org.hibernate.collection.spi.PersistentCollection 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)
}
}
use of org.hibernate.collection.spi.PersistentCollection 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);
}
}
use of org.hibernate.collection.spi.PersistentCollection 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();
}
use of org.hibernate.collection.spi.PersistentCollection 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;
}
use of org.hibernate.collection.spi.PersistentCollection in project hibernate-orm by hibernate.
the class Cascade method deleteOrphans.
/**
* Delete any entities that were removed from the collection
*/
private static void deleteOrphans(EventSource eventSource, String entityName, PersistentCollection pc) throws HibernateException {
//TODO: suck this logic into the collection!
final Collection orphans;
if (pc.wasInitialized()) {
final CollectionEntry ce = eventSource.getPersistenceContext().getCollectionEntry(pc);
orphans = ce == null ? java.util.Collections.EMPTY_LIST : ce.getOrphans(entityName, pc);
} else {
orphans = pc.getQueuedOrphans(entityName);
}
for (Object orphan : orphans) {
if (orphan != null) {
LOG.tracev("Deleting orphaned entity instance: {0}", entityName);
eventSource.delete(entityName, orphan, false, new HashSet());
}
}
}
Aggregations