即使我使用的是openSessionInViewInterceptor,我也有LazyInitializationException的问题。我已经读了很多关于该主题的文章,并且尝试了三种或四种不同的方法来解决这个问题。
首先,我不想在Hibernate配置文件中将false属性设置为false。所以,我想要一个实际的解决方案。我正在使用Spring 2.5,Hibernate 3,Netbeans和Tomcat。
我的实现如下:
servlet.xml
<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>
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;
}
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;
}
}
我认为这些是重要文件。我已经尝试了很多东西,并且总是收到LazyInitializationException或
org.hibernate.HibernateException:没有Hibernate session 绑定(bind)到线程,并且配置不允许在此处创建非事务性 session
...
我不知道哪一个最坏。
提前致谢!
最佳答案
我认为您需要 web.xml 级别的过滤器:
<filter>
<filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
只有这样,spring才能知道何时渲染 View 。