I use Spring 1.2.6 along with Hibernate 3.0.5 on WSAD 5.1.2. I want to load an object "A" with many-to-one relationship of "B". I know Hibernate 3 comes with lazy-loading="true" by default, and it always returns me proxies instead of real objects. I tried to use the OpenSessionInViewFilter to solve the problem but failed. Following are my codes:
web.xml
applicationContext.xml (**notice that I use "autodetect")Code:... <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application-context.xml</param-value> </context-param> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <display-name>OpenSessionInViewFilter</display-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ...
SearchServiceImpl.javaCode:<beans default-autowire="autodetect"> ... <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/testDS</value> </property> </bean> <bean id="searchDao" class="test.SearchDaoHibernate"/> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>/WEB-INF/hibernate.cfg.xml</value> </property> </bean> <bean id="searchService" class="test.SearchServiceImpl"/> ...
SearchDaoHibernate.javaCode:public class SearchServiceImpl implements SearchService { private SearchDao searchDao; public Data execute(String id) { return (Data)searchDao.search(new Integer(id)); } /** * @param dao */ public void setSearchDao(SearchDao dao) { searchDao = dao; } }
Data.hbm.xmlCode:public class SearchDaoHibernate extends HibernateDaoSupport implements SearchDao{ public Data search(Integer seqNo) { return (Data)getHibernateTemplate().get(test.Data.class, seqNo); } }
Reason.hbm.xmlCode:<hibernate-mapping> <class name="test.Data" table="m_data"> <id name="id" column="m_data_seq_no"> <generator class="native"/> </id> <property name="mailerName" column="mailer_name" type="string"/> <property name="mailerAddress" column="mailer_address" type="string"/> <many-to-one name="reason" column="m_reasons" class="test.Reason"/> </class> </hibernate-mapping>
Code:<hibernate-mapping> <class name="test.Reason" table="reasons"> <id name="id" column="m_reasons_code" type="integer"> <generator class="native"/> </id> <property name="activityDate" column="activity_date" type="date"/> <property name="description" column="reason_description" type="string"/> </class> </hibernate-mapping>
I could get everything to work if I only loaded Data object by using HibernateTemplate.get() (**notice that even this simple loading wouldn't work if I used HibernateTemplate.load()), but I gotif I want to load Data along with Reason by using <many-to-one> relationship. I got the error on the UI tier when JSP tried to display information of Reason object. Why was this happening since I used OpenSessionInViewFilter? Shouldn't the connection not be closed and still accessible by the JSP tags (such as <bean:write> from Struts) because the response hasn't been sent yet?org.hibernate.LazyInitializationException could not initialize proxy - the owning Session was closed
Also why didn't HibernateTemplate.load() work with this simple scenario?
Thank you in advance.


Reply With Quote