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 6592543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:34:04+00:00 2026-05-25T17:34:04+00:00

I have questions on the statements I found while going through hibernate caching tutorial.

  • 0

I have questions on the statements I found while going through hibernate caching tutorial.

Statement1:-First-level cache always Associates with the Session object. Hibernate uses this cache by default.

Question1:–
As this cache is by default holding all the results from session methods like load,get etc. Wont it be a major memory issue because of holding all the objects in seesion ?

Question2:–
Do we need to enable this cache or its there by default? Can I disable First level cache, if I want? If yes how?

Below questions are for both first level and second level cache

Question3:-
If I get the same object from cache(say I load the object customer with customerid 1 twice in the same session)both objects returned from cache will be having different references from the object lying in cache. Right? Because if I update some field in customer object, it should not reflect in cache until and unless save/update method is called.

Question4:-
As per my understanding if we try to get the customer object from id 1 second time from this cache, it will return the same old object even if it got updated in between. How can we ensure if it got updated it reads from database otherwise from cache? I guess we can use read write cache strategy with class like EHCache

Question5:- On Query Caching
I read this statement :-Updates in the queries occur very often. So, for query caching, two cache regions are necessary.

For storing the results.( cache identifier values and results of value type only).
For storing the most recent updates.

I think “Updates in the queries occur very often” mean here we usually change some parameter value like select * from customer where custid=? So id will be changed for different customer id. So query cache will keep each query (along with parameter ) and results returned by each query.

Right? But not sure why do we need two cache region for query caching?

  • 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-25T17:34:05+00:00Added an answer on May 25, 2026 at 5:34 pm

    AFAIK
    Answer 01:

    Hibernate caches the objects loaded so as to avoid unwanted access to database. The simplest way to avoid a persistance context would be to use a StatelessSession. A Stateless Hibernate Session has no dirty checking responsibility and hence goes well with your requirement of not having the first level cache(Persistant Context).

    Answer 02:

    1. You don’t have to enable first level cache

    Answer 03

    1. If an object is loaded twice in the same session using its identity, Hibernate makes sure that the same object is returned regardless of whether the second level cache is enabled. Let me clarify that second level cache DOES NOT cache instances of object. It maintains a serialized (not exactly) version of the instance.
      The following link gives a brief explanation of structure of hibernate second level cache.

    I am sorry but I fail to understand the concept behind "updating customer object and it not being reflected in the cache". I hope the link clarifies your question.

    Answer 04

    1. Problems of stale data fetched from cache do arise if not configured correctly.
      State data will be fetched if the expiration time on the cache has not occurred. Eviction can be carried out using Session.evict() however chances are you might not have a clear idea of what data needs to evicted. Hence caching should always be the final step in optimizing your access to database.
      Good level of optimization can be achieved by re-evaluating the associations and fetch-plans on collection relations so as to avoid N+1 selection issues and at the same time avoid.

      EHCache provides read-write caching stratergies for data which does not get updated
      that conforms to transaction read committed protection. That is it avoid dirty reads but cannot help against repeatable reads.

    Check out EHCache configuration here

    Answer 05 : Query Caching

    1. Check Out these links for better understanding of Session Cache and Query Cache
      A. Session Cache
      B. Second Level Cache
      C. Query Cache

    2. Checkout this link which gives a brief/simpler explanation of structure of query cache.

    Hope this helps.

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

Sidebar

Related Questions

I have two questions. 1) Should you always use a using statement on a
I am going to be making a small user system but I have questions.
I have been coding in Java(Mainly) and .Net for a while. What I found
I have just found a pretty major vulnerability in my code while doing some
simple question :- i have the following simple if (..) statements :- if (foo
I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
Similar questions have been asked, but nothing exactly like mine, so here goes. We
similar questions have been asked before but I cant find an exact match to
I have some questions about basic CSS that I was unable to understand or
I have some questions about customers about NF mode for DB2. Google had very

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.