java - How to use lazy loading in Spring MVC -


how use lazy loading in spring mvc? i'm using eager @ moment, makes app works slowler. part of domain:

@manytomany(fetch = fetchtype.eager) @jointable(name = "news_tag", joincolumns = @joincolumn(name = "news_id"), inversejoincolumns = @joincolumn(name = "tag_id")) private list<tags> tags = new arraylist<tags>();  public list<tags> gettags() {     return this.tags; } 

and dao:

public list<news> getsomenews(long b, long hm) {      list<news> news = (list<news>) sessionfactory             .getcurrentsession()             .createquery(                     "from news title!='about' order publish_time")             .setmaxresults((int) hm).setfirstresult((int) b).list();     return news; } 

servlet-context:

    <context:annotation-config />      <context:component-scan base-package="net.babobka.blog" />        <import resource="../../db/db-config.xml" />      <bean id="urlforwardcontroller"         class="org.springframework.web.servlet.mvc.urlfilenameviewcontroller" />      <bean id="tilesconfigurer"         class="org.springframework.web.servlet.view.tiles2.tilesconfigurer">         <property name="definitions">             <list>                 <value>/web-inf/tiles.xml</value>             </list>         </property>     </bean>      <bean id="viewresolver"         class="org.springframework.web.servlet.view.urlbasedviewresolver">         <property name="viewclass"             value="org.springframework.web.servlet.view.tiles2.tilesview" />     </bean>  </beans> 

db-config:

<bean id="propertyconfigurer"         class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"         p:location="/web-inf/db/jdbc.properties" />      <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"         destroy-method="close" p:driverclassname="${jdbc.driverclassname}"         p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />      <bean id="sessionfactory"         class="org.springframework.orm.hibernate3.localsessionfactorybean">         <property name="datasource" ref="datasource" />         <property name="configlocation">             <value>/web-inf/db/hibernate.cfg.xml</value>         </property>         <property name="configurationclass">             <value>org.hibernate.cfg.annotationconfiguration</value>         </property>         <property name="hibernateproperties">             <props>                 <prop key="hibernate.dialect">${jdbc.dialect}</prop>                 <prop key="hibernate.show_sql">${jdbc.show_sql}</prop>             </props>         </property>     </bean>      <bean id="transactionmanager"         class="org.springframework.orm.hibernate3.hibernatetransactionmanager">         <property name="sessionfactory" ref="sessionfactory" />     </bean>      <tx:annotation-driven />  </beans> 

web.xml:

<?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"     xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">      <!-- definition of root spring container shared servlets          , filters -->     <context-param>         <param-name>contextconfiglocation</param-name>          <param-value>         /web-inf/spring/root-context.xml         /web-inf/spring/application-security.xml         </param-value>     </context-param>        <!-- creates spring container shared servlets , filters -->     <listener>         <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>     </listener>        <!-- processes application requests -->     <servlet>         <servlet-name>appservlet</servlet-name>         <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>         <init-param>             <param-name>contextconfiglocation</param-name>             <param-value>/web-inf/spring/appservlet/servlet-context.xml</param-value>         </init-param>         <load-on-startup>1</load-on-startup>     </servlet>      <servlet-mapping>         <servlet-name>appservlet</servlet-name>         <url-pattern>/</url-pattern>     </servlet-mapping>      <servlet-mapping>         <servlet-name>appservlet</servlet-name>         <url-pattern>*.gif</url-pattern>     </servlet-mapping>     <filter>         <filter-name>springsecurityfilterchain</filter-name>         <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class>     </filter>      <filter-mapping>         <filter-name>springsecurityfilterchain</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>      <filter>         <filter-name>hibernatefilterchain</filter-name>         <filter-class>org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class>     </filter>     <filter-mapping>         <filter-name>hibernatefilterchain</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>     </web-app> 

what have solve problem?

you can use opensessioninviewfilter prevent hibernate session closed. add web.xml:

  <filter>     <filter-name>hibernatefilterchain</filter-name>     <filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class>   </filter>   <filter-mapping>     <filter-name>hibernatefilterchain</filter-name>     <url-pattern>/*</url-pattern>   </filter-mapping> 

@see read more here: opensessioninviewfilter

and remove fetch = fetchtype.eager. @manytomany lazy default.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -