-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate initializes lazy collection during INSERT of child
PostPosted: Thu Oct 30, 2003 7:17 pm 
Newbie

Joined: Fri Aug 29, 2003 2:42 am
Posts: 4
I have a simple one-to-many relationship. When I insert child record for a parent, hibernate initializes the lazy collection for the parent. Even though there is no change in the parent attributes. The lazy collection should not initialize. This is big performance issue as the performance degrades if there are more children for the parent.
Any help to overcome/resolve this issue?

Thanks in advance !!!

Example:
Parent Object = Person
Child Object = Pfolder

Mapping definition:
<!-- One-to-Many Relation -->
<class name="net.sf.hibernate.examples.quickstart.Person" table="PERSON" dynamic-update="true">
<id name="id" type="string" unsaved-value="null" >
<column name="person_ID" sql-type="varchar(255)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="name" type="string"/>
<set name="folders" lazy="true" table="PFOLDER">
<key column="person_id"/>
<one-to-many class="net.sf.hibernate.examples.quickstart.Pfolder"/>
</set>
</class>

<class name="net.sf.hibernate.examples.quickstart.Pfolder" table="PFOLDER" dynamic-update="true">
<id name="id" type="string" unsaved-value="null" >
<column name="folder_ID" sql-type="varchar(255)" not-null="true"/>
<generator class="assigned"/>
</id>

Code:
public void createFolderPerson(PrintWriter out)
        throws HibernateException {
        out.print("<h3>Creating Folder for Person:</h3>");
      try {

         String id = "101";
         String name = null;
         Class cls = Class.forName("net.sf.hibernate.examples.quickstart.Person");

         Person person = (Person) session.load(cls, id);
                     out.println("Creating folder for this person:");

            // new folder
            Pfolder pfld = new Pfolder();
            pfld.setId(new String("111"));
            pfld.setName("folder 111");
            pfld.setParent(person);
            session.save(pfld);
         } catch (Exception e)  {
            e.printStackTrace();
         }
    }

===============================================
Logs:
- net.sf.hibernate.examples.quickstart.SampleServlet - before begin transaction
- net.sf.hibernate.examples.quickstart.SampleServlet - before session.load
Hibernate: select person0_.person_ID as person_ID0_, person0_.name as name0_ from PERSON person0_ where person0_.person_ID=?
- net.sf.hibernate.examples.quickstart.SampleServlet - after session.load
Hibernate: select pfolder0_.folder_ID as folder_ID__, pfolder0_.person_id as person_id__, pfolder0_.folder_ID as folder_ID0_, pfolder0_.person_id as person_id0_, pfolder0_.name as name0_ from PFOLDER pfolder0_ where pfolder0_.person_id=?
Hibernate: select person0_.person_ID as person_ID0_, person0_.name as name0_ from PERSON person0_ where person0_.person_ID=?
- net.sf.hibernate.examples.quickstart.SampleServlet - before session.save
- net.sf.hibernate.examples.quickstart.SampleServlet - after session.save
Hibernate: insert into PFOLDER (person_id, name, folder_ID) values (?, ?, ?)
Hibernate: update PFOLDER set person_id=? where folder_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 7:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/117.html#A8

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 30, 2003 7:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
And focus on chapter 8 Parent-child relationship.

Add
Code:
<many-to-one name="parent"  column="person_id" not-null="true"/>


Code:
<class name="net.sf.hibernate.examples.quickstart.Person" table="PERSON" dynamic-update="true">
<id name="id" type="string" unsaved-value="null" >
<column name="person_ID" sql-type="varchar(255)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="name" type="string"/>
<set name="folders" lazy="true" table="PFOLDER">
<key column="person_id"/>
<one-to-many class="net.sf.hibernate.examples.quickstart.Pfolder"/>
</set>
</class>

<class name="net.sf.hibernate.examples.quickstart.Pfolder" table="PFOLDER" dynamic-update="true">
<id name="id" type="string" unsaved-value="null" >
<column name="folder_ID" sql-type="varchar(255)" not-null="true"/>
<generator class="assigned"/>
</id>
<many-to-one name="parent"  column="person_id" not-null="true"/>

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Resolved the issue with Lazy Initialization
PostPosted: Thu Oct 30, 2003 8:26 pm 
Newbie

Joined: Fri Aug 29, 2003 2:42 am
Posts: 4
Thanks for the help !!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.