HibernateJpaDialect exposes underlying Session for underlying SessionFactory
This commit is contained in:
parent
0ff83606df
commit
37c601c8dd
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2009 the original author or authors.
|
* Copyright 2002-2011 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -280,7 +280,11 @@ public abstract class SessionFactoryUtils {
|
||||||
|
|
||||||
Assert.notNull(sessionFactory, "No SessionFactory specified");
|
Assert.notNull(sessionFactory, "No SessionFactory specified");
|
||||||
|
|
||||||
SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
|
Object resource = TransactionSynchronizationManager.getResource(sessionFactory);
|
||||||
|
if (resource instanceof Session) {
|
||||||
|
return (Session) resource;
|
||||||
|
}
|
||||||
|
SessionHolder sessionHolder = (SessionHolder) resource;
|
||||||
if (sessionHolder != null && !sessionHolder.isEmpty()) {
|
if (sessionHolder != null && !sessionHolder.isEmpty()) {
|
||||||
// pre-bound Hibernate Session
|
// pre-bound Hibernate Session
|
||||||
Session session = null;
|
Session session = null;
|
||||||
|
|
|
||||||
|
|
@ -20,12 +20,15 @@ import java.lang.reflect.Method;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
import javax.persistence.PersistenceException;
|
import javax.persistence.PersistenceException;
|
||||||
|
|
||||||
import org.hibernate.FlushMode;
|
import org.hibernate.FlushMode;
|
||||||
import org.hibernate.HibernateException;
|
import org.hibernate.HibernateException;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.ejb.HibernateEntityManager;
|
import org.hibernate.ejb.HibernateEntityManager;
|
||||||
|
import org.hibernate.ejb.HibernateEntityManagerFactory;
|
||||||
|
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.jdbc.datasource.ConnectionHandle;
|
import org.springframework.jdbc.datasource.ConnectionHandle;
|
||||||
|
|
@ -35,6 +38,7 @@ import org.springframework.orm.jpa.DefaultJpaDialect;
|
||||||
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
|
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
|
||||||
import org.springframework.transaction.TransactionDefinition;
|
import org.springframework.transaction.TransactionDefinition;
|
||||||
import org.springframework.transaction.TransactionException;
|
import org.springframework.transaction.TransactionException;
|
||||||
|
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -79,12 +83,17 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
|
||||||
previousFlushMode = flushMode;
|
previousFlushMode = flushMode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
EntityManagerFactory emf = entityManager.getEntityManagerFactory();
|
||||||
|
if (emf instanceof HibernateEntityManagerFactory) {
|
||||||
|
SessionFactory sf = ((HibernateEntityManagerFactory) emf).getSessionFactory();
|
||||||
|
TransactionSynchronizationManager.bindResource(sf, session);
|
||||||
|
}
|
||||||
return new SessionTransactionData(session, previousFlushMode);
|
return new SessionTransactionData(session, previousFlushMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanupTransaction(Object transactionData) {
|
public void cleanupTransaction(Object transactionData) {
|
||||||
((SessionTransactionData) transactionData).resetFlushMode();
|
((SessionTransactionData) transactionData).cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -134,7 +143,8 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
|
||||||
this.previousFlushMode = previousFlushMode;
|
this.previousFlushMode = previousFlushMode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void resetFlushMode() {
|
public void cleanup() {
|
||||||
|
TransactionSynchronizationManager.unbindResource(this.session.getSessionFactory());
|
||||||
if (this.previousFlushMode != null) {
|
if (this.previousFlushMode != null) {
|
||||||
this.session.setFlushMode(this.previousFlushMode);
|
this.session.setFlushMode(this.previousFlushMode);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2002-2011 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 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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple <code>FactoryBean</code> 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> {
|
||||||
|
|
||||||
|
public SessionFactory getObject() {
|
||||||
|
EntityManagerFactory emf = getEntityManagerFactory();
|
||||||
|
Assert.isInstanceOf(HibernateEntityManagerFactory.class, emf);
|
||||||
|
return ((HibernateEntityManagerFactory) emf).getSessionFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<?> getObjectType() {
|
||||||
|
return SessionFactory.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue