Ok. I did some more coding to understand the concept and found the following
This are my Event, Attendee, Location,Speaker classes.
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;
}
public class Attendee {
private Long id;
private String firstName;
private String lastName;
}
public class Speaker {
private Long id;
private String firstName;
private String lastName;
}
public class Location {
private Long id;
private String name;
private String address;
}
These are my config files.
Code:
Event.hbm.xml
<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>
Now if i try to do the following
Case1: Instantiating only Event and not associated object(Attendees, Speaker)
Code:
Event event = new Event();
e.setName("testSave");
session.saveOrUpdate(event);
i get the following output sql.
Code:
insert into GC820NODE3.events (name, start_date, duration, location_id, id) values (?, ?, ?, ?, ?)
But if i perform the following operation
Case2: Instantiating Event and associated object(Attendees, Speaker) as shown below
Code:
e.setName("testSaveWithCascades");
e.setSpeakers(new HashSet());
e.getSpeakers().add(new Speaker("John", "Doe"));
e.getSpeakers().add(new Speaker("Jane", "Doe"));
e.setAttendees(new HashSet());
e.getAttendees().add(new Attendee("John", "Smith"));
e.getAttendees().add(new Attendee("Jane", "Smith"));
Long id = manager.save(e);
i get the following output sql.
Code:
Hibernate: insert into GC820NODE3.events (name, start_date, duration, location_id, id) values (?, ?, ?, ?, ?)
Hibernate: insert into GC820NODE3.speakers (firstName, lastName, id) values (?, ?, ?)
Hibernate: insert into GC820NODE3.speakers (firstName, lastName, id) values (?, ?, ?)
Hibernate: insert into GC820NODE3.attendees (firstName, lastName, id) values (?, ?, ?)
Hibernate: insert into GC820NODE3.attendees (firstName, lastName, id) values (?, ?, ?)
Hibernate: update GC820NODE3.speakers set event_id=? where id=?
Hibernate: update GC820NODE3.speakers set event_id=? where id=?
Hibernate: update GC820NODE3.attendees set event_id=? where id=?
Hibernate: update GC820NODE3.attendees set event_id=? where id=?
Conclusion:
Does that mean that when i save only Event(Case1), only inserts is done.
If i save Event, which has associated speaker and attenedes(Case2)
first Event is inserted, then the speakers and attendes are inserted and finally the event_id are set in speakers and attendees.
Why does hibernate perform inserts and update, when the entire operation could have been done using inserts. Is there any advantage of doing so?