0

我正在使用带有 Hibernate 4 的 Spring 3.2。在我的 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>
4

1 回答 1

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");
于 2013-08-10T12:38:58.243 回答