From bf5739c56b46553d2c376bc4c952880e9bccd987 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 31 Oct 2014 15:07:21 +0100 Subject: [PATCH] HibernateJpaSessionFactoryBean is compatible with Hibernate 4.3 as well now Issue: SPR-12401 --- .../vendor/HibernateJpaSessionFactoryBean.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaSessionFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaSessionFactoryBean.java index 511cd91c586..163d69f890c 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaSessionFactoryBean.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/vendor/HibernateJpaSessionFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * 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. @@ -16,14 +16,15 @@ package org.springframework.orm.jpa.vendor; +import java.lang.reflect.Method; import javax.persistence.EntityManagerFactory; import org.hibernate.SessionFactory; -import org.hibernate.ejb.HibernateEntityManagerFactory; 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} @@ -42,8 +43,14 @@ public class HibernateJpaSessionFactoryBean extends EntityManagerFactoryAccessor @Override public SessionFactory getObject() { EntityManagerFactory emf = getEntityManagerFactory(); - Assert.isInstanceOf(HibernateEntityManagerFactory.class, emf); - return ((HibernateEntityManagerFactory) emf).getSessionFactory(); + 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