Pergunta

I'm not sure if it's a real problem or it's only a matter of configuration but I can see on my log console how hibernate hits (or at least throws the select query) even on cache hits.

I've checked the cache is properly working on Ehcache monitor and it registers 100% of hits for a certain request. But I always see the queries in the log.

All entities are anotated as shown:

@Entity
@Cacheable
@Cache(usage = READ_WRITE)
@Table(name = "city")
//@NamedQuery(name = "city.findById", query = "from City where ID = :id")
public class City extends Audit implements Serializable {

My ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    dynamicConfig="true" monitoring="autodetect">

    <!-- Location of persistent caches on disk -->
    <diskStore path="java.io.tmpdir/MxlServiceLayer" />

    <cacheManagerPeerListenerFactory
        class="org.terracotta.ehcachedx.monitor.probe.ProbePeerListenerFactory"
        properties="monitorAddress=localhost, monitorPort=9889, memoryMeasurement=true" />
    <defaultCache eternal="false" maxElementsInMemory="1000"
        overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
        timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" statistics="true" />
    <cache name="authToken" eternal="false" maxElementsInMemory="100"
        overflowToDisk="false" diskPersistent="true" timeToIdleSeconds="0"
        timeToLiveSeconds="31536000" memoryStoreEvictionPolicy="LRU"
        statistics="true" />
</ehcache>

And what I see in my over and over again...

Hibernate: select city0_.ID as ID2_, city0_.CREATED as CREATED2_, city0_.CREATOR as CREATOR2_, city0_.MODIFIED as MODIFIED2_, city0_.MODIFIER as MODIFIER2_, city0_.NAME as NAME2_, city0_.state_fk as state7_2_ from city city0_ where State_fk=?

Is hibernate really hitting the DB? Can anyone explain this to me, please?

I'm using:

JPA 2.0 in Spring Data JPA 1.2.0

Ehcache 2.6.0

Spring 3.2.1

Foi útil?

Solução

What you enabled is the second-level cache. This cache caches the state of the entities, and indexes them by their ID. It's like a Map<ID, EntityState>. This cache is only used if you get the entity by ID:

  • by calling session.get()
  • by calling session.load()
  • by navigating through a XxxToOne association targeting the entity

Your query doesn't fall into this category: it looks for your entity by one of its fields, and not by its ID.

For the rest, Hibernate can't execute arbitrary SQL queries against the cache, and even if it could, the cache only contains a subset of the table, so it has to query the database.

You can however, cache the result of queries as well. You'll need to enable the query cache to do so, and to mark your query as cacheable. You can also cache an association (which seems like the reason for this query: it's looking for all the cities of a given state). You'll have to annotate the association with @Cache.

Read the documentation.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top