首页 文章

JPA和缓存本机SQL查询

提问于
浏览
2

是否存在在JPA中缓存本机sql查询的可能性?

我发现 :

JPA 2.0缓存API

JPA 2.0提供了一组标准查询提示,以允许刷新或绕过缓存 . 查询提示在两个枚举类CacheRetrieveMode和CacheStoreMode上定义 .

查询提示:

javax.persistence.cache.retrieveMode : CacheRetrieveMode
    BYPASS : Ignore the cache, and build the object directly from the database result.
    USE : Allow the query to use the cache. If the object/data is already in the cache, the cached object/data will be used.
javax.persistence.cache.storeMode : CacheStoreMode
    BYPASS : Do not cache the database results.
    REFRESH : If the object/data is already in the cache, then refresh/replace it with the database results.
    USE : Cache the objects/data returned from the query.

缓存提示示例

查询查询= em.createQuery(“从员工e中选择e”); query.setHint(“javax.persistence.cache.storeMode”,CacheStoreMode.REFRESH);

但它可能只适用于NATIVE查询...

1 回答

  • 1

    不,JPA缓存不适用于本机查询,因为JPA提供程序不会解析或理解本机查询 - 它们直接进入数据库 . 您可能需要的是语句缓存或查询结果缓存,它们是JPA未涵盖的不同概念,并且特定于您的JPA提供程序,请参阅http://eclipse.org/eclipselink/documentation/2.5/jpa/extensions/q_query-results-cache.htm以获取有关EclipseLink查询缓存的信息(尽管我不知道它适用于本机查询) .

相关问题