1 Reply Latest reply on Feb 18, 2007 6:44 PM by Dimitri Poppe

    failed to lazily initialize a collection of role: mall.entit

    jojo Newbie

      hi

      I have two entity Beans

      @Entity
      @Table(name = "items")
      public class Item implements Serializable {
      ...
      @ManyToOne
      @JoinColumn(name = "CATEGORY_ID")
      public Category getCategory() {
      return category;
      }

      public void setCategory(Category category) {
      this.category = category;
      }
      }

      -------------------------
      @Entity
      @Table(name = "categories")
      public class Category implements Serializable {
      private Collection items;
      ...
      @OneToMany(mappedBy="category",cascade=CascadeType.ALL)
      public Collection getItems() {
      return items;
      }

      public void setItems(Collection items) {
      this.items = items;
      }
      }
      -----------------------------------------
      in ItemAdminBean.java

      @Stateless
      public class ItemAdminBean implements ItemAdminInterface {
      @PersistenceContext
      protected EntityManager em;
      ...
      public Collection getItemsbyCategory(String catName) {
      Category category = (Category) em.createQuery("from Category c WHERE c.name LIKE :catName").setParameter("catName",
      catName).getSingleResult();
      return category.getItems();
      }

      }


      when i call getItemsbyCategory in jsp page i have a problem


      <% Collection items = itemAdmin.getItemsbyCategory(request.getParameter("categoryChoice"));

      %>

      <%for (Iterator iter = items.iterator(); iter.hasNext();) {
      Item item = (Item) iter.next();

      %>


      org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: mall.entityDto.Category.items, no session or session was closed
      org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
      org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
      org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
      org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
      org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:246)
      org.apache.jsp.online_005fmall.Products_jsp._jspService(Products_jsp.java:111)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


      note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

      can you help me?