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

org.hibernate.search.jpa.impl.FullTextEntityManagerImpl Maven / Gradle / Ivy

/*
 * 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.jpa.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.metamodel.Metamodel;

import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.MassIndexer;
import org.hibernate.search.Search;
import org.hibernate.search.SearchFactory;
import org.hibernate.search.exception.SearchException;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.query.engine.spi.QueryDescriptor;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;

/**
 * @author Emmanuel Bernard
 */
final class FullTextEntityManagerImpl implements FullTextEntityManager, Serializable {

	private static final Log log = LoggerFactory.make();

	private final EntityManager em;
	private FullTextSession ftSession;

	public FullTextEntityManagerImpl(EntityManager em) {
		if ( em == null ) {
			throw log.getNullEntityManagerPassedToFullEntityManagerCreationException();
		}
		this.em = em;
	}

	private FullTextSession getFullTextSession() {
		if ( ftSession == null ) {
			Object delegate = em.getDelegate();
			if ( delegate == null ) {
				throw new SearchException(
						"Trying to use Hibernate Search without an Hibernate EntityManager (no delegate)"
				);
			}
			else if ( Session.class.isAssignableFrom( delegate.getClass() ) ) {
				ftSession = Search.getFullTextSession( (Session) delegate );
			}
			else if ( EntityManager.class.isAssignableFrom( delegate.getClass() ) ) {
				//Some app servers wrap the EM twice
				delegate = ( (EntityManager) delegate ).getDelegate();
				if ( delegate == null ) {
					throw new SearchException(
							"Trying to use Hibernate Search without an Hibernate EntityManager (no delegate)"
					);
				}
				else if ( Session.class.isAssignableFrom( delegate.getClass() ) ) {
					ftSession = Search.getFullTextSession( (Session) delegate );
				}
				else {
					throw new SearchException(
							"Trying to use Hibernate Search without an Hibernate EntityManager: " + delegate.getClass()
					);
				}
			}
			else {
				throw new SearchException(
						"Trying to use Hibernate Search without an Hibernate EntityManager: " + delegate.getClass()
				);
			}
		}
		return ftSession;
	}

