
org.springframework.orm.hibernate.SpringSessionContext Maven / Gradle / Ivy
package org.springframework.orm.hibernate;
import javax.persistence.PersistenceException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.context.spi.CurrentSessionContext;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.springframework.orm.jpa.EntityManagerHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@SuppressWarnings("serial")
public class SpringSessionContext implements CurrentSessionContext {
public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
}
@Override
public Session currentSession() throws HibernateException {
for (Object value : TransactionSynchronizationManager.getResourceMap().values()) {
if (value instanceof EntityManagerHolder) {
try {
return ((EntityManagerHolder) value).getEntityManager().unwrap(Session.class);
}
catch (PersistenceException e) {
throw new HibernateException(e);
}
}
}
throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy