-->
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: Can one-to-many associations be discovered from the cache
PostPosted: Tue Jun 21, 2005 4:04 am 
Newbie

Joined: Wed Mar 02, 2005 10:02 pm
Posts: 13
Location: Adelaide, Australia
Can Hibernate be configured such that is understands that all children in a one to many relationship are in the cache?

This would allow it to query the cache to determine the children when navigating from parent to children, instead of querying the database.

e.g.
Code:
class Parent {
    Set children;  // one-to-many
}

class Child {
    Parent parent; // many-to-one   
}


// Load
Collection children = session.createCriteria(Child.class).list();
Collection parents = session.createCriteria(Parent.class).list();
...
// Ideally, calling the line below would not cause Hibernate to determine the children from the database, but from the session cache instead.
parent.getChildren().iterator().next() ...

Note: I understand I can configure outer-join and batch-size parameters to improve Hibernate's default performance, but both have significant limitations in functionality and performance.

If Hibernate cannot query the cache, does anyone have an efficient work-around for the scenario of loading any entire object graph?

Hibernate version:
2.x or 3.x


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 4:05 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Did you read the caching documentation before posting?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 4:23 am 
Newbie

Joined: Wed Mar 02, 2005 10:02 pm
Posts: 13
Location: Adelaide, Australia
Yes, I have read the documentation and believe what I am asking is not possible.

I was/am hoping that an explicit optimisation has been made for the case where an entire object graph is loaded. i.e. when Hibernate generates a where clause of "where 1=1"

I believe it is a common requirement in n-tier deployments to cache all instances of a certain "static" classes in memory on remote tiers. Hence, I'm hoping that others will have an efficient solution to this problem using Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 4:28 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
So read it again and try to understand "collection cache". It in the first sentence.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 5:15 am 
Newbie

Joined: Wed Mar 02, 2005 10:02 pm
Posts: 13
Location: Adelaide, Australia
OK, I've re-read the documentation and enabled 2nd level caching for both the Parent and Child and the collection. I see debug output showing the collections being cached, etc, so caching seems to be working.

However, it has no effect for my issue. i.e. Hibernate still accesses the database to determine what children there are for a given parent.

My issue is about trying to get Hibernate to efficiently load the collections in the first place, not caching them efficiently after retrieving them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 5:19 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Could it be that you are doing something wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 5:47 am 
Newbie

Joined: Wed Mar 02, 2005 10:02 pm
Posts: 13
Location: Adelaide, Australia
I certainly could be doing something wrong. :-)

However, I suspect that Hibernate doesn't support the functionality I'm looking for.

i.e. If I was hand writing this it would be something like:

1. select * from Parent
- construct a parent object for each row
- put each parent object in a map based it's key

2. select * from Child
- contruct a child object for each row
- lookup the related parent object from the map
- set the parent reference on the child
- add the child to parent set

Note: you could also load the Child rows first, then Parents

I was hoping that Hibernate might be able to do something similar. However, despite loading all Parent and Child objects explicity within the same session, Hibernate will still go to the database to locate the children when I navigate from parent to children. (It's fine navigating from child to parent and loads from the session cache in this case).

I understand that in the general case, Hibernate has to go to the database to find the children. However, in the special case where all objects for a given class are in the session cache, this is not necessary. This is the optimisation I'm looking for.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 5:51 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hibernate supports all of this. And if you put in a little more effort with the documentation (and maybe Hibernate in Action) you will certainly find out how.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 6:09 am 
Newbie

Joined: Wed Mar 02, 2005 10:02 pm
Posts: 13
Location: Adelaide, Australia
Thanks for your time.

I'm yet to find any Hibernate feature that does what I looking for. i.e. a single select per table to load a graph where collections are involved. And I think I'm fairly up to speed on lazy loading, outer-join and batch-size configurations.

FYI, I do have a copy of Hibernate In Action. :-)

Anyway, I'll keep digging ... thanks again for your time. Much appreciated.


Top
 Profile  
 
 Post subject: I'm trying to get my points....
PostPosted: Wed Sep 28, 2005 1:51 pm 
Regular
Regular

Joined: Thu Sep 09, 2004 6:46 pm
Posts: 96
I believe he is talking about Query Cache. You can query the Parents and initialize the collections for the children.

The 2nd level cache keeps the result set for a query with same parameters so the 2nd time you query it will not go to get either Parents or Children.


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.