-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate issuing a lot of sql statements
PostPosted: Wed Sep 10, 2003 7:26 pm 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
I have a very general question. I have my setup with hibernate working just fine (21 tables) with a lot of associations where one entity has a collection of another entity(one-to-many). I am using cascade="save-update" on almost half of these relationships. Most of my normal calls to hibernate are working fine but it seems that hibernate is issuing a lot of sql calls to the database. Even when table_A 1 has a one-to-many relation to table_B and table_B is empty. Hibernate seems to still make a lot of calls to the database for no apparent reason.

Some of my tables have a lot of data loaded in them while others are empty initially.

Generally speaking, what would cause it to make such a large number of calls? I understand that this is a very broad question, but there is not error to report other than that I am seeing a bunch of queries that I don't seem to see a reason for. Just checking if anyone else has seen this kind of behaviour and figured out why it was happening.

Thanks for all your comments.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2003 7:32 pm 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
BTW, I am using Hibernate 2.0.3. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2003 10:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This IS a ridiculously broad question.

In general, to reduce the number of queries, you have to carefully think about how associations are mapped: are you using proxies effectively? are you using outer join fetching effectively? Have you enabled caching, where appropriate, etc.


Top
 Profile  
 
 Post subject: Thanks...
PostPosted: Thu Sep 11, 2003 11:05 am 
Newbie

Joined: Sun Aug 31, 2003 1:20 am
Posts: 17
I actually intended to have the question be this broad. I wanted kind of a list (like the one you gave) of the things that might affect the issue I was describing. Just wanted to know if I missed anything that I need to take a look at. Thanks for the reply. I have been going over the different settings and checking what the impact was. Will post what I find since I am sure it is something that I have overlooked.


Top
 Profile  
 
 Post subject: Why there are lots of queries for associations...
PostPosted: Thu Sep 11, 2003 2:48 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
I have figured this out as a result of debugging a problem I am having with getting too many results from my Criterias.

In my system (maybe I am still doing something wrong...) I get one query executed for every single object (or collection thereof) that lives within my mapping hierarchy. What this means is that if you have the following structure:
Code:
class A {
    private Map bInstances = new HashMap();
}

class B {
    private Map cInstances = new HashMap();
}

class C {
    private Long something = new Long(0);
}


Let's say there are two records for class A in the database...
Four records for class B (A1-B1, A1-B2, A2-B3, A2-B4)...
And eight for class C (B1-C1, B1-C2, B2-C3, B2-C4, etc. ...)...

Assuming it is mapped with <one-to-many...> from A to B and B to C...

When you load an instance of class A you will get the following amount of SQL:
one select for the A record and all it's joining etc from the where criteria.
one select for the list of B records that go with A
depending on how things are joined: one or two more selects for the B records (whether you wanted them or not) details
two more selects for the lists of C records for each of the B's.
four selects for the details of the four C records.

As you can see this becomes exponentially large in nature very fast.

In my system I get several pages of sql output when I load one record because I have nested mappings 5 layers deep with mulitple mappings at the fifth level and the exponential amount of queries is staggering but it does seem to run fairly quickly.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 11:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Map your collections with lazy="true", and understand what setFetchMode() does.


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