Hibernate查询缓存不起作用

时间:2016-02-19 20:07:33

标签: java hibernate ehcache hibernate-cache

以下是我的代码:

For Each cel In Intersect(Sheets(1).Range("F:F"), Sheets(1).UsedRange)
    If cel.Font.Underline = xlUnderlineStyleSingle Then
        cel.Value = "x" & cel.Value
    End If
Next

For Each cel In Intersect(Sheets(1).Range("H:H"), Sheets(1).UsedRange)
    If cel.Font.Underline = xlUnderlineStyleSingle Then
        cel.Value = "x" & cel.Value
    End If
Next

控制台:

        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();

二级缓存有效,但我不明白为什么查询缓存没有。谁能解释发生了什么?我会很感激,提前:)

我的实体:

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 (
                ? , ?
            )

persistence-hidden.xml:

@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;
}

ehcache.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>

maven dependecies:

<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"/>

0 个答案:

没有答案