-->
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: update while doing save in hibernate
PostPosted: Tue Oct 19, 2010 7:20 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
I am trying to perform save operation in hibernate3.

I have the following java class

Code:
  public class Location {

    private Long id;
    private String name;
    private String address;


The corresponding config file
Quote:

<hibernate-mapping package="com.entity.association">
<class name="Location" table="ora_locations">
<id name="id" type="long">
<generator class="native"/>
</id>
<property name="name" type="string"/>
<property name="address" type="string"/>
</class>
</hibernate-mapping>


Code for saving the object

Quote:

Location l = new Location();
l.setName("name");
session.save(l);


When i execute the above code, i get the following output in sql.

Code:
Hibernate: insert into GC820NODE3.ora_locations (name, address, id) values (?, ?, ?)
Hibernate: update GC820NODE3.ora_locations set name=?, address=? where id=?
   




I wanted to understand that what is the need for first doing insert and then performing update. I re-executed the code with using the assigned generator, but i get the same SQLs.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: update while doing save in hibernate
PostPosted: Wed Oct 20, 2010 5:34 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
My guess would be that this is how your native ID generator working. By inserting an empty record into table.


Top
 Profile  
 
 Post subject: Re: update while doing save in hibernate
PostPosted: Thu Oct 21, 2010 1:38 am 
Beginner
Beginner

Joined: Thu Nov 12, 2009 1:57 am
Posts: 22
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?


Top
 Profile  
 
 Post subject: Re: update while doing save in hibernate
PostPosted: Thu Oct 21, 2010 2:02 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Why does hibernate perform inserts and update, when the entire operation could have been done using inserts. Is there any advantage of doing so?


This is explained in the Parent/Child example in the Hibernate documentation:
http://docs.jboss.org/hibernate/stable/ ... child.html

The document also explains how to get rid of it by making the associations bi-directional.


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.