Hi.
I have a legacy project where some of the code are using HibernateTemplate. We want to start using JPA with Hibernate as a JPA provider in new parts of the project, while keeping the old parts untouched.
The problem is that when JPATransactionManager is used, the active session is not bound in such a way that HibernateTemplate can locate it.
From HibernateTemplate.getSession():
Only if isAllowedNewSession() and isAllowedCreate() both is false, the HibernateTemplate will use sessionFactory.getCurrentSession() which returns the correct session. If not, a new session is created and this leads to some strange behavior (obviously) which may be a bit hard to debug if new developers enters the project.Code:if (isAlwaysUseNewSession()) { return SessionFactoryUtils.getNewSession(getSessionFactory(), getEntityInterceptor()); } else if (isAllowCreate()) { return SessionFactoryUtils.getSession(getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator()); } else if (SessionFactoryUtils.hasTransactionalSession(getSessionFactory())) { return SessionFactoryUtils.getSession(getSessionFactory(), false); } else { try { return getSessionFactory().getCurrentSession(); } catch (HibernateException ex) { throw new DataAccessResourceFailureException("Could not obtain current Hibernate Session", ex); } }
My question is:
Can I use another TransactionManager that works both for HibernateTemplate and JPA? Or is there another way of configuring things so that both HibernateTemplate and JPA will work.
Or is use of HibernateTemplate simply not supported with JPA?
Best regards


Reply With Quote