001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.orm.jpa.vendor;
018
019import java.lang.reflect.Method;
020import javax.persistence.EntityManagerFactory;
021
022import org.hibernate.SessionFactory;
023
024import org.springframework.beans.factory.FactoryBean;
025import org.springframework.orm.jpa.EntityManagerFactoryAccessor;
026import org.springframework.util.Assert;
027import org.springframework.util.ReflectionUtils;
028
029/**
030 * Simple {@code FactoryBean} that exposes the underlying {@link SessionFactory}
031 * behind a Hibernate-backed JPA {@link EntityManagerFactory}.
032 *
033 * <p>Primarily available for resolving a SessionFactory by JPA persistence unit name
034 * via the {@link #setPersistenceUnitName "persistenceUnitName"} bean property.
035 *
036 * <p>Note that, for straightforward cases, you could also simply declare a factory method:
037 *
038 * <pre class="code">
039 * &lt;bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory"/&gt;
040 * </pre>
041 *
042 * <p>And as of JPA 2.1, {@link EntityManagerFactory#unwrap} provides a nice approach as well,
043 * in particular within configuration class arrangements:
044 *
045 * <pre class="code">
046 * &#064;Bean
047 * public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
048 *     return emf.unwrap(SessionFactory.class);
049 * }
050 * </pre>
051 *
052 * Please note: Since Hibernate 5.2 changed its {@code SessionFactory} interface to extend JPA's
053 * {@code EntityManagerFactory}, you may get conflicts when injecting by type, with both the
054 * original factory and your custom {@code SessionFactory} matching {@code EntityManagerFactory}.
055 * An explicit qualifier for the original factory (as indicated above) is recommended here.
056 *
057 * @author Juergen Hoeller
058 * @since 3.1
059 * @see #setPersistenceUnitName
060 * @see #setEntityManagerFactory
061 * @deprecated as of Spring Framework 4.3.12 against Hibernate 5.2, in favor of a custom solution
062 * based on {@link EntityManagerFactory#unwrap} with explicit qualifiers and/or primary markers
063 */
064@Deprecated
065public class HibernateJpaSessionFactoryBean extends EntityManagerFactoryAccessor implements FactoryBean<SessionFactory> {
066
067        @Override
068        public SessionFactory getObject() {
069                EntityManagerFactory emf = getEntityManagerFactory();
070                Assert.state(emf != null, "EntityManagerFactory must not be null");
071                try {
072                        Method getSessionFactory = emf.getClass().getMethod("getSessionFactory");
073                        return (SessionFactory) ReflectionUtils.invokeMethod(getSessionFactory, emf);
074                }
075                catch (NoSuchMethodException ex) {
076                        throw new IllegalStateException("No compatible Hibernate EntityManagerFactory found: " + ex);
077                }
078        }
079
080        @Override
081        public Class<?> getObjectType() {
082                return SessionFactory.class;
083        }
084
085        @Override
086        public boolean isSingleton() {
087                return true;
088        }
089
090}