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);
}
}
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);
}
}
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();
}
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;
}
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;
}
Aggregations