• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

What causes Hiberante to issue a SQL update command

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code which just query an object from the database.
But when I turn on hibernate sql output. it causes 2 sqls:
1. query
2. update

My question is why hibernate generate the 2nd update command? I did not modify the cat object at all in my transaction?


private static Cat getCat(String name, Home home) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();

// cause hibernate to create 1 query. This is expected
Query query = session
.createQuery(
"from Cat as cat where cat.name= :name and cat.home.id = :homeid");

List result = query.setString("name", name).setLong("homeid",
home.getId().longValue()).list();

// why hibernate caused a sql update query here? I did not modify
// any of the persistent object
session.getTransaction().commit();

if (result.size() == 1) {
return (Cat) result.get(0);
}
}



Here is my mapping file:



<class name="com.testproject.model.Cat" table="BUILDS"
lazy="true">
<id name="id" column="CAT_ID">
<generator class="native" />
</id>
<version name="version" column="version"
type="java.lang.Integer" />


<property name="name" not-null="true" />

<many-to-one name="home" column="HOME_ID"
class="com.testproject.model.Home" not-null="true" />


</class>

Is there a way to disable it? I would like to minimize Hibernate sql if possible. The object that I get from the database should be 'clean' instead of 'dirty' so hibernate shouldn't need to update it. Right?

Thank you.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"yinglcs",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First the code is tough to read. There is a CODE button below Add Reply button to put CODE tags in your post so that you can put your actual code in between them and keep its indentation.

But I am thinking that there must be more to your code that you didn't post.

If you had loaded an object, made a change, then run a query, Hibernate will execute the update for the changed object when the query is run because in order for Hibernate to be sure that th query will return the correct results it needs to update/insert/delete data that might be changed that needs to be changed in the database so that the query gets the correct data.

Mark
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Here is the code and mapping file. I appreciate if you can tell me why hibernate issues an UPDATE command. And how can I avoid it?

Code:


Mapping file:

sql generate by hibernate
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code I execute the query:

From the code, I did not modify the object I get from the query. So I don't understand why hibernate issues an update command.

Any help is appreciated.

Thank you.

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you committing the transaction after the call to list()?

Which line appears to be triggering the update sql to execute. (Is it happening during the commit?)

What was the value of version in the database before the select? Was it null?

Set the Hibernate log level to DEBUG. You should see some log entries that specify which property of cat is dirty. (Search for "dirty".)
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help.

>> Why are you committing the transaction after the call to list()?
This is because I just get the presistent object thru the query. And I return from the getCat() method , as a result, I commit the transaction before I return the 'Cat'.

>> Which line appears to be triggering the update sql to execute. (Is it happening during the commit?)

Yes, it happens during commit.

>> What was the value of version in the database before the select? Was it null?
It was 0


>> Set the Hibernate log level to DEBUG. You should see some log entries that specify which property of cat is dirty. (Search for "dirty".)

Okay, i will try that.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[ying]It was 0

I'll bet that's the issue. Manually run an sql to set the version to 1 and see if the update still happens.

I think Hibernate sees the 0 and thinks it's a default value and that the object has never been persisted.
[ November 05, 2006: Message edited by: Scott Johnson ]
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


>> Which line appears to be triggering the update sql to execute. (Is it happening during the commit?)

Yes, it happens during commit.


SQL statements are only sent to the DB when flushing (which is done automatically when commiting) the session.

But the versioning seems to be a good candidate .

Pascal
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[Pascal]SQL statements are only sent to the DB when flushing (which is done automatically when commiting) the session.



Hibernate does not guarantee that the only time sqls are executed is when the session is flushed.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hibernate does not guarantee that the only time sqls are executed is when the session is flushed.


Point taken :-)
 
He's giving us the slip! Quick! Grab this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic