Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7671521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:04:43+00:00 2026-05-31T16:04:43+00:00

I configured Second Level Cache using Ehcache in hibernate.cfg.xml ,ehcache.xml.And setting the cache-usage property

  • 0

I configured Second Level Cache using Ehcache in hibernate.cfg.xml ,ehcache.xml.And setting the cache-usage property in mapping files.And tyring to check the data is loaded from cache or db using hibenrate statices.But its not loaded.Its again execute the query.I mentioned code

<hibernate-configuration>
<session-factory>
    <property name="connection.username">pension2</property>
    <property name="connection.password">pension2</property>
    <property name="connection.url">jdbc:oracle:thin:@191.161.0.25:1521:pension</property>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="myeclipse.connection.profile">pension</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="use_sql_comments">true</property>

    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.generate_statistics">true</property>
    <property name="hibernate.cache.region.provider_class">
         net.sf.ehcache.hibernate.EhCacheProvider</property>
    <property name="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml </property>      
    <mapping resource="com/aims/mapping/Teacher.hbm.xml" />
    <mapping resource="com/aims/mapping/Student.hbm.xml" />
    <mapping resource="com/aims/mapping/Student_marks_detl.hbm.xml" />
    <mapping resource="com/aims/mapping/User.hbm.xml" />

</session-factory>
</hibernate-configuration>

ehcache.xml

    <ehcache> 
    <diskStore path="java.io.tmpdir"/> 
     <cache name="com.aims.beans.Teacher" 
    maxElementsInMemory="300" 
    eternal="false" 
    overflowToDisk="false"/> 
    </ehcache>

Mapping.xml

<hibernate-mapping>
<class name="com.aims.beans.Teacher" table="teacher">
<cache usage="read-write" />
<id name="tno" column="tno" type="java.lang.Integer" >
    <generator class="assigned" />
</id>
<property name="tname" type="java.lang.String" column="tname"/>
</class> 
</hibernate-mapping>

I trying to load teachers list in my jsp.So,Iam using createquery and setCacheable is true.

long oldHitCount = HibernateUtil.getHitCount();
long oldMissCount = HibernateUtil.getMissCount();
log.info(“oldHitCount” +oldHitCount + “oldMissCount”+ oldMissCount );
Query q = session.createQuery(“from Teacher”);
q.setCacheable(true);
list = q.list();
long newHitCount = HibernateUtil.getHitCount();
long newMissCount= HibernateUtil.getMissCount();

HibernateUtil.getHitCount()/HibernateUtil.getMissCount() Code

public static long getHitCount() {
        long hitcount = 0;
        hitcount = sessionFactory.getStatistics()
                .getSecondLevelCacheStatistics("com.aims.beans.Teacher")
                .getHitCount();
        return hitcount;
    }

    public static long getMissCount() {
        long miscount = 0;
        miscount = sessionFactory.getStatistics()
                .getSecondLevelCacheStatistics("com.aims.beans.Teacher")
                .getMissCount();
        return miscount;
    }

But every time execute the createQuery.I configured and every thing why its not returned from the cache.Is there any mistake to configure the second level cache.Please help me>?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:04:45+00:00Added an answer on May 31, 2026 at 4:04 pm

    The second level cache maps ids to entities, so it is only used when entities are queried by id. The query cache maps queries to a set of entity ids retrieved by the query. So the second level cache and query cache are actually only helpful when used together.

    To enable the query cache it is not enough to set the query cacheable but you must also enable the query cache in your Hibernate session-factory configuration:

    <property name="hibernate.cache.use_query_cache">true</property>
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I get Spring to load Hibernate's properties from hibernate.cfg.xml ? We're using
I am trying to configure the hibernate.cfg.xml file to use memcache. I see the
Hi I've run into some problems with hibernate 2nd level cache. As cache provider
I have flex application consisting of several modules which is configured using maven. I'm
I have an application where a property placeholder is used to read properties, configured
Let's say I have a Windows Forms timer configured with a 10 second (10k
Hello all: I'm building an app using C#/MVC3/Razor which has a database configured in
I have two filter classes extending from org.springframework.web.filter.RequestContextFilter , both configured in the web.xml
I have two rules configured in NLog.config : <logger name=Model level=Info writeTo=modelLog final=true />
I configured IntelliJ IDEA 6.0.6 to use Subversion for version control. The Subversion menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.