-->
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.  [ 2 posts ] 
Author Message
 Post subject: Lazy loading not working as expected
PostPosted: Tue Oct 26, 2010 4:14 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
I am making use of hibernate3 and having the following code

Code:
public class Event {
   private Long id;
   private String name;
   private Date startDate;
   private int duration;
   private Set speakers;
   private Set attendees;
   private Location location;
}

<hibernate-mapping package="com.manning.hq.ch03">
    <class name="Event" table="events">
        <id name="id" type="long" unsaved-value="null">
            <generator class="native"/>
        </id>
        <property name="name" type="string" length="100"/>
        <property name="startDate" column="start_date"
                  type="date"/>
        <property name="duration" type="integer"/>
        <many-to-one name="location" column="location_id"
                     class="Location"/>
        <set name="speakers" cascade="all" >
            <key column="event_id"/>
            <one-to-many class="Speaker"/>
        </set>
        <set name="attendees" cascade="all" >
            <key column="event_id"/>
            <one-to-many class="Attendee"/>
        </set>
    </class>
</hibernate-mapping>

public class Location {
    private Long id;
    private String name;
    private String address;
}

<hibernate-mapping package="com.manning.hq.ch03">
    <class name="Location" table="locations">
        <id name="id" type="long">
            <generator class="native"/>
        </id>
        <property name="name" type="string"/>
        <property name="address" type="string"/>
    </class>
</hibernate-mapping>

public class Attendee {
    private Long id;
    private String firstName;
    private String lastName;
}

<hibernate-mapping package="com.manning.hq.ch03">
    <class name="Attendee" table="attendees">
        <id name="id" type="long">
            <generator class="native"/>
        </id>
        <property name="firstName" type="string" length="20"/>
        <property name="lastName" type="string" length="20"/>
    </class>
</hibernate-mapping>


Hibernate3 has lazy as true by default. I am executing the following code.

Code:
event = (Event) session.load(Event.class, id);
System.out.println(event.getId());
System.out.println(event.getName());


When the line

Code:
System.out.println(event.getName());


is executed, I get the following sql in my console

Code:
SELECT event0_.ID AS id8_0_, event0_.NAME AS name8_0_,
       event0_.start_date AS start3_8_0_, event0_.DURATION AS duration8_0_,
       event0_.location_id AS location5_8_0_
  FROM gc820node3.EVENTS event0_
WHERE event0_.ID =


SELECT attendees0_.event_id AS event4_1_, attendees0_.ID AS id1_,
       attendees0_.ID AS id9_0_, attendees0_.firstname AS firstname9_0_,
       attendees0_.lastname AS lastname9_0_
  FROM gc820node3.attendees attendees0_
WHERE attendees0_.event_id = ?


SELECT speakers0_.event_id AS event4_1_, speakers0_.ID AS id1_,
       speakers0_.ID AS id10_0_, speakers0_.firstname AS firstname10_0_,
       speakers0_.lastname AS lastname10_0_
  FROM gc820node3.speakers speakers0_
WHERE speakers0_.event_id = ?


The first SQL is fine, but in case of 2nd query, the attendees is being fetched when the event method is called.
Accoring the lazy loading, it should not load the object when event is being accessed. Why is 2nd and 3rd SQL fetching the associated objects


Top
 Profile  
 
 Post subject: Re: Lazy loading not working as expected
PostPosted: Tue Oct 26, 2010 5:04 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
I figured out the problem. My config files were not using hibernate3, since it had wrong entry.

Old config file entry
Code:
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">


New config file entry

Code:
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


On making use of new config file entry, the lazy loading works as expected.


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