-->
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.  [ 3 posts ] 
Author Message
 Post subject: how do I use ehcache in JPA with Lazy collections?
PostPosted: Wed Aug 12, 2009 6:00 pm 
Newbie

Joined: Tue Sep 20, 2005 3:51 pm
Posts: 18
Location: Boston, MA
Hello All,
How can I cache a query for an object with children that are lazily fetched? I am defining the collection as LAZY fetch and retrieving the children via a "left join fetch" in the query.

When I set hibernate.cache.use_query_cache to false, everything works great. When I enable ehcache, it works the first time, but the 2nd time I load the page, I get the exception "failed to lazily initialize a collection, " presumably because it's retrieving the data from the cache. The error specifically happens when a facelet in my view loops through the children to print them.

I can get the error to go away if I loop through the collection and call any method in each child while still in the DAO that performs the query.

Surely, I am missing something.

Any suggestions would be greatly appreciated.

I have an application using Hibernate 3.3 through Spring 2.5 and JPA.

My exception is:
Code:
Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: mydomain.MyObject.replies, no session or session was closed
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
   at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
   at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
   at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
   at com.sun.facelets.tag.jstl.fn.JstlFunction.length(JstlFunction.java:88)



My bean is listed below. I am using the named query listed below, MyObject.BY_UUID.

Code:

@Table(name = "my_table")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@NamedQueries( {
      @NamedQuery(name = MyObject.BY_UUID, query = "from MyObject g left join fetch g.replies  where g.uuid in (:uids)", hints = @QueryHint(name = "org.hibernate.cacheable", value = "true")) })
public class MyObject  {
   public static final String BY_UUID = "BY_UUID";
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private int id;
   private String title;
   private String uuid;
   @OneToMany(cascade = CascadeType.MERGE)
   @JoinTable(name = "my_table_comment", inverseJoinColumns = @JoinColumn(name = "reply_id"))
   @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
   private List<Comment> replies = new ArrayList<Comment>();
//getters and setters
}


My persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
   version="1.0">
   <persistence-unit name="grants">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
         <property name="hibernate.show_sql" value="true" />
         <!-- 2nd level cache  -->
         <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
         <property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />
         <property name="hibernate.cache.use_second_level_cache" value="true" />
         <property name="hibernate.cache.use_query_cache" value="true" />

         <property name="hibernate.generate_statistics" value="true" />
         <property name="hibernate.cache.use_structured_entries" value="true" />
      </properties>
   </persistence-unit>
</persistence>


My DAO:
Code:

@Repository
@Transactional
public class MyObjectDAO{
   public Grant findMyObjectByGUID(String guid) {   
      MyObject obj =  namedQueryWithSingleResult(MyObject.BY_UUID, "uid", guid);
      ehcacheHack(obj);
      return obj;
   }

   private void ehcacheHack(MyObject grant) {
      logger.debug("I was called");
      for(Comment c : grant.getReplies()){   //ehcache hack.  Calling collection while session is open stops lazy init exception
         c.getId();
      }
   }

   public <K, E> E namedQueryWithSingleResult(String queryName, String columnName, K key) {
      Query qry = em.createNamedQuery(queryName);
      qry.setParameter(columnName, key);
      qry.setHint("org.hibernate.cacheable", true);
      return (E) qry.getSingleResult();
   }




Any helps is greatly appreciated.


Top
 Profile  
 
 Post subject: Re: how do I use ehcache in JPA with Lazy collections?
PostPosted: Mon Apr 26, 2010 10:43 am 
Newbie

Joined: Mon Apr 26, 2010 10:33 am
Posts: 1
I get the same exception when I try to enable ehcache 2nd level cache with JPA/hibernate.... Did you manage to crack it..?

regards


Top
 Profile  
 
 Post subject: Re: how do I use ehcache in JPA with Lazy collections?
PostPosted: Wed Oct 27, 2010 11:16 am 
Newbie

Joined: Wed Oct 27, 2010 11:14 am
Posts: 1
Sorry for resurrecting this topic, but we're encountering the exact same problem and would like to know if anyone has a solution for this?


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