| Author |
update while doing save in hibernate
|
Naresh Chaurasia
Ranch Hand
Joined: May 18, 2005
Posts: 309
|
|
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: 309
|
|
|
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: 309
|
|
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: 508
|
|
|
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: 309
|
|
I am using the following tables, constraints and views while executing the above program.
|
 |
Arun Kumarr
Ranch Hand
Joined: May 16, 2005
Posts: 508
|
|
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: 309
|
|
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: 508
|
|
|
As I said, it displays ownership of the items and it reflects on cascade of saves. More on hibernate documentation.
|
 |
 |
|
|
subject: update while doing save in hibernate
|
|
|