-->
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: hibernate showing two sql statements for update
PostPosted: Tue May 30, 2006 1:16 pm 
Newbie

Joined: Tue Mar 28, 2006 11:46 am
Posts: 10
Hi,

I am using hibernate 3.1.3.

In one hibernate session I am creating and updating the same object. I am executing update using createQuery method. I will always update this object using createQuery method. In hibernate mapping file "show_sql" is set to true. So when I execute my program I am seeing one insert and two update sql statements. I am totally confused why hiberate is sending two updates.

Other thing which I have noticed if I create and update of object in two different sessions then hibernate creates only one update statement.

MY PROGRAM
=============
Session session = HibernateSessionFactory.openSession();
Transaction tx = session.beginTransaction();
// insert record
User user = new User();
user.setId(1);
user.setName("test");
user.setDescription("testing create");
session.save(user);
// update same record
Query query = session.createQuery("update User set name=? where id=?");
query.setParameter(0, "newName");
query.setParameter(1, 1);
int numInserted = query.executeUpdate();
tx.commit();
session.close();

OUTPUT SQL
===================
Hibernate: INSERT INTO User (name, description, id) VALUES (?, ?, ?)

Hibernate: UPDATE User SET name=?, description=? WHERE id=?

Hibernate: update User SET name=? WHERE id=?

MAPPING FILE
========================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="systemdb.model">
<class name="User" table="User">
<id name="id" column="id" type="int">
<generator class="assigned"/>
</id>
<property name="name" column="name" type="string"/>
<property name="description" column="description" type="string"/>
</class>
</hibernate-mapping>

It will be very greatful if somebody can explain me why this is happening. Is there are any configuration I have to set so that hibernate won't generate its own update statement and just use which are specified in createQuery method.

Thanks.

Sanjeev


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 2:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You want to look at http://www.hibernate.org/hib_docs/v3/re ... tch-direct

Basically, bulk update delete statements should not be issued from sessions which already have entities associated with them.

But anyway, the first update comes from the flush process that occurs prior to executing the HQL query. Somehow the Session thinks that User has changed between the time you save() it and the time it gets flushed. Typically this indicates bad getter/setter implementations or problems with the type mappings. Paste in the source of your User class (and please use the code tags)...


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 30, 2006 2:46 pm 
Newbie

Joined: Tue Mar 28, 2006 11:46 am
Posts: 10
Thanks you.

You were right. My junit test between the insert and update was updating the user object.

Is it possible if you can reply to one of my posting where I am waiting for a correct answer.

http://forum.hibernate.org/viewtopic.php?t=959208

Thanks.


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.