private static boolean collectionIsInitialized(Object collection) { return !(collection instanceof PersistentCollection) || ( (PersistentCollection) collection ).wasInitialized(); }
private static boolean collectionIsInitialized(Object collection) { return !(collection instanceof PersistentCollection) || ( (PersistentCollection) collection ).wasInitialized(); }
@Override public CacheEntry<T> assertIsLoaded() { Assert.assertTrue("Collection wasn't initialized yet", persistentCollection.wasInitialized()); return this; }
private void doLoad() { if(!persistentCollection.wasInitialized()) { Hibernate.initialize(persistentCollection); } else { throw new RuntimeException("Collection was already initialized"); } }
/** * Check if the proxy or persistent collection is initialized. * * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> * @return true if the argument is already initialized, or is not a proxy or collection */ public static boolean isInitialized(Object proxy) { if ( proxy instanceof HibernateProxy ) { return !( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().isUninitialized(); } else if ( proxy instanceof PersistentCollection ) { return ( ( PersistentCollection ) proxy ).wasInitialized(); } else { return true; } }
@Override public String toText() { if ((getList() instanceof PersistentCollection) && (!((PersistentCollection) getList()).wasInitialized())) { return ""; } return StringUtils.join(getList().toArray(), " "); }
public boolean isInitialized(Object o) { if (o instanceof HibernateProxy) { return !((HibernateProxy)o).getHibernateLazyInitializer().isUninitialized(); } if (o instanceof PersistentCollection) { return ((PersistentCollection)o).wasInitialized(); } return true; }
/** * Delete any entities that were removed from the collection */ private static void deleteOrphans(String entityName, PersistentCollection pc, SessionImplementor session) throws HibernateException { if ( pc.wasInitialized() ) { //can't be any orphans if it was not initialized! CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(pc); if (ce!=null) { Iterator orphanIter = ce.getOrphans(entityName, pc).iterator(); while ( orphanIter.hasNext() ) { Object orphan = orphanIter.next(); if (orphan!=null) { if ( log.isTraceEnabled() ) log.trace("deleting orphaned: " + entityName); session.delete(entityName, orphan, false); } } } } }
/** * Check if the proxy or persistent collection is initialized. * * @param proxy a persistable object, proxy, persistent collection or <tt>null</tt> * @return true if the argument is already initialized, or is not a proxy or collection */ public static boolean isInitialized(Object proxy) { if ( proxy instanceof HibernateProxy ) { return !( ( HibernateProxy ) proxy ).getHibernateLazyInitializer().isUninitialized(); } else if ( proxy instanceof PersistentCollection ) { return ( ( PersistentCollection ) proxy ).wasInitialized(); } else { return true; } }
/** * {@inheritDoc} * <p> * This is important.. Otherwise we can get a stackoverflow calling toXML() * * @see com.xpn.xwiki.objects.BaseProperty#toString() */ @Override public String toString() { if ((getList() instanceof PersistentCollection) && (!((PersistentCollection) getList()).wasInitialized())) { return ""; } return toXMLString(); } }
protected Object findLazyValue(PersistentCollection coll) { // If lazy-loaded, not yet loaded, may serialize as null? if (!Feature.FORCE_LAZY_LOADING.enabledIn(_features) && !coll.wasInitialized()) { return null; } if(_sessionFactory != null) { Session session = openTemporarySessionForLoading(coll); initializeCollection(coll, session); } return coll.getValue(); }
protected Object findLazyValue(PersistentCollection coll) { // If lazy-loaded, not yet loaded, may serialize as null? if (!Feature.FORCE_LAZY_LOADING.enabledIn(_features) && !coll.wasInitialized()) { return null; } if(_sessionFactory != null) { Session session = openTemporarySessionForLoading(coll); initializeCollection(coll, session); } return coll.getValue(); }
public boolean isSnapshotEmpty(PersistentCollection collection) { //TODO: does this really need to be here? // does the collection already have // it's own up-to-date snapshot? return collection.wasInitialized() && ( getLoadedPersister()==null || getLoadedPersister().isMutable() ) && collection.isSnapshotEmpty( getSnapshot() ); }
/** * Called after execution of an action */ public void afterAction(PersistentCollection collection) { loadedKey = getCurrentKey(); setLoadedPersister( getCurrentPersister() ); boolean resnapshot = collection.wasInitialized() && ( isDoremove() || isDorecreate() || isDoupdate() ); if ( resnapshot ) { snapshot = loadedPersister==null || !loadedPersister.isMutable() ? null : collection.getSnapshot(loadedPersister); //re-snapshot } collection.postAction(); }
@Override public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException { if (value instanceof PersistentCollection) { PersistentCollection coll = (PersistentCollection) value; // If lazy-loaded, not yet loaded, may serialize as null? if (!Feature.FORCE_LAZY_LOADING.enabledIn(_features) && !coll.wasInitialized()) { provider.defaultSerializeNull(jgen); return; } value = coll.getValue(); if (value == null) { provider.defaultSerializeNull(jgen); return; } } if (_serializer == null) { // sanity check... throw JsonMappingException.from(jgen, "PersistentCollection does not have serializer set"); } if (Feature.REPLACE_PERSISTENT_COLLECTIONS.enabledIn(_features)) { value = convertToJavaCollection(value); // Strip PersistentCollection } _serializer.serialize(value, jgen, provider); }
@Override public void serializeWithType(Object value, JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) throws IOException { if (value instanceof PersistentCollection) { PersistentCollection coll = (PersistentCollection) value; if (!Feature.FORCE_LAZY_LOADING.enabledIn(_features) && !coll.wasInitialized()) { provider.defaultSerializeNull(jgen); return; } value = coll.getValue(); if (value == null) { provider.defaultSerializeNull(jgen); return; } } if (_serializer == null) { // sanity check... throw JsonMappingException.from(jgen, "PersistentCollection does not have serializer set"); } if (Feature.REPLACE_PERSISTENT_COLLECTIONS.enabledIn(_features)) { value = convertToJavaCollection(value); // Strip PersistentCollection } _serializer.serializeWithType(value, jgen, provider, typeSer); }
/** * Determine if the collection is "really" dirty, by checking dirtiness * of the collection elements, if necessary */ private void dirty(PersistentCollection collection) throws HibernateException { boolean forceDirty = collection.wasInitialized() && !collection.isDirty() && //optimization getLoadedPersister() != null && getLoadedPersister().isMutable() && //optimization ( collection.isDirectlyAccessible() || getLoadedPersister().getElementType().isMutable() ) && //optimization !collection.equalsSnapshot( getLoadedPersister() ); if ( forceDirty ) { collection.dirty(); } }
Object processCollection(Object collection, CollectionType type) throws HibernateException { if (collection==CollectionType.UNFETCHED_COLLECTION) return null; SessionImplementor session = getSession(); Serializable key = getKey(); CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() ); if (isUpdate) removeCollection(persister, key, session); if ( collection!=null && (collection instanceof PersistentCollection) ) { PersistentCollection wrapper = (PersistentCollection) collection; wrapper.setCurrentSession(session); if ( wrapper.wasInitialized() ) { session.getPersistenceContext().addNewCollection(wrapper, persister); } else { reattachCollection( wrapper, wrapper.getCollectionSnapshot() ); } } else { // otherwise a null or brand new collection // this will also (inefficiently) handle arrays, which // have no snapshot, so we can't do any better //processArrayOrNewCollection(collection, type); } return null; }
/** * Reattach a detached (disassociated) initialized or uninitialized * collection wrapper, using a snapshot carried with the collection * wrapper */ protected void reattachCollection(PersistentCollection collection, CollectionType type) throws HibernateException { if ( collection.wasInitialized() ) { CollectionPersister collectionPersister = getSession().getFactory() .getCollectionPersister( type.getRole() ); getSession().getPersistenceContext() .addInitializedDetachedCollection( collectionPersister, collection ); } else { if ( !isCollectionSnapshotValid(collection) ) { throw new HibernateException( "could not reassociate uninitialized transient collection" ); } CollectionPersister collectionPersister = getSession().getFactory() .getCollectionPersister( collection.getRole() ); getSession().getPersistenceContext() .addUninitializedDetachedCollection( collectionPersister, collection ); } }
/** * Reattach a detached (disassociated) initialized or uninitialized * collection wrapper, using a snapshot carried with the collection * wrapper */ protected void reattachCollection(PersistentCollection collection, CollectionSnapshot snapshot) throws HibernateException { if ( collection.wasInitialized() ) { getSession().getPersistenceContext() .addInitializedDetachedCollection( collection, snapshot, getSession().getEntityMode() ); } else { if ( !isCollectionSnapshotValid(snapshot) ) { throw new HibernateException( "could not reassociate uninitialized transient collection" ); } getSession().getPersistenceContext().addUninitializedDetachedCollection( collection, getSession().getFactory().getCollectionPersister( snapshot.getRole() ), snapshot.getKey(), getSession().getEntityMode() ); } }