Hibernate version: 3.2.6.ga
Spring version: 2.5.5
This is for standalone application, and I know EXACTLY where the transaction starts and ends. I don't have a problem with reading lazy initialized values outside of transaction (session), I want to read them INSIDE transaction, so please don't send me to read about OpenSessionInView. I want the session to last exactly as long as the transaction lasts.
Files of note:
Spring configuration
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
<property name="dataSource"><ref local="dataSource" /></property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
<property name="url" value="***" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource"><ref local="dataSource" /></property>
<property name="entityManagerFactory"><ref local="entityManagerFactory" /></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="someService" class="***.SomeServiceImpl" />
</beans>
SomeService interface:
Code:
public interface SomeService {
public void doSomethingInTransaction();
}
SomeServiceImpl class:
Code:
@Transactional
public class SomeServiceImpl implements SomeService {
private EntityManager entityManager;
@PersistenceContext(type=PersistenceContextType.TRANSACTION)
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void doSomethingInTransaction() {
// Do Something with entityManager that involves retrieval and
// further read of lazy loaded @Entity beans
}
}
Nature of the problem/Expected result:
Essentially, I want to start the transaction at the beginning of
doSomethingInTransaction(), and end it just after finishing it. The problem is, even though the whole code of the method should run in a transaction I'm getting
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session.
Am I missing something? Do I need to specify somewhere, that the Session object should be bound to the Transaction?