-->
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.  [ 10 posts ] 
Author Message
 Post subject: db access number
PostPosted: Fri Aug 27, 2004 2:08 am 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
Hi,

I want to know how many real DB accesses in Hibernate Query.

Say Object A has a collection of Object B (10 records) and set non-lazy initialization for collection B. When I retrieve one record A, the SQL generated will be 11, one for record A and 10 for record B. Does it mean that in this single HQL, hibernate uses 11 DB accesses. Then it will be extremely non-effecient especially the application and db are in the different computer.

Please tell me if the number of SQL generated by hibernate is the number of real db accesses. Any hint is really appreciated.

thanx


Top
 Profile  
 
 Post subject: Re: db access number
PostPosted: Fri Aug 27, 2004 2:19 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
pwangb wrote:
Hi,

I want to know how many real DB accesses in Hibernate Query.

Say Object A has a collection of Object B (10 records) and set non-lazy initialization for collection B. When I retrieve one record A, the SQL generated will be 11, one for record A and 10 for record B. Does it mean that in this single HQL, hibernate uses 11 DB accesses. Then it will be extremely non-effecient especially the application and db are in the different computer.

Please tell me if the number of SQL generated by hibernate is the number of real db accesses. Any hint is really appreciated.

thanx


I assume you've turned on logging particularly for SQL, all the sql you see is all the db hits.
so, what you need to do is consider where to use outer-join="true" and other considerations as well, please read hibernate docs for more info.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 12:06 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
thank you for the reply. Could you please point out which part of the hibernate doc I should read. thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 2:38 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
I checked the document, the default value for outer-join is "auto". I don't really know the difference between "true" and "auto"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 3:15 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
I have Parent P and Child C. For the following HQL
from C as c where c.parent.uid_pk = ?
Hibernte issues 3 SQL command, it means it hits DB 3 times for this simple query.

Howerver, if I just use jdbc instead of hibernte, I only have 1 db hit.

does it mean hibernate has worse performance then pure jdbc connection with more db hits?

I am really confused and lost here. Please give me some ideas.

thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 3:46 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
pwangb wrote:
I have Parent P and Child C. For the following HQL
from C as c where c.parent.uid_pk = ?
Hibernte issues 3 SQL command, it means it hits DB 3 times for this simple query.

Howerver, if I just use jdbc instead of hibernte, I only have 1 db hit.


FALSE.

If one single sql query you will retrieve only the child C where you will have both the parent and the child with Hibernate.
Why so?

Because when building the java instance for the child C, Hibernate needs to set the value of the 'parent' field. This value is an instance of your Parent java class.

If you don't want Hibernate to initialize the Parent when you retrieve a Child, then consider using the 'lazy' keyword on your Parent mapping definition. If the Parent is lazy, HIbernate will instantiate a proxy rather than a concrete class. The actual Parent value will then be loaded from the DB the first time you access it.

On the other hand, both the Child and the Parent could be retrieved in one single query (using outer join) but probably not in your case because you have been using an HQL query to find out the child instance to load.

Have a look at the 3 generated SQL statements and you will understand what is happening. Then you will surely find a way to reduce the amount of DB accesses...


pwangb wrote:
does it mean hibernate has worse performance then pure jdbc connection with more db hits?


Of course not... provided you understand what Hibernate is doing and how to use it.


pwangb wrote:
I am really confused and lost here. Please give me some ideas.


The best ideas you can have is by reading the entire documentation and try by yourself. When you hesitate, try both solutions, turn sql debug on, and see what is happening.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 4:39 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
thank you for your reply.

I don't know how to set Parent lazy. I tried to set lazy="true" in the many-to-one mapping on the children side, however it doesn't work. Could you please tell me how to do that? I don't know either if it can work without any proxy setting.

I have another question here. Say I have 1000 products that the user view. After the first time user view them, I want to cache them in the hard disk so that it won't have db hit next time.
I tried to use OSCache. What I did is copying oscache.jar file to web-inf/lib folder, copy oscache.properties file to web-in/classes folder. modify the properties file as save to hard disk and provide the folder name. add the following property into hibernate.cfg.xml file
<property name="hibernate.cache.use_query_cache">true</property>
In the persistent mapping file: I use <cache usage="read-write"/>In the query, I used session.createQuery().setCacheable(true).

However, it doesn't work. I don't know how to make it work. I don't see any caching file and I have no idea how to make OSCache work together with Hibernate.

Please help me out. thanx a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 5:17 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
http://www.hibernate.org/hib_docs/refer ... ance-cache

you need to specify a cache provider in the hibernate.cfg.xml

to make a A many-to-one B assocation lazy
open B mapping file
in the class element set lazy = "true" and write a default contructor in B

you can also fetch the association in hql
select a from A a join fetch a.b

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 6:33 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
Actually, I want the initialization for Parent to be lazy. I mean when I load Child, the Parent object is not initialized. There is nothing to do with collection. How to do it?


I did specify a cache provider in the hibernate.cfg.xml like:
<property name="hibernate.cache.provider_class">net.sf.hibernate.cache.OSCacheProvider</property>

However, the weild thing is that:
If I keep running the same query, the results is cached.
If I run query1, query2 and query1, the result of query1 is not cached anymore.

my code is like:
session.createQuery("from A as a where a.uid_Pk = 28")
.setCacheable(true).setCacheRegion("a").list();session.createQuery("from B as b where b.uid_Pk = 28")
.setCacheable(true).setCacheRegion("b").list();

thanx.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 29, 2004 6:44 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
pwangb wrote:
Actually, I want the initialization for Parent to be lazy. I mean when I load Child, the Parent object is not initialized. There is nothing to do with collection. How to do it?


Sorry man, but it looks to me you haven't read the doc - or not long enough...

Last hint: do you have to cache the *result* of a query - in which case the query cache is what you are looking for (caches on the id of the persistent entity matching the query) - or do you need to cache the persistent entities so you don't have to reload them from the db ?

Think about it, test it and then come back with more detailled questions ;)


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