org.hibernate.collection.internal.AbstractPersistentCollection Java Examples

The following examples show how to use org.hibernate.collection.internal.AbstractPersistentCollection. 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: Mutiny.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Asynchronously fetch an association that's configured for lazy loading.
 *
 * <pre>
 * {@code Mutiny.fetch(author.getBook()).map(book -> print(book.getTitle()));}
 * </pre>
 *
 * @param association a lazy-loaded association
 *
 * @return the fetched association, via a {@code Uni}
 *
 * @see org.hibernate.Hibernate#initialize(Object)
 */
static <T> Uni<T> fetch(T association) {
	if ( association == null ) {
		return Uni.createFrom().nullItem();
	}

	SharedSessionContractImplementor session;
	if ( association instanceof HibernateProxy) {
		session = ( (HibernateProxy) association ).getHibernateLazyInitializer().getSession();
	}
	else if ( association instanceof PersistentCollection) {
		session = ( (AbstractPersistentCollection) association ).getSession();
	}
	else {
		return Uni.createFrom().item( association );
	}
	if (session==null) {
		throw new LazyInitializationException("session closed");
	}
	return Uni.createFrom().completionStage(
			( (ReactiveSession) session ).reactiveFetch( association, false )
	);
}
 
Example #2
Source File: Stage.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Asynchronously fetch an association that's configured for lazy loading.
 *
 * <pre>
 * {@code Stage.fetch(author.getBook()).thenAccept(book -> print(book.getTitle()));}
 * </pre>
 *
 * @param association a lazy-loaded association
 *
 * @return the fetched association, via a {@code CompletionStage}
 *
 * @see org.hibernate.Hibernate#initialize(Object)
 */
static <T> CompletionStage<T> fetch(T association) {
	if ( association == null ) {
		return CompletionStages.nullFuture();
	}

	SharedSessionContractImplementor session;
	if ( association instanceof HibernateProxy) {
		session = ( (HibernateProxy) association ).getHibernateLazyInitializer().getSession();
	}
	else if ( association instanceof PersistentCollection) {
		session = ( (AbstractPersistentCollection) association ).getSession();
	}
	else {
		return CompletionStages.completedFuture( association );
	}
	if (session==null) {
		throw new LazyInitializationException("session closed");
	}
	return ( (ReactiveSession) session ).reactiveFetch( association, false );
}
 
Example #3
Source File: CollectionEntry.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void postInitialize(PersistentCollection collection) throws HibernateException {
	snapshot = getLoadedPersister().isMutable()
			? collection.getSnapshot( getLoadedPersister() )
			: null;
	collection.setSnapshot(loadedKey, role, snapshot);
	if ( getLoadedPersister().getBatchSize() > 1 ) {
		( (AbstractPersistentCollection) collection ).getSession()
				.getPersistenceContext()
				.getBatchFetchQueue()
				.removeBatchLoadableCollection( this );
	}
}
 
Example #4
Source File: CollectionType.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object replace(
		final Object original,
		final Object target,
		final SharedSessionContractImplementor session,
		final Object owner,
		final Map copyCache) throws HibernateException {
	if ( original == null ) {
		return null;
	}
	if ( !Hibernate.isInitialized( original ) ) {
		if ( ( (PersistentCollection) original ).hasQueuedOperations() ) {
			final AbstractPersistentCollection pc = (AbstractPersistentCollection) original;
			pc.replaceQueuedOperationValues( getPersister( session ), copyCache );
		}
		return target;
	}

	// for a null target, or a target which is the same as the original, we
	// need to put the merged elements in a new collection
	Object result = ( target == null ||
			target == original ||
			target == LazyPropertyInitializer.UNFETCHED_PROPERTY ) ?
			instantiateResult( original ) : target;

	//for arrays, replaceElements() may return a different reference, since
	//the array length might not match
	result = replaceElements( original, result, owner, copyCache, session );

	if ( original == target ) {
		// get the elements back into the target making sure to handle dirty flag
		boolean wasClean = PersistentCollection.class.isInstance( target ) && !( ( PersistentCollection ) target ).isDirty();
		//TODO: this is a little inefficient, don't need to do a whole
		//      deep replaceElements() call
		replaceElements( result, target, owner, copyCache, session );
		if ( wasClean ) {
			( ( PersistentCollection ) target ).clearDirty();
		}
		result = target;
	}

	return result;
}
 
Example #5
Source File: HibernateCollectionConverterImpl.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
@Override
public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
    if (((AbstractPersistentCollection) source).wasInitialized()) {
        context.convertAnother(new ArrayList((Collection) source));
    }
}