-->
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.  [ 2 posts ] 
Author Message
 Post subject: Simple performance in Hibernate question
PostPosted: Sun Apr 18, 2010 5:42 pm 
Newbie

Joined: Sun Apr 18, 2010 5:33 pm
Posts: 3
How to make hibernate issue one "Insert" sql statement ? (and not some unnecessary Selects) ?

In a simple case where I have Person object that has many Emails, and I want to add an email address to some person I do the following (taken from Hibernate example):

session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
aPerson.getEmailAddresses().add(emailAddress);
session.getTransaction().commit();

checking the logs, hibernate execute THREE sql stmt for that:

Hibernate: select person0_.PERSON_ID as PERSON1_2_0_, person0_.age as age2_0_, ...

Hibernate: select emailaddre0_.PERSON_ID as PERSON1_0_, emailaddre0_.EMAIL_ADDR as EMAIL2_0_ from PERSON_EMAIL_ADDR .....

Hibernate: insert into PERSON_EMAIL_ADDR (PERSON_ID, EMAIL_ADDR) values (?, ?)

why THREE ?
why not just the "INSERT" ?
how do I execute just one simple INSERT statement with Hibernate ? (without the TWO selects)


Top
 Profile  
 
 Post subject: Re: Simple performance in Hibernate question
PostPosted: Mon Apr 19, 2010 2:42 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi haimke,

usually making this 2 selects don't represent a performance problem, as there should exist indexes on the
regarding attributes referred in the where-condition, so the selects should just take some millisecond to execute.
This 2 selects are necessary as this is the way the hibernate persistent context work:
-first you load the entities into persistent context making internal snapshots all loaded (non-readonly) entities
-then you do your changes
-on flush hibernate checks the difference between the entities and their regarding internal snapshots.
In this way hibernate recognizes which entities has to be updated on the database.

It the 2 selects create a performance problem to you, then you should check
-if they create a performance problem because they are executed extremely and unneccessarily often
-it they crate a performance problem because the database is slow or there are indexes missing on the database.

If you absolutely don't want this 2 select because you simply don't want them,
then you can use hibernates bulk-update feature for doing the inserts.
(You must be aware, that using bulk-updates(inserts) you are working outside hibernates persistent context)


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