-->
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: ManyToOne association is not fetched from cache?
PostPosted: Sat Oct 31, 2009 5:40 pm 
Newbie

Joined: Sat Oct 31, 2009 3:36 pm
Posts: 6
I wanted to introduce caching in my enterprise app, so I configured everything and made simple unit test to check if it works.
And it did work, log file proved everything, I saw entities being cached and later picked up from cache.
This was a unit test and hibernate was configured with JPA RESOURCE_LOCAL DS. (i think this may be important)

So I proceeded to deploy entire app to JBoss5.1 and log showed that cache is configured properly just like standalone unit test.
HOWEVER, entity I was looking for is fetched from cache but when i navigate to its ManyToOne assoc, i get lazy exception. I spent days debugging this but I still don't realize why it works with unit test and not within container (same entities, same session beans)

I logged everything and I saw that associated entity is placed in cache but it is not fetched from cache, lazy exception is thrown instead.

I don't know if it is important but I use named query to load entity and query cache is properly configured. The same ehcache.xml is used for both unit test and EAR application (only data source is different).

Entity looks like this:
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;



I get employees with named query which explicitly joins with departments and department is loaded and shown first time. I can observe in the log file that both employee and department are put in cache.


When I access employee second time, I can see it is found in cache but when i access it's department I get lazy exception. Hibernate didn't even try to check the cache, even though department IS cached.

But, why does it work in unit test???


Thanks.


Top
 Profile  
 
 Post subject: Re: ManyToOne association is not fetched from cache?
PostPosted: Sun Nov 01, 2009 4:47 pm 
Newbie

Joined: Sat Oct 31, 2009 3:36 pm
Posts: 6
OK, I understand that unit test works because entity manager is manually created and closed. I call "getDept()" before em is closed:
Code:
@Test
public void testEmployee() {
   MyServiceBean myService = new MyServiceBean();  //session bean that does the work
   myService.em = emf.createEntityManager();;
   Employee emp = myService.findEmployeeByDept("SALES");  //executes named query that joins employee and dept tables
   System.out.println(emp.getDept());
   em.close();
}


When the app is deployed, emp.getDept() is called from the web tier and at that point em i already closed.

At least that is my explanation, I am not sure if it is entirely correct, please correct.

BUT...

Even though dept is mapped as lazy, it is definitely loaded by custom query and I can access it first time when it comes from DB. Next time, it comes from cache and lazy excpetion is thrown.

I am willing to provide more info if anyone finds it necessary. I spent hours fighting with this and haven't got any ide what am I supposed to do to fix this most basic example.

Thanks.


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.