4 Replies Latest reply on Jun 19, 2008 3:05 PM by Vinicius Carvalho

    Hibernate Cache

    Vinicius Carvalho Newbie

      Hello there! I have an application scoped component where it queries a db. This is a registry for my application, so, I have a few records on the db, and they never change unless a complete re-start is done.

      So, I decided to use Hibernate Session instead of EntityManager (each new project, I start to believe that JPA is a crap and that portability talk just brings problems. We have NEVER changed an entity IMPL what's the point of using jpa anyway :) ) sorry for that.

      Ok, So I inject my Session (my beans are anotated with jpa anotations and hibernate, don't know if this works :( )

      Here's a snippet of my code:

           private Session session;
      public SearchModule getModule(String name){
                org.hibernate.Query query = session.createQuery("from SearchModule where name = :name");
                query.setParameter("name", name);
                return (SearchModule) query.uniqueResult();

      Ok, so I search for a given module, and it hits the db. On the next search for the same module, it also hits the db.

      I was expecting that the second time the db would not be queried. Am I missing something here?


        • 1. Re: Hibernate Cache
          João Bosco Monteiro Newbie

          Hi Vinicius,

          Did you enable query cache in hibernate.cfg.xml?
          Something like:
          hibernate.cache.usequerycache true

          Query cache only caches identifiers, not entire results, so you need second-level cache to avoid database queries.


          • 2. Re: Hibernate Cache
            Vinicius Carvalho Newbie

            Well Ive set it up on my persistence.xml I do have caching for both quries and objects on other projects, but those are 100% hibernate. The whole JPA is that's making me believe I cant get it working.

                     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
                     <property name="hibernate.hbm2ddl.auto" value="update"/>
                     <property name="hibernate.show_sql" value="true"/>
                     <property name="hibernate.format_sql" value="true"/>
                     <property name="jboss.entity.manager.factory.jndi.name" value="java:/PortalSearchEntityManagerFactory"/>
                     <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
                     <property name="hibernate.cache.use_query_cache" value="true"/>


            • 3. Re: Hibernate Cache
              João Bosco Monteiro Newbie

              Hi Vinicus,

              I use second-level cache and query cache with JPA plus Hibernate and it works fine.
              I know it could sound silly but review your configuration because you are injecting Hibernate Session and configuring an EntityManagerFactory using JNDI. Looks strange to me.

              Verify your components.xml and check if the Session component is configured correctly. I think maybe your injected Session is not using your persistence.xml.


              • 4. Re: Hibernate Cache
                Vinicius Carvalho Newbie

                Well, I have the session configured as a factory component as explained at the seam docs:

                <core:manager conversation-timeout="120000"
                    <persistence:managed-persistence-context name="entityManager"
                        <factory name="session" value="#{entityManager.delegate}"
                                       scope="stateless" auto-create="true"/>
                    --><security:identity authenticate-method="#{authenticator.authenticate}"/>

                So, my session is being created from my entityManager. I'm still looking for what could be the reason, but so far nothing.

                If only java had support for collections query, I could store my entire table (which may never get to 20 rows), and query the collection. Sometimes I have this strange feeling that C sharp is kicking java's ass :)