-->
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.  [ 4 posts ] 
Author Message
 Post subject: session.save generates unnecessary(?) SELECT before INSERT
PostPosted: Fri Mar 21, 2008 7:20 am 
Newbie

Joined: Fri Mar 21, 2008 7:01 am
Posts: 2
Hibernate version:
3.0.5

Mapping documents:
<class name="Country" table="COUNTRY" >
<id name="id" column="COUNTRYID"> </id>
<property name="name" column="NAME" />
</class>

<class name="City" table="CITY">
<id name="id" column="CITYID"></id>
<property name="name" column="NAME" />
<many-to-one class="Country" name="country" column="COUNTRYID"/>
</class>

Code between sessionFactory.openSession() and session.close():
Transaction transaction = session.beginTransaction();
// Country("US", "USA") is already in DB
City city = new City("WA", "Washington", new Country("US", null));
session.save(city);
transaction.commit();

Name and version of the database you are using:
HSQLDB 1.8.0.8

The generated SQL (show_sql=true):
Hibernate: select country_.COUNTRYID, country_.NAME as NAME1_ from COUNTRY country_ where country_.COUNTRYID=?
Hibernate: insert into CITY (NAME, COUNTRYID, CITYID) values (?, ?, ?)


Why does hibernate perform select query before insert query? How to configure it to perform only insert query?

Thanks for help!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 21, 2008 12:15 pm 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
Hi,

this is what I believe is going on:

you are trying to save a City object by providing a transient Country object with its identifier set. Since you have defined in your .hbm files a unidirectional many-to-one association, hibernate, in order to maintain the integrity of the association, must somehow verify that this object you are passing is indeed a valid persistent object. Since, it can’t find in its session cache a loaded persistent object with the same identifier it must perform this extra select statement.

I don’t think there is a way to avoid that extra select but a more elegant way of performing all this, in my view, is to load first the associated Country object (e.g. Country("US", "USA")) and then set this in your City object.


mosa


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 21, 2008 12:17 pm 
Newbie

Joined: Fri Mar 21, 2008 11:35 am
Posts: 10
I believe it's because you're using a new object, rather than an already committed object. Hibernate will map it based on ID, since your object (defined in new Country("US",null)) doesn't have an ID so it has to determine which one to use.

If instead you found the US country in the DB first, then persisted the entire thing, you wouldn't have that select (though, finding the US country would be a select as well).

Although, maybe "US" is actually the ID for the country table? In which case, it is likely verifying that the US id already exists (in order to maintain proper FK)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 23, 2008 1:50 pm 
Newbie

Joined: Fri Mar 21, 2008 7:01 am
Posts: 2
Thanks for replies.
"US" is the ID for the country table.
This select is unacceptable performance overhead in my application. I hope there is a way to eliminate it.


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