aspose file tools*
The moose likes Object Relational Mapping and the fly likes update while doing save in hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "update while doing save in hibernate" Watch "update while doing save in hibernate" New topic
Author

update while doing save in hibernate

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I am trying to perform save operation in hibernate3.

I have the following java class



The corresponding config file

<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


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


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





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.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Its doing an insert because your location does not have an id.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
All of this(insert and update) could have been done in single operation(insert), instead of first doing insert and then update. What is the advantage of performing 2 operations.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

None. Are you sure this is the only operation you perform against this object in the current transaction?
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Ok. I did some more coding to understand the concept and found the following

This are my Event, Attendee, Location,Speaker classes.



These are my config files.



Now if i try to do the following

Case1: Instantiating only Event and not associated object(Attendees, Speaker)



i get the following output sql.



But if i perform the following operation

Case2: Instantiating Event and associated object(Attendees, Speaker) as shown below




i get the following output sql.



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?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Can you post your table structure?


If you are not laughing at yourself, then you just didn't get the joke.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I am using the following tables, constraints and views while executing the above program.

Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Can you try adding ownership at the events hbm file by specifying,

<set name="speakers" inverse="true">
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
If i add the above line i get the expected output


Can you please explain why it is so?
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

As I said, it displays ownership of the items and it reflects on cascade of saves. More on hibernate documentation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: update while doing save in hibernate