-->
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.  [ 5 posts ] 
Author Message
 Post subject: cache and lazy fields
PostPosted: Tue Nov 03, 2009 6:56 am 
Newbie

Joined: Sat Oct 31, 2009 3:36 pm
Posts: 6
I have already asked this question, but I got no answers, I'll try to rephrase it:

I have entity Employee that is associated to Department by ManyToOne lazy association.

I use custom query with explicit join to load employees with their department.

When I introduce cache, custom query is not executed only once (because it is cached), and subsequent calls return cached data.

Problem is that employees returned from cache don't have their "dept" field set (it is lazy field, only proxy is there) and i get lazy exception when trying to access employees department.

It sounds OK that objects from cache will initialize their lazy fields on demand, but when i ask for it, it is already late, session is closed. (this is not a problem when objects come from DB because explicit join is used)

Is there any way I can use cache and keep "lazy" init???

Thanks!


Top
 Profile  
 
 Post subject: Re: cache and lazy fields
PostPosted: Tue Nov 03, 2009 7:01 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Did you use "join fetch" explicitly? "Join" is not enough.

Anyway, some example code would help...

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: cache and lazy fields
PostPosted: Tue Nov 03, 2009 7:30 am 
Newbie

Joined: Sat Oct 31, 2009 3:36 pm
Posts: 6
This is JPA named query:
Code:
"SELECT e  FROM Employee LEFT JOIN FETCH e.dept"



Employee.class

Code:
@Entity
@Table(name = "EMP")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class Employee implements Serializable {
   @Id
   @Column(name = "EMP_ID")
   private Long id;
   
   @JoinColumn(name ............)
   @ManyToOne(optional = false, fetch = FetchType.LAZY)
   private Department dept;



method that loads employees:

Code:
List<Employee> employees =
  (List<Employees>) em.createNamedQuery("Employee.load")
  .setHint("org.hibernate.cacheable", "true")
  .getResultList();


First call is OK, data comes from DB and dept is inited.

2nd call, data comes from cache and dept is not inited. I see dept is cached but hibernate doesn't bother to get it form cache and set it to employee.

Thank you very much for any help.


Top
 Profile  
 
 Post subject: Re: cache and lazy fields
PostPosted: Tue Nov 03, 2009 11:18 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
From hibernate ref:
Quote:
Note that the query cache does not cache the state of the actual entities in the result set; it caches only identifier values and results of value type. The query cache should always be used in conjunction with the second-level cache.


Are you also using second level cache? If yes, how are your entities configured to be cached?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: cache and lazy fields
PostPosted: Tue Nov 03, 2009 12:53 pm 
Newbie

Joined: Sat Oct 31, 2009 3:36 pm
Posts: 6
Yes, I DO use second level cache and observe what is happening in the log output. It is clearly shown that dept is cached

01.Nov.09, 15:38:22 DEBUG org.hibernate.cache.ReadOnlyCache debug() Caching: mypkg.Department#14420
01.Nov.09, 15:38:22 FINE net.sf.ehcache.hibernate.EhCache put() key: mypkg.Department#14420 value: CacheEntry(....


I believe that 2nd level cache is properly configured, because if I navigate to dept while session is open, I get it from cache. (shown in the log file)

If I walked through the list of all employees and called getDept() on each (while session is still opened), my problem would be gone. But I'd like to avoid explicit lazy initialization in lists (it is not necessary when results come from DB).


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