3 Replies Latest reply on Nov 17, 2005 5:51 AM by Emmanuel Bernard

    Caching Entity Beans

    Marcus Schmidke Newbie

      Hello,

      I'm still fairly new to EJB3.0/Hibernate and am trying to get some caching working. I've searched all the docs (Hibernate Annotations etc.) and the Internet, but wasn't able to figure out what to do.

      Do you know a site, tutorial, doc, ..., which I didn't find?

      My model has several entities which change very rarely or not at all. Furthermore, there are not too many of those entities, so I would like to have them in memory instead of having gigantic joins on every single query.

      I turned caching on using the @Cache annotation on the entities. Firstly, this didn't change anything because Hibernate seems to insist on querying non-lazy associations always in a Join, no matter if caching is enabled. I've constructed a workaround: made all associations lazy and implemented @Postload-Methods with Hibernate.initialize.

      This seemed to work well, but the next problem was already right at the door: the cache was obviously there, but did not last very long. In fact, it lasted extremely short, for about one transaction ...

      So what do I have to do:
      - to configure the cache to last longer than one transaction
      - to get Hibernate not to make SQL Joins to query associated cached entities
      - and, perhaps, to cache entire tables for some few entities

      ?

      Thank you very very much!

      Marcus.

        • 1. Re: Caching Entity Beans
          Emmanuel Bernard Master

          set the assoc as lazy or hibernate will do a outer join to load them
          set @Cache
          set your cache_provider (see the Hibernate ref doc)
          set you cache provider configuration (depends on the one you've chosen)

          • 2. Re: Caching Entity Beans
            Marcus Schmidke Newbie

             

            "epbernard" wrote:
            set the assoc as lazy or hibernate will do a outer join to load them
            set @Cache
            set your cache_provider (see the Hibernate ref doc)
            set you cache provider configuration (depends on the one you've chosen)


            Yes ... but: where can I read about configuring a cache? Hibernate Reference Documentation leaves me totally alone. In Chapter 20.2, it only tells that several types of cache exist.

            However, the only one of them which works without error messages is "Hashtable" (which seems to be the one which only lasts for one transaction). All the other types seem to require some kind of configuration, but the whole internet, at least google, does not tell me how to do this. For none of the types.

            Since I'm using JBoss, TreeCache seems to be the best choice, but I was not able to figure out anything about that secret treecache.xml file. (For example, have a look at http://wiki.jboss.org/wiki/Wiki.jsp?page=TreeCache).

            Marcus.


            • 3. Re: Caching Entity Beans
              Emmanuel Bernard Master

              Google "Jboss Cache", first link, then documentation ;-)
              http://www.jboss.org/products/jbosscache/docs