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;
020
021import javax.persistence.EntityManagerFactory;
022
023import org.hibernate.SessionFactory;
024
025import org.springframework.beans.factory.FactoryBean;
026import org.springframework.lang.Nullable;
027import org.springframework.orm.jpa.EntityManagerFactoryAccessor;
028import org.springframework.util.Assert;
029import org.springframework.util.ReflectionUtils;
030
031/**
032 * Simple {@code FactoryBean} that exposes the underlying {@link SessionFactory}
033 * behind a Hibernate-backed JPA {@link EntityManagerFactory}.
034 *
035 * <p>Primarily available for resolving a SessionFactory by JPA persistence unit name
036 * via the {@link #setPersistenceUnitName "persistenceUnitName"} bean property.
037 *
038 * <p>Note that, for straightforward cases, you could also simply declare a factory method:
039 *
040 * <pre class="code">
041 * &lt;bean id="sessionFactory" factory-bean="entityManagerFactory" factory-method="getSessionFactory"/&gt;
042 * </pre>
043 *
044 * <p>And as of JPA 2.1, {@link EntityManagerFactory#unwrap} provides a nice approach as well,
045 * in particular within configuration class arrangements:
046 *
047 * <pre class="code">
048 * &#064;Bean
049 * public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
050 *     return emf.unwrap(SessionFactory.class);
051 * }
052 * </pre>
053 *
054 * Please note: Since Hibernate 5.2 changed its {@code SessionFactory} interface to extend JPA's
055 * {@code EntityManagerFactory}, you may get conflicts when injecting by type, with both the
056 * original factory and your custom {@code SessionFactory} matching {@code EntityManagerFactory}.
057 * An explicit qualifier for the original factory (as indicated above) is recommended here.
058 *
059 * @author Juergen Hoeller
060 * @since 3.1
061 * @see #setPersistenceUnitName
062 * @see #setEntityManagerFactory
063 * @deprecated as of Spring Framework 4.3.12 against Hibernate 5.2, in favor of a custom solution
064 * based on {@link EntityManagerFactory#unwrap} with explicit qualifiers and/or primary markers
065 */
066@Deprecated
067public class HibernateJpaSessionFactoryBean extends EntityManagerFactoryAccessor implements FactoryBean<SessionFactory> {
068
069        @Override
070        @Nullable
071        public SessionFactory getObject() {
072                EntityManagerFactory emf = getEntityManagerFactory();
073                Assert.state(emf != null, "EntityManagerFactory must not be null");
074                try {
075                        Method getSessionFactory = emf.getClass().getMethod("getSessionFactory");
076                        return (SessionFactory) ReflectionUtils.invokeMethod(getSessionFactory, emf);
077                }
078                catch (NoSuchMethodException ex) {
079                        throw new IllegalStateException("No compatible Hibernate EntityManagerFactory found: " + ex);
080                }
081        }
082
083        @Override
084        public Class<?> getObjectType() {
085                return SessionFactory.class;
086        }
087
088        @Override
089        public boolean isSingleton() {
090                return true;
091        }
092
093}