/* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.orm.jpa.vendor; import java.lang.reflect.Method; import javax.persistence.EntityManagerFactory; import org.hibernate.SessionFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.orm.jpa.EntityManagerFactoryAccessor; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** * Simple {@code FactoryBean} that exposes the underlying {@link SessionFactory} * behind a Hibernate-backed JPA {@link EntityManagerFactory}. * * <p>Primarily available for resolving a SessionFactory by JPA persistence unit name * via the {@link #setPersistenceUnitName "persistenceUnitName"} bean property. * * @author Juergen Hoeller * @since 3.1 * @see #setPersistenceUnitName * @see #setEntityManagerFactory */ public class HibernateJpaSessionFactoryBean extends EntityManagerFactoryAccessor implements FactoryBean<SessionFactory> { @Override public SessionFactory getObject() { EntityManagerFactory emf = getEntityManagerFactory(); Assert.state(emf != null, "EntityManagerFactory must not be null"); try { Method getSessionFactory = emf.getClass().getMethod("getSessionFactory"); return (SessionFactory) ReflectionUtils.invokeMethod(getSessionFactory, emf); } catch (NoSuchMethodException ex) { throw new IllegalStateException("No compatible Hibernate EntityManagerFactory found: " + ex); } } @Override public Class<?> getObjectType() { return SessionFactory.class; } @Override public boolean isSingleton() { return true; } }