All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.search.engine.impl.HibernateSessionLoadingInitializer Maven / Gradle / Ivy

There is a newer version: 5.11.12.Final
Show newest version
/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or .
 */

package org.hibernate.search.engine.impl;

import java.util.Collection;
import java.util.Map;

import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.search.spi.InstanceInitializer;

/**
 * This EntityInitializer is relative to a specific Hibernate Session,
 * so it's able to attach detached collections to it's Session.
 *
 * @author Sanne Grinovero (C) 2011 Red Hat Inc.
 */
public class HibernateSessionLoadingInitializer extends HibernateStatelessInitializer implements InstanceInitializer {

	private final SessionImplementor hibernateSession;

	public HibernateSessionLoadingInitializer(SessionImplementor hibernateSession) {
		this.hibernateSession = hibernateSession;
	}

	@Override
	public Object unproxy(Object instance) {
		if ( instance instanceof HibernateProxy ) {
			final HibernateProxy proxy = (HibernateProxy) instance;
			final LazyInitializer lazyInitializer = proxy.getHibernateLazyInitializer();
			Object initialized = lazyInitializer.getImplementation( hibernateSession );
			if ( initialized != null ) {
				return initialized;
			}
			else {
				// This is the case in which the proxy was created by a different session.
				// unproxyAndReassociate is the ultimate bomb,
				// able to deal with a Session change:
				return hibernateSession.getPersistenceContext().unproxyAndReassociate( proxy );
			}
		}
		return instance;
	}

	@Override
	public  Collection initializeCollection(Collection value) {
		//No action needed
		return value;
	}

	@Override
	public  Map initializeMap(Map value) {
		//No action needed
		return value;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy