Java Code Examples for org.hibernate.collection.spi.PersistentCollection#forceInitialization()

The following examples show how to use org.hibernate.collection.spi.PersistentCollection#forceInitialization() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateProxyHandler.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
public void initialize(Object o) {
    if (o instanceof PersistentCollection) {
        final PersistentCollection col = (PersistentCollection)o;
        if (!col.wasInitialized()) {
            col.forceInitialization();
        }
    }
    super.initialize(o);
}
 
Example 2
Source File: Collections.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 1. record the collection role that this collection is referenced by
 * 2. decide if the collection needs deleting/creating/updating (but
 *	don't actually schedule the action yet)
 */
@SuppressWarnings( {"JavaDoc"})
private static void prepareCollectionForUpdate(
		PersistentCollection collection,
		CollectionEntry entry,
		SessionFactoryImplementor factory) {
	if ( entry.isProcessed() ) {
		throw new AssertionFailure( "collection was processed twice by flush()" );
	}
	entry.setProcessed( true );

	final CollectionPersister loadedPersister = entry.getLoadedPersister();
	final CollectionPersister currentPersister = entry.getCurrentPersister();
	if ( loadedPersister != null || currentPersister != null ) {
		// it is or was referenced _somewhere_

		// check if the key changed
		// excludes marking key changed when the loaded key is a DelayedPostInsertIdentifier.
		final boolean keyChanged = currentPersister != null
				&& entry != null
				&& !currentPersister.getKeyType().isEqual( entry.getLoadedKey(), entry.getCurrentKey(), factory )
				&& !( entry.getLoadedKey() instanceof DelayedPostInsertIdentifier );

		// if either its role changed, or its key changed
		final boolean ownerChanged = loadedPersister != currentPersister || keyChanged;

		if ( ownerChanged ) {
			// do a check
			final boolean orphanDeleteAndRoleChanged =
					loadedPersister != null && currentPersister != null && loadedPersister.hasOrphanDelete();

			if (orphanDeleteAndRoleChanged) {
				throw new HibernateException(
						"Don't change the reference to a collection with delete-orphan enabled : "
								+ loadedPersister.getRole()
				);
			}

			// do the work
			if ( currentPersister != null ) {
				entry.setDorecreate( true );
			}

			if ( loadedPersister != null ) {
				// we will need to remove ye olde entries
				entry.setDoremove( true );
				if ( entry.isDorecreate() ) {
					LOG.trace( "Forcing collection initialization" );
					collection.forceInitialization();
				}
			}
		}
		else if ( collection.isDirty() ) {
			// the collection's elements have changed
			entry.setDoupdate( true );
		}
	}
}