	@Override
	public FullTextQuery createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class... entities) {
		FullTextSession ftSession = getFullTextSession();
		return new FullTextQueryImpl( ftSession.createFullTextQuery( luceneQuery, entities ), ftSession );
	}

	@Override
	public FullTextQuery createFullTextQuery(QueryDescriptor descriptor, Class... entities) {
		FullTextSession ftSession = getFullTextSession();
		return new FullTextQueryImpl( ftSession.createFullTextQuery( descriptor, entities ), ftSession );
	}

	@Override
	public  void index(T entity) {
		getFullTextSession().index( entity );
	}

	@Override
	public SearchFactory getSearchFactory() {
		return getFullTextSession().getSearchFactory();
	}

	@Override
	public  void purge(Class entityType, Serializable id) {
		getFullTextSession().purge( entityType, id );
	}

	@Override
	public  void purgeAll(Class entityType) {
		getFullTextSession().purgeAll( entityType );
	}

	@Override
	public void flushToIndexes() {
		getFullTextSession().flushToIndexes();
	}

	@Override
	public void persist(Object entity) {
		em.persist( entity );
	}

	@Override
	public  T merge(T entity) {
		return em.merge( entity );
	}

	@Override
	public void remove(Object entity) {
		em.remove( entity );
	}

	@Override
	public  T find(Class entityClass, Object primaryKey) {
		return em.find( entityClass, primaryKey );
	}

	@Override
	public  T find(Class entityClass, Object primaryKey, Map hints) {
		return em.find( entityClass, primaryKey, hints );
	}

	@Override
	public  T find(Class entityClass, Object primaryKey, LockModeType lockModeType) {
		return em.find( entityClass, primaryKey, lockModeType );
	}

	@Override
	public  T find(Class entityClass, Object primaryKey, LockModeType lockModeType, Map hints) {
		return em.find( entityClass, primaryKey, lockModeType, hints );
	}

	@Override
	public  T getReference(Class entityClass, Object primaryKey) {
		return em.getReference( entityClass, primaryKey );
	}

	@Override
	public void flush() {
		em.flush();
	}

	@Override
	public void setFlushMode(FlushModeType flushMode) {
		em.setFlushMode( flushMode );
	}

	@Override
	public FlushModeType getFlushMode() {
		return em.getFlushMode();
	}

	@Override
	public void lock(Object entity, LockModeType lockMode) {
		em.lock( entity, lockMode );
	}

	@Override
	public void lock(Object entity, LockModeType lockModeType, Map hints) {
		em.lock( entity, lockModeType, hints );
	}

	@Override
	public void refresh(Object entity) {
		em.refresh( entity );
	}

	@Override
	public void refresh(Object entity, Map hints) {
		em.refresh( entity, hints );
	}

	@Override
	public void refresh(Object entity, LockModeType lockModeType) {
		em.refresh( entity, lockModeType );
	}

	@Override
	public void refresh(Object entity, LockModeType lockModeType, Map hints) {
		em.refresh( entity, lockModeType, hints );
	}

	@Override
	public void clear() {
		em.clear();
	}

	@Override
	public void detach(Object entity) {
		em.detach( entity );
	}

	@Override
	public boolean contains(Object entity) {
		return em.contains( entity );
	}

	@Override
	public LockModeType getLockMode(Object entity) {
		return em.getLockMode( entity );
	}

	@Override
	public void setProperty(String key, Object value) {
		em.setProperty( key, value );
	}

	@Override
	public Map getProperties() {
		return em.getProperties();
	}

	@Override
	public Query createQuery(String ejbqlString) {
		return em.createQuery( ejbqlString );
	}

	@Override
	public  TypedQuery createQuery(CriteriaQuery criteriaQuery) {
		return em.createQuery( criteriaQuery );
	}

	@Override
	public  TypedQuery createQuery(String qlString, Class resultClass) {
		return em.createQuery( qlString, resultClass );
	}

	@Override
	public Query createNamedQuery(String name) {
		return em.createNamedQuery( name );
	}

	@Override
	public  TypedQuery createNamedQuery(String name, Class resultClass) {
		return em.createNamedQuery( name, resultClass );
	}

	@Override
	public Query createNativeQuery(String sqlString) {
		return em.createNativeQuery( sqlString );
	}

	@Override
	public Query createNativeQuery(String sqlString, Class resultClass) {
		return em.createNativeQuery( sqlString, resultClass );
	}

	@Override
	public Query createNativeQuery(String sqlString, String resultSetMapping) {
		return em.createNativeQuery( sqlString, resultSetMapping );
	}

	@Override
	public void joinTransaction() {
		em.joinTransaction();
	}

	@Override
	public  T unwrap(Class type) {
		if ( type.equals( FullTextSession.class ) ) {
			@SuppressWarnings("unchecked")
			final T ftSession = (T) Search.getFullTextSession( em.unwrap( Session.class ) );
			return ftSession;
		}
		else {
			return em.unwrap( type );
		}
	}

	@Override
	public Object getDelegate() {
		return em.getDelegate();
	}

	@Override
	public void close() {
		em.close();
	}

	@Override
	public boolean isOpen() {
		return em.isOpen();
	}

	@Override
	public EntityTransaction getTransaction() {
		return em.getTransaction();
	}

	@Override
	public EntityManagerFactory getEntityManagerFactory() {
		return em.getEntityManagerFactory();
	}

	@Override
	public CriteriaBuilder getCriteriaBuilder() {
		return em.getCriteriaBuilder();
	}

	@Override
	public Metamodel getMetamodel() {
		return em.getMetamodel();
	}

	@Override
	public MassIndexer createIndexer(Class... types) {
		return getFullTextSession().createIndexer( types );
	}

	@Override
	public Query createQuery(CriteriaUpdate updateQuery) {
		return em.createQuery( updateQuery );
	}

	@Override
	public Query createQuery(CriteriaDelete deleteQuery) {
		return em.createQuery( deleteQuery );
	}

	@Override
	public StoredProcedureQuery createNamedStoredProcedureQuery(String name) {
		return em.createNamedStoredProcedureQuery( name );
	}

	@Override
	public StoredProcedureQuery createStoredProcedureQuery(String procedureName) {
		return em.createStoredProcedureQuery( procedureName );
	}

	@Override
	public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses) {
		return em.createStoredProcedureQuery( procedureName, resultClasses );
	}

	@Override
	public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) {
		return em.createStoredProcedureQuery( procedureName, resultSetMappings );
	}

	@Override
	public boolean isJoinedToTransaction() {
		return em.isJoinedToTransaction();
	}

	@Override
	public  EntityGraph createEntityGraph(Class rootType) {
		return em.createEntityGraph( rootType );
	}

	@Override
	public EntityGraph createEntityGraph(String graphName) {
		return em.createEntityGraph( graphName );
	}

	@Override
	public EntityGraph getEntityGraph(String graphName) {
		return em.getEntityGraph( graphName );
	}

	@Override
	public  List> getEntityGraphs(Class entityClass) {
		return em.getEntityGraphs( entityClass );
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy