سؤال

Hi have set 'cache true' for domain class which means cache is enabled

static mapping = {
    cache true
}

Now if I say DomianClass.get(1) -- it doesn't hit the database but when I use DomianClass.getAll() it hits the database.

Isn't DomianClass.getAll() fetches from cache? why does it hit the database everytime? Is there any option to fetch all the records from 2nd level cache?

هل كانت مفيدة؟

المحلول

It isn't possible for the hibernate 2nd level cache to determine if "all" exists in the cache or not. Let's walk through why with some examples.

Case #1

DomainClass.get(1) asks cache if 1 exists. If so, return it, if not hit the db, cache it and return it. Further requests for 1 from the cache will return from the cache.

Case #2

DomainClass.getAll() asks cache if "all" exists. The cache then has to think "What does all mean?" and the only logical response is "I don't know, but the database does."

Now in case #2 you /might/ be tempted to think that if you made a request for "all" then the cache could cache that response. However, that's a different kind of cache than the 2nd level cache for hibernate.

Put simply, the 2nd level cache is for instances, indexed by domain class and identity.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top