-->
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: Caching and Associations
PostPosted: Thu Mar 11, 2010 11:20 am 
Newbie

Joined: Tue Mar 09, 2010 6:30 pm
Posts: 6
Hello,

This is my first post here so if I'm missing anything out, please don't hesitate to let me know. For information, I've been reading the forums for a bit, along with the Persistence with Hibernate book so I do have some background information but I'm in no way very competent in this :)

I'm using JBoss 5.1.0-GA along with the distributed version of Hibernate that ships with it, and I use annotations in my entities for peristence using (ie I do not manage hibernate sessions etc. manually).

I have a simple entity called 'room' that has a one to many associations with another entity called 'window' such as:

Code:
   
class RoomDTOImpl...

    @OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL, CascadeType.PERSIST }, targetEntity = WindowDTOImpl.class)
    @JoinColumn(name = "ROOM_ID", referencedColumnName = "ROOM_ID", nullable = false, updatable = false, insertable = true)
    public Set<WindowDTO> getWindows() {
        return myWindows;
    }


In my Window entity, I have enabled query hints for a certain named query such as:
Code:
class WindowDTOImpl...

        @NamedQuery(name = PersistenceConstants.NAMED_QUERY_GET_WINDOWS_FOR_ROOMS, query = WindowDTOImpl.GET_WINDOWS_FOR_ROOMS, hints = {
                @QueryHint(name = "org.hibernate.cacheRegion", value = "HibernateTest"), @QueryHint(name = "org.hibernate.cacheable", value = "true") }) })


I have also enabled JBoss cache in my persistence.xml. The cache seems to be properly enabled and working in the logs.

I'm now trying to do the following:

1- Fetch Windows for Rooms using the cached query
2- Fetch Rooms

My idea was that because the second level cache is being used, I would have expected to see the following:

First Run:
1- Select Windows for Rooms
2- Fetch Rooms without using JOIN FETCH since hte windows would already be cached

Second Run:
1- Do NOT fetch Windows for Rooms (cached)
2- Fetch Rooms without using JOIN FETCH since hte windows would already be cached

When I run sample tests, the second point is not what I see. I still will always load the entire rooms and windows association.

So my question is this: if I enable caching for a particular named query, the results are only cached for subsequent calls that would use that named query or there is a way to "expose" the results so that other methods may use them? Note that I don't think one or wrong and the other is right. I can certainly understand how there is no way for hibernate to know that I've already fetched what it will need when it does not know yet what to fetch...

This is basically for the following purpose: when I have 1 object that has a one to many and another one to many (unrelated), using JOIN FETCH can be extremely costly because I could end up, for say 10 objects with 15 sets for each association with an exponential number of rows because the SQL statement will load as such:

Code:
SELECT T1.ID, T1.NAME FROM T1 INNER JOIN T2 ON T1.T2_id=T2.ID INNER JOIN T3 ON T1.T3_id=T3.ID


I'm not sure this makes too much sense, sorry if it does not :)

I'm trying to get a good grasp of what my options are in this scenario and how to properly apply them.

Regards,

Greg


Top
 Profile  
 
 Post subject: Re: Caching and Associations
PostPosted: Thu Mar 11, 2010 4:05 pm 
Newbie

Joined: Tue Mar 09, 2010 6:30 pm
Posts: 6
Hi,

I will answer part of my post to clarify some things.

My issue is the following:

I purposely load a list of WindowDTO entities that contain their identifiers along with a RoomId, for a particular RoomId, say 10 to cache them.
Then I want to load a particular RoomDTO entity with ID 10.

What I don't understand is that Hibernate will, via eager fetching, issue SQL statements to load my RoomDTO's WindowDTO one by one.

How can I make it understand that I have already loaded those? Is is not possible because in one case I just loaded a list of entities and in the other I am trying to load an association and only that particular association will be subsequently cached? In other words, these are the same objects but they are cached separately?

Thanks,

Greg


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.