aspose file tools*
The moose likes Object Relational Mapping and the fly likes What causes Hiberante to issue a SQL update command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "What causes Hiberante to issue a SQL update command" Watch "What causes Hiberante to issue a SQL update command" New topic
Author

What causes Hiberante to issue a SQL update command

ying lam
Ranch Hand

Joined: May 17, 2004
Posts: 85
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.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

"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.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

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

Joined: May 17, 2004
Posts: 85
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

Joined: May 17, 2004
Posts: 85
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.

Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
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

Joined: May 17, 2004
Posts: 85
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

Joined: Aug 24, 2005
Posts: 518
[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 ]
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547

>> 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

Joined: Aug 24, 2005
Posts: 518
[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

Joined: Jun 19, 2001
Posts: 547
Hibernate does not guarantee that the only time sqls are executed is when the session is flushed.

Point taken :-)
 
 
subject: What causes Hiberante to issue a SQL update command