openSessionInViewInterceptor を使用しているにもかかわらず、LazyInitializationException に問題があります。そのトピックに関する非常に多くの投稿を読み、3 つまたは 4 つの異なるアプローチを試みました。
まず、Hibernate 構成ファイルの lazzy 属性を false に設定したくないということです。だから、私はその問題に対する実際の解決策を望んでいます。Spring 2.5、Hibernate 3、Netbeans、および Tomcat を使用しています。
私の実装は次のとおりです。
サーブレット.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 セッションがありません。構成では、ここで非トランザクション セッションを作成することはできません ...
どっちが悪いのかわからない。
前もって感謝します!