-->
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.  [ 3 posts ] 
Author Message
 Post subject: the hibernate solution confused me, please explain me this..
PostPosted: Sun May 14, 2006 5:21 pm 
Newbie

Joined: Sun May 14, 2006 5:09 pm
Posts: 11
without hibernate, to update a record, only need to send one update statement to database:
update tablename set col2 = 'abc' where col1 = '123'.
however, with hibernate, I have to load object first,then update:
obj = (Obj)session.load(tableNm.class,123);
obj.setCol2 = "abc";
session.update(obj);

this need 2 statement :
1. select * from tablename where where col1 = '123'.
2. update tablename set col2 = 'abc' where col1 = '123'.

if this is true, then further more, to update multiple record, we need a loop to load and update record one by one. however, with native SQL, on one update setament like:
update tablename set col2 = 'abc' where col1 in (...).
is neccssary.

is hibernate provide a poor solution regarding database performance?
size=18][/size]


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 6:17 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Quote:
without hibernate, to update a record, only need to send one update statement to database:
update tablename set col2 = 'abc' where col1 = '123'.


True, but, in a real applications, often is not that simple (you have to worry about, for example, optimistic concurrency, master-detail relationships, identity generation, business rules constrains, etc).


Quote:
however, with hibernate, I have to load object first,then update:
obj = (Obj)session.load(tableNm.class,123);
obj.setCol2 = "abc";
session.update(obj);


Thanks to that, Hibernate can compare the internal snapshots of your data, and see if there could be conflicting changes, without having to use database locks, that IMHO are not as scalable. Also by working with objects, you can set multiple master-detail relationships without worrying about having to issue the INSERTS and UPDATES in a particular order (Hibernate does that automatically). Also you can configure validation before saving you objects so that the comply with your particular bussines rules.

Quote:
this need 2 statement :
1. select * from tablename where where col1 = '123'.
2. update tablename set col2 = 'abc' where col1 = '123'.

if this is true, then further more, to update multiple record, we need a loop to load and update record one by one. however, with native SQL, on one update setament like:
update tablename set col2 = 'abc' where col1 in (...).
is neccssary.


Exactly, and there are cases when the best solution is to send a direct update to the database (if you need to modify lots of records, and you are not that worried that your modification might break some bussines logic constraint, mess with optimistic concurrency or have master-detail implications).


Quote:
is hibernate provide a poor solution regarding database performance?


It depends on the case, there are not "one solution fits all" in persistency... you have tu analize things in a case by case basis... for example, you will need to think about how the way your user is going to interact with the data affects the SQL you send to the database (and what data you need to keep in memory, in a tipicaly OLTP application, you need to show the data to the user and then allow him to modify it).

In other words, it is not that simple, you would have to provide me with an example of a bussines transaction use case with at least 3 tables and complex interaction with the user... and then you could say "in X particular case, using direct SQL is more efficient" in my experience, specially with OLTP applications, Hibernate helps creating a more mantainable, "more correct" (from concurrecny or business logic validation point of view) and more efficient solution.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 11:36 am 
Newbie

Joined: Sun May 14, 2006 5:09 pm
Posts: 11
Thanks luxspes,
The answer really helpful, the credit has been added for your kindly help


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