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