Hibernate query cache does not work

2.2k views Asked by At

Here are my code :

        SessionFactory sf = getSession().getSessionFactory();
        String query = "select r from RefEntity r where r.id in :ids";

        Session s1 = sf.openSession();
        s1.get(RefEntity.class, 1).getValue();
        s1.createQuery(query).setParameterList("ids", Arrays.asList(1, 2)).setCacheable(true).list();

        Session s2 = sf.openSession();
        s2.get(RefEntity.class, 1).getValue();
        s2.createQuery(query).setParameterList("ids", Arrays.asList(1, 2)).list();

Console :

Hibernate: 
    select
        refentity0_.id as id1_6_0_,
        refentity0_.value as value2_6_0_ 
    from
        RefEntity refentity0_ 
    where
        refentity0_.id=?
Hibernate: 
    /* select
        r 
    from
        RefEntity r 
    where
        r.id in :ids */ select
            refentity0_.id as id1_6_,
            refentity0_.value as value2_6_ 
        from
            RefEntity refentity0_ 
        where
            refentity0_.id in (
                ? , ?
            )
Hibernate: 
    /* select
        r 
    from
        RefEntity r 
    where
        r.id in :ids */ select
            refentity0_.id as id1_6_,
            refentity0_.value as value2_6_ 
        from
            RefEntity refentity0_ 
        where
            refentity0_.id in (
                ? , ?
            )

2nd level cache works, but I do not understand why query cache does not. Who can explain what's going on?? I will appreciate it, thx in advance :)

My entity :

@Table
@Entity
@Getter
@Setter
@ToString
@Cacheable
@org.hibernate.annotations.Cache(region = "dictionaryCache", usage = CacheConcurrencyStrategy.READ_ONLY)
public class RefEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ref_seq")
    @SequenceGenerator(name = "ref_seq", sequenceName = "ref_seq", allocationSize = 1)
    @Column(name = "id")
    private Integer id;

    @Column(name = "value")
    private String value;
}

persistence-hidden.xml :

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="TEST">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.use_sql_comments" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>

            <!-- cache -->
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
            <property name="hibernate.cache.use_query_cache" value="true"/>
            <property name="hibernate.cache.use_second_level_cache" value="true"/>
            <property name="net.sf.ehcache.configurationResourceName" value="ehcache.xml"/>
            <property name="hibernate.cache.region.factory_class"
                      value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
        </properties>

    </persistence-unit>
</persistence>

ehcache.xml :

<cache name="dictionaryCache"
       maxElementsInMemory="10000"
       eternal="true"
       timeToIdleSeconds="600"
       timeToLiveSeconds="3600"
       overflowToDisk="false">
</cache>

<cache name="org.hibernate.cache.internal.StandardQueryCache"
       maxElementsInMemory="500"
       eternal="false"
       timeToIdleSeconds="600"
       timeToLiveSeconds="3600"/>

<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
       maxElementsInMemory="50"
       eternal="true"/>

maven dependecies:

        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.6.9</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.0.0.Final</version>
        </dependency>
0

There are 0 answers