Announcement Announcement Module
Collapse
No announcement yet.
"spring+hibernae"--Failed to lazily initialize a collection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • "spring+hibernae"--Failed to lazily initialize a collection

    Hi,all
    I have a question about spring + hibernate:

    org.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
    org.hibernate.collection.PersistentCollection.init ialize(PersistentCollection.java:210)
    org.hibernate.collection.PersistentCollection.read (PersistentCollection.java:72)
    org.hibernate.collection.Set.size(Set.java:104)
    servlet.TestServlet.doPost(TestServlet.java:63)
    servlet.TestServlet.doGet(TestServlet.java:37)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter.doFilterInternal(OpenSessionInVie wFilter.java:174)
    org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)

    can you tel me the way about this question ?can you give me some code about this question!

    thank you very very....much!

  • #2
    Not sure how you are configured, but try the OpenSessionInView. Its all over the forum here, just run a search.

    Good luck,

    Steve O

    Comment


    • #3
      Originally posted by Steve O
      Not sure how you are configured, but try the OpenSessionInView. Its all over the forum here, just run a search.

      Good luck,

      Steve O
      Steve ,thank you reply
      this is my configure file
      web.xml
      <?xml version="1.0" encoding="ISO-8859-1"?>

      <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">

      <web-app>

      <filter>
      <filter-name>openSessionInViewFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>

      </filter>
      <filter-mapping>
      <filter-name>openSessionInViewFilter</filter-name>
      <url-pattern>/*</url-pattern>
      </filter-mapping>


      <servlet>
      <servlet-name>context</servlet-name>
      <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
      </servlet>

      <listener>
      <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
      </listener>

      <filter>
      <filter-name>webinit</filter-name>
      <filter-class>common.ServerFilter</filter-class>
      </filter>
      <filter-mapping>
      <filter-name>webinit</filter-name>
      <url-pattern>*.*</url-pattern>
      </filter-mapping>

      <servlet>
      <servlet-name>myTest</servlet-name>
      <servlet-class>servlet.TestServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
      </servlet>

      <servlet-mapping>
      <servlet-name>myTest</servlet-name>
      <url-pattern>/test</url-pattern>
      </servlet-mapping>


      <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      </web-app>

      applicationContext.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

      <beans>
      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean" singleton="true">
      <property name="configLocation">
      <value>classpath:hibernate.cfg.xml</value>
      </property>
      </bean>

      <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
      <property name="sessionFactory">
      <ref local="sessionFactory" />
      </property>
      </bean>

      <bean id="hibernateDao" class="common.HibernateDao">
      <property name="sessionFactory">
      <ref local="sessionFactory" />
      </property>
      <property name="transactionManager">
      <ref local="transactionManager" />
      </property>
      </bean>


      <bean id="daoManager" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
      <property name="transactionManager">
      <ref bean="transactionManager" />
      </property>


      <property name="target">
      <ref local="hibernateDao" />
      </property>

      <property name="transactionAttributes">
      <props>
      <prop key="insert*">PROPAGATION_REQUIRED</prop>
      <prop key="update*">PROPAGATION_REQUIRED</prop>
      <prop key="delete*">PROPAGATION_REQUIRED</prop>
      <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
      </props>
      </property>
      </bean>

      </beans>

      HibernateDao.java

      package common;
      import java.util.List;
      import org.springframework.orm.hibernate3.HibernateTransa ctionManager;
      import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport;

      public class HibernateDao extends HibernateDaoSupport{
      private HibernateTransactionManager transactionManager;

      public Object insert(Object obj){
      return getHibernateTemplate().save(obj);
      }

      public void update(Object obj){
      getHibernateTemplate().update(obj);
      }

      public void delete(Object obj){
      getHibernateTemplate().delete(obj);
      }

      public List getAllEntityByHql(String hql){
      return getHibernateTemplate().find(hql);
      }

      public Object loadEntityById(Class entityClass,Integer id){
      return getHibernateTemplate().load(entityClass,id);
      }


      public HibernateTransactionManager getTransactionManager() {
      return transactionManager;
      }

      public void setTransactionManager(
      HibernateTransactionManager transactionManager) {
      this.transactionManager = transactionManager;
      }

      }

      TestServlet.java
      /*
      * Created on 2006-1-12
      */
      package servlet;

      import java.io.IOException;
      import java.io.PrintWriter;
      import java.util.List;

      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      import map.UserInfo;

      import common.Hibernate;
      import common.HibernateLazyResolver;
      import common.ServiceProvider;

      public class TestServlet extends HttpServlet{


      private static final long serialVersionUID = 1L;

      protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
      throws ServletException, IOException {
      doPost(arg0,arg1);
      }

      protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
      throws ServletException, IOException {
      List list = Hibernate.getAllEntityByHql("from UserInfo");
      UserInfo uinfo = new UserInfo();
      uinfo.setUser("xurun");
      uinfo.setPassword("xurun");
      uinfo.setUseremail("xurun");
      Hibernate.insert(uinfo);
      PrintWriter out = arg1.getWriter();

      String header = "<html>"+
      "<head>"+
      "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"+
      "<title>{0}</title>"+
      "</head>"+
      "<body>";

      out.print(header);

      String down="</body></html>";

      out.println();

      for(int i=0;i<list.size();i++){
      UserInfo ui = (UserInfo)list.get(i);
      // out.print(ui.getUsername()+"_"+ui.getUseremail()+" <br>");
      out.print(ui.getUsername()+"_"+ui.getApply().size( )+"_"+ui.getUseremail()+"<br>"); // throw exception where I run ui.getApply() , }
      out.print(down);
      }
      }

      can you help me ?

      thank you !!!
      Last edited by kxurun; Jan 12th, 2006, 10:52 PM.

      Comment


      • #4
        <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>

        <listener>
        <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
        </listener>
        You should not use both of them - you have to use only one.

        Comment

        Working...
        X