-->
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.  [ 5 posts ] 
Author Message
 Post subject: why 2 update in database?
PostPosted: Thu Oct 14, 2004 5:31 pm 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
Here is my java code:

Item item = new Item();
item.setName("Odissea nello spazio");

Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
session.saveOrUpdate(item);
item.setDescription("libro");
tx.commit();


This code is resolved in one SQL INSERT and one SQL UPDATE, instead i expected only 1 INSERT.
Here is the log of the sql:


[main] Hibernate: insert into ITEMS (name, description) values (?, ?)
[main] Hibernate: update ITEMS set name=?, description=? where item_id=?

Do you think i missed some configuration or approach, or is this what hibernate is expected to do?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 14, 2004 6:11 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Set the description before calling saveOrUpdate?

I would assume that once the item gets into the session then anything you change will be updated during the flush (a la the commit).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 4:18 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
For Example

update(item);
item.setName("balh");
item.setDescirption("blah blah");

will produce only 1 update...i don't understand why mixing insert and update then, it will produce 2 sql command.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 11:09 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I don't really have the time to invest trolling through the source code in depth but in 3.0 at least it would appear that inserts/deletes and updates are maintained in separate collections to be processed.

So I would assume that calling save and then updating the object would provide 2 entries, one for the insert and one for the update. It looks like updates used to be kept in a Hash (or so the comment in the SessionImpl says) so that multiple 'updates' would be collected and executed once. Now in 3, it's an ArrayList but I'm not quite sure of all the semantics.

Anyone that has a clue can jump in at any time ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 11:10 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
mapping files please

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.