Java 使用hibernate4.1和Spring3.1获取LazyInitializationException异常
我正在使用OpenSessionInViewFilter。这是我的web.xml中的第一个过滤器Java 使用hibernate4.1和Spring3.1获取LazyInitializationException异常,java,spring,hibernate,Java,Spring,Hibernate,我正在使用OpenSessionInViewFilter。这是我的web.xml中的第一个过滤器 <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我无法理解为什么即使在使用
OpenSessionInViewFilter
时也没有会话。需要这方面的帮助。您是否有可能在单独的线程中调用getUserCoachingRoles
?如果是这样,这是预期的行为
由于UserCoachingRoles
没有被急切地加载,因此Spring无法访问该线程内的数据库连接,也无法加载它。您需要:
- 在线程中创建一个封闭的会话包装器李>
- 将
的id传递给线程,而不是传递对象,并在此线程内加载属性UserCoachingRoles
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userBc" class="com.tutorial.bc.auth.UserBcImpl">
<property name="userDao">
<ref local="userDao"/>
</property>
</bean>
...
<tx:annotation-driven transaction-manager="txManager"/>
<set name="userCoachingRoles" table="user_coaching_role" lazy="true" cascade="all-delete-orphan">
<key column="user_id"/>
<many-to-many column="coaching_id" class="com.tutorial.entity.coaching.Coaching" lazy="proxy"/>
</set>
org.hibernate.LazyInitializationException: could not initialize proxy - no Session