-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: lazy initiallization of collections
PostPosted: Tue Jul 06, 2004 6:06 am 
Newbie

Joined: Wed Jun 16, 2004 8:26 am
Posts: 19
Hi all,

i have a problem working with lazy initiallization on collections.

I tried to use a proxy in my mapping file (same class) and
i tried to work with Hibernate.initialize(); at different places on my java-files.

Could someone tell me where change my source-code or mapping to get the lazy-thing working?

here my files i'm working with in a struts-web-environment.

Action_detail_isb_view.java
Code:
public class Action_detail_isb_view extends Action {

   public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws IOException, ServletException, HibernateException {

      FormBean_detail_isb insertForm = (FormBean_detail_isb) form;

      Integer idUpdateIsb = new Integer(insertForm.getIdUpdateIsb());
      Bean_Update_ISB detail_isb =
         HibernateQueryUtil.loadUpdate_isb(idUpdateIsb);

...
         
      Integer changeRequest = detail_isb.getChangeRequest();

      Integer testTrack = detail_isb.getTestTrack_Nr();
      Set List_Update = detail_isb.getList_Update();
      
...
      request.setAttribute("listeUpdate", List_Update);

      return (mapping.findForward("success"));
   }
}


HibernateUtil.java --> from the official docs

Code:
   public class HibernateUtil {

      private static final SessionFactory sessionFactory;
      static {
         try {
            sessionFactory =
               new Configuration().configure().buildSessionFactory();
         } catch (HibernateException ex) {
            throw new RuntimeException(
               "Exception building SessionFactory: " + ex.getMessage(),
               ex);
         }
      }

      public static final ThreadLocal session = new ThreadLocal();


      public static Session currentSession() throws HibernateException {
         Session s = (Session) session.get();
         //      Open a new Session, if this Thread has none yet
         if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
         }
         return s;
      }

      public static void closeSession() throws HibernateException {
         Session s = (Session) session.get();
         session.set(null);
         if (s != null)
            s.close();
      }
   }


HibernateQueryUtil --> my own helper-class

Code:
   public static Bean_Update_ISB loadUpdate_isb(Integer id)
      throws IOException, ServletException, HibernateException {

      Session hibernate_session = HibernateUtil.currentSession();
      Transaction tx = hibernate_session.beginTransaction();
      Bean_Update_ISB update_isb = new Bean_Update_ISB();

      try {
         tx = hibernate_session.beginTransaction();

         update_isb =
            (Bean_Update_ISB) hibernate_session.load(
               Bean_Update_ISB.class,
               id);
         tx.commit();

      } catch (Exception e) {
         e.printStackTrace(System.err);
         if (tx != null)
            tx.rollback();
      }
      finally {
         HibernateUtil.closeSession();
      }
      return update_isb;
   }


finally my mapping-file
Code:
<hibernate-mapping>
    <class name="com.tecdoc.db_update.Bean_Update_ISB"
          table="t_Update_ISB">
           
        <id name="id_Update_ISB" column="id_Update_ISB" type="integer">
            <generator class="identity">
            </generator>
        </id>

        <property name="Datum"
                column="Datum"
                type="java.lang.String"/>

        <property name="Bezeichnung"
                column="Bezeichnung"
                type="java.lang.String"/>

        <property name="Pfadangabe_Speicherort"
                column="Pfadangabe_Speicherort"
                type="java.lang.String"/>

        <property name="ChangeRequest"
                column="ChangeRequest"
                type="integer"/>

        <property name="TestTrack_Nr"
                column="TestTrack_Nr"
                type="integer"/>
           
        <set name="List_Update" inverse="true" lazy="true">
           <key column="id_Update_ISB"/>
              <one-to-many class="com.tecdoc.db_update.Bean_Update"/>
          </set>
    </class>
</hibernate-mapping>


I always get
net.sf.hibernate.LazyInitializationException:
Failed to lazily initialize a collection - no session or session was closed

Thanks in advance.

_________________
Have Fun!

Torsten


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 4:58 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
IF I understand your question correctly, while your session is still open, all you have to do is call Hibernate.initialize(update_isb.getList_Update()).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 2:59 am 
Newbie

Joined: Wed Jun 16, 2004 8:26 am
Posts: 19
i think that's what i needed.
simple but i haven't done the initialisation correctly.

it seems to work

thx for your help.

often (especially for beginners) an obvious tip is really important cause not all aspects have a concrete example in the docs.

_________________
Have Fun!

Torsten


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 3:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
but you have everything in Hibernate in Action

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 3:15 am 
Newbie

Joined: Wed Jun 16, 2004 8:26 am
Posts: 19
but this book isn't released yet.

i only have one chapter that's free for download.

_________________
Have Fun!

Torsten


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 3:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you can buy it on meap.... ;))

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 5:37 pm 
Beginner
Beginner

Joined: Fri Mar 26, 2004 8:19 am
Posts: 49
I bought it on meap and it is great. Highly recommended.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.