Hibernate LazyInitializationException即使openSessionInViewInterceptor
即使我使用的是openSessionInViewInterceptor,LazyInitializationException也有问题。我读过很多关于这个话题的帖子,我试过三四种不同的方法 首先,我不想在Hibernate配置文件中将lazzy属性设置为false。所以,我想要一个实际的解决方案。我正在使用Spring2.5、Hibernate3、Netbeans和Tomcat 我的执行情况如下: servlet.xmlHibernate LazyInitializationException即使openSessionInViewInterceptor,hibernate,spring,lazy-loading,Hibernate,Spring,Lazy Loading,即使我使用的是openSessionInViewInterceptor,LazyInitializationException也有问题。我读过很多关于这个话题的帖子,我试过三四种不同的方法 首先,我不想在Hibernate配置文件中将lazzy属性设置为false。所以,我想要一个实际的解决方案。我正在使用Spring2.5、Hibernate3、Netbeans和Tomcat 我的执行情况如下: servlet.xml <bean id="urlMapping" class="org.s
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
<property name="mappings">
<props>
<prop key="/index.htm">indexController</prop>
</props>
</property>
</bean>
<bean id ="openSessionInViewInterceptor" name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
索引控制器
applicationContext.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>TasquesDAOHibernate/Model/Tasca.hbm.xml</value>
<value>TasquesDAOHibernate/Model/TipusTasca.hbm.xml</value>
<value>TasquesDAOHibernate/Model/Prioritat.hbm.xml</value>
<value>TasquesDAOHibernate/Model/Persona.hbm.xml</value>
<value>TasquesDAOHibernate/Model/EstatTasca.hbm.xml</value>
<value>TasquesDAOHibernate/Model/Usuari.hbm.xml</value>
<value>TasquesDAOHibernate/Model/LogActivitat.hbm.xml</value>
<value>TasquesDAOHibernate/Model/ObjecteSIPANUsuari.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="tasquesDAO" class="TasquesDAOHibernate.TasquesDAOHibernate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="tasquesService" name="tasquesService" class="Tasques_www.service.TasquesService" >
<property name="tasquesDAO">
<ref local="tasquesDAO"/>
</property>
<property name="transactionManager" ref="transactionManager"/>
</bean>
TasquesDAOHibernate/Model/Tasca.hbm.xml
TasquesDAOHibernate/Model/TipusTasca.hbm.xml
TasquesDAOHibernate/Model/Prioritat.hbm.xml
TasquesDAOHibernate/Model/Persona.hbm.xml
TasquesDAOHibernate/Model/EstatTasca.hbm.xml
TasquesDAOHibernate/Model/Usuari.hbm.xml
TasquesDAOHibernate/Model/LogActivitat.hbm.xml
TasquesDAOHibernate/Model/ObjecteSIPANUsuari.hbm.xml
${hibernate.dial}
0
TasquesService.java
public List<Tasca> getTasques() {
List<Tasca> tasques = (List)this.transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
Object tasques = tasquesDAO.getTasques();
return tasques;
}
});
return tasques;
}
公共列表getTasques(){
List tasques=(List)this.transactionTemplate.execute(new TransactionCallback()){
公共对象doInTransaction(TransactionStatus状态){
对象tasques=tasquesDAO.getTasques();
返回塔斯克;
}
});
返回塔斯克;
}
TasquesDAOHibernate.java
public List<Tasca> getTasques() {
Session session = this.sessionFactory.getCurrentSession();
try{
Query query = session.createQuery("SELECT element FROM Tasca AS element");
List result = query.list();
return result;
}catch(HibernateException ex){
return null;
}
}
公共列表getTasques(){
会话会话=this.sessionFactory.getCurrentSession();
试一试{
Query=session.createQuery(“从Tasca中选择元素作为元素”);
列表结果=query.List();
返回结果;
}捕获(HibernateeException例外){
返回null;
}
}
我认为这些是重要的文件。我已经尝试了很多东西,但我总是得到懒散的初始化或异常
hibernate.HibernateException:没有绑定到线程的hibernate会话,并且配置不允许在此创建非事务会话
我不知道哪一个最差
提前谢谢 我认为您需要在web.xml级别设置一个过滤器:
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
Spring OpenEntityManager视图过滤器
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
只有这样,spring才能知道您的视图何时被呈现。您可以尝试使用spring自带的拦截器查看我的
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="txAttributeSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props> <!-- this catches every method with the interceptor-->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="txInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref local="txAttributeSource" />
</property>
</bean>
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<idref local="txInterceptor" />
</list>
</property>
<property name="beanNames">
<list> <!--this proxies every bean with the especified pattern -->
<value>*BL</value>
</list>
</property>
</bean>
需要进行传播
*基本法
要在整个请求过程中保持会话打开,您需要将Spring的OpenSessionInViewFilter添加到web.xml中。这是特定于hibernate3的:
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
OpenSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
问题在于您对事务管理器的使用:这将启动一个新会话,由于您手动打开了它,它也将关闭它。您需要使用Springs配置配置事务管理,以便所有组件都能正常工作
在您的业务层(TasqueService)上使用事务拦截器。查看indexController的bean定义和源代码也会很有帮助。假设控制器正在转发到视图,那么拦截器应该具有与过滤器相同的效果。我相信在某个spring版本中,拦截器存在错误,因此,过滤器是首选。但不确定。这将如何解决问题?