-->
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.  [ 15 posts ] 
Author Message
 Post subject: How can I avoid unnecessary DB query before session.update()
PostPosted: Wed Dec 22, 2004 5:29 am 
Newbie

Joined: Wed Dec 22, 2004 5:07 am
Posts: 1
Now I want to update a single column of a DB table by primary key.

If JDBC, only the following sql statment will be ok:
Update tableName
Set columnName = newvalue
Where idName = id

But with hibernate , I have to do the following:

PO po = (PO) session.load(PO.class, id);
po.setSomeAttribute(newValue);
sess.update(po);

Running these statments, Hibernate performs the following two DB operations (even with mapping file of PO set to lazy="true")
first Select from DB
secondly , update to DB.

Compare to JDBC, the first query is unnessary.

Does Hibernate provide any mechanism so that the first unnessary query can be avoided?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 9:23 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It must be possible to optimize this case if "lazy="true", the same is about delete by primary key. You can request feature/bug fix using bug tracker.


Top
 Profile  
 
 Post subject: I have same issue with delete & can't find documentatio
PostPosted: Wed Dec 22, 2004 4:36 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
I have same issue with delete & can't find documentation. If I know the primary key why do I need to do a select to implement the delete?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 5:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
because persistent objects has a lifecycle and need to check for cascade etc.

in H3 (and EJB3) you will get bulk delete/update (which will do stuff efficently on the db, but of course cannot be able to keep the session in a consistent state)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 5:02 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
max wrote:
because persistent objects has a lifecycle and need to check for cascade etc.

in H3 (and EJB3) you will get bulk delete/update (which will do stuff efficently on the db, but of course cannot be able to keep the session in a consistent state)



What does that mean. In hibernate 2.1.6 it is not possible to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 5:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh ? yes - that was what i wrote ,)

H2 does not have bulk delete, H3 will have it + I explained why we load objects before deleting them....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: But if there are no cascades apart from the primary key???
PostPosted: Wed Dec 22, 2004 5:16 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
Can you give us an example of what you would need to check for? Why can't hibernate just delete blindly all sub collections if the mapping is set to cascade. What is the technical reason why this is not possible.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 5:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
read hibernate in action (and think about it again when taking into consideration LifeCycle, Interceptor, session scoped identity and where attributes and other stuff you can map in the mapping files!)

We simply cannot deduce the equal sql deletes and magic java code you might want executed on an objects deletion....

but as i said - you will get the possiblity in H3 and EJB3 (where you get to execute update/delete with HQL/EJBQL but with that natural limitation that deletions will not be reflected into your current session!)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: update / delete without select
PostPosted: Wed Dec 22, 2004 9:03 pm 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
max wrote:
- you will get the possiblity in H3 and EJB3 (where you get to execute update/delete with HQL/EJBQL but with that natural limitation that deletions will not be reflected into your current session!)


Max has got a key point right there. If Hibernate hasn't loaded an object into the session, there's no way for it to do all the lifecycle stuff. Not to mention the fact that your application code might not expect this state. It's a little like the mistake developers often make when using an ORM like Hibernate and then trying to change the database without telling the ORM about it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 2:25 am 
Newbie

Joined: Wed Feb 25, 2004 11:41 pm
Posts: 19
Location: China
hi max..

road map in hibernate.org:

Bulk update and delete

Hibernate 3.1 will introduce support for EJB3-style bulk UPDATE and bulk DELETE queries.

now bulk UPDATE/delete in H3 ? oyeah...glad to get it

_________________
who can tell me why?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 4:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the road map is the correct and precise one - I use H3 as a term for all what will and can come in H3.

so bulk delete/update is targeted for Hibernate 3.1

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 5:21 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It must be possible to implement this optimization without breaking life cycle stuff, it can depend on mapping. If there is no application level triggers then you do not need to load object to update or delete. Boolean parameter in "load" can be used as hint too.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 5:48 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Submit a patch.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 8:25 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
ok


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 23, 2004 9:12 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
I'm not saying that everyone will like it and the patch will be applied :) I'm saying that "must be possible" might be true.


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