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