Spring使用hibernate缓存本机查询

时间:2013-08-10 12:16:31

标签: spring ehcache

我正在使用Spring 3.2和Hibernate 4.在我的DAO实现中,我想缓存本机SQL查询的结果。获取此查询结果的方法如下所示:

public List<Object[]> getBestSellers(String category)
{
    Session session = sessionFactory.getCurrentSession();

    Query query = session.createSQLQuery( "SELECT i_id, i_title, a_fname, a_lname , SUM(ol_qty) AS val " +
            "FROM " +
            "orders, order_line, item, author " +
            "WHERE " +
            "order_line.ol_o_id = orders.o_id AND item.i_id = order_line.ol_i_id " +
            "AND item.i_subject = :category AND item.i_a_id = author.a_id GROUP BY i_id " +
            "ORDER BY orders.o_date, val DESC" );

    query.setParameter( "category", category );
    query.setMaxResults( 50 );
    query.setCacheable( true );

    List<Object[]> res = query.list();

    return res;
}

这似乎不起作用,我不知道为什么。

我在applicationContext.xml中配置了Hibernate,如下所示:

<props>
    <prop key="hibernate.jdbc.batch_size">50</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
    <prop key="hibernate.max_fetch_depth">4</prop>
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>    
    <prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop> 
    <prop key="hibernate.generate_statistics">true</prop>
</props>

和我的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">    
    <diskStore path="java.io.tmpdir/Cloudscale-cache"/>
    <defaultCache 
        eternal="false" 
        maxElementsInMemory="1000"
        overflowToDisk="false" 
        diskPersistent="false" 
        timeToIdleSeconds="0"
        timeToLiveSeconds="600" 
        memoryStoreEvictionPolicy="LRU"
    />
    <cache 
        name="org.hibernate.cache.spi.UpdateTimestampsCache"
        maxElementsInMemory="50"
        eternal="false"
        timeToIdleSeconds="0"
        timeToLiveSeconds="86400"
        overflowToDisk="true"/>
    <cache 
        name="org.hibernate.cache.internal.StandardQueryCache"
        maxElementsInMemory="50"
        eternal="false"
        timeToIdleSeconds="0"
        timeToLiveSeconds="86400"
        overflowToDisk="true"/> 
</ehcache>

1 个答案:

答案 0 :(得分:0)

如果要缓存本机sql查询,则必须使用addScalar()
通过使用addScalar(); hibernate将尝试将sql查询的结果转换为单个命名列的返回对象,而不是实体。

修改您的查询,如下所示

 Query query = session.createSQLQuery( "SELECT i_id as a, i_title as b, a_fname as c, a_lname as d, SUM(ol_qty) AS val " +
            "FROM " +
            "orders, order_line, item, author " +
            "WHERE " +
            "order_line.ol_o_id = orders.o_id AND item.i_id = order_line.ol_i_id " +
            "AND item.i_subject = :category AND item.i_a_id = author.a_id GROUP BY i_id " +
            "ORDER BY orders.o_date, val DESC" ).addScalar("a").addScalar("b").addScalar("c").addScalar("d").addScalar("val");