修改Hibernate的QueryImpl.class的工作原理

时间:2013-11-19 21:31:05

标签: java spring hibernate jpa

我有一个缓存,其中包含我的数据库中找到的数据子集。我希望Hibernate首先检查缓存而不是直接查询数据库。

例如,在getResultList()中,我想首先检查缓存。有命中时,构造对象列表并返回DAO。否则,查询数据库为Hibernate。

我的问题是如何配置Hibernate,以便它使用我的函数getResultList()实现。

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "unchecked", "RedundantCast" })
public List<X> getResultList() {
    try {


        // CHECK EXTERNAL CACHE HERE *************************
        // start pseudo code...
        if (found in cache)
           get data from cache
           put data into a collection
           return collection
        else
           return (List<X>) query.list();
        // end pseudo code...


    }
    catch (QueryExecutionRequestException he) {
        throw new IllegalStateException(he);
    }
    catch( TypeMismatchException e ) {
        throw new IllegalArgumentException(e);
    }
    catch (HibernateException he) {
        throw getEntityManager().convert( he );
    }
}

0 个答案:

没有答案