【问题标题】:How does Hibernate handle caching with NativeSQL calls?Hibernate 如何处理 NativeSQL 调用的缓存?
【发布时间】:2014-06-21 04:40:26
【问题描述】:

我需要在不使用我的 Hibernate 实体的情况下运行 NativeQuery 插入命令。但我担心这将如何影响可能完成的任何缓存(L1 / L2)。从我在 blogs/etc 中读到的内容来看,Hibernate 似乎会在遇到任何 NativeQuery 调用时自动刷新所有缓存。

这是真的吗?知道我正在使用本机 sql 调用操作哪些表,我是否能够禁用 Hibernate 的自动刷新并具体控制要刷新的内容和维护的内容?这可以通过注释来完成,还是必须通过调用 EntityManagerFactory.getCache().evict() 来完成?

例如:

entityManager.createNativeQuery(
    "INSERT INTO JOIN_TABLE (CONTRACT_ID, ATTACHMENT_ID ) VALUES( ?, ? )"
    .setParameter(1, 13)
    .setParameter(2, 25) 
    .executeUpdate();

默认情况下,无论我是否请求,hibernate 似乎都会刷新我的缓存。

2014-06-20 15:00:56,964 [http-bio-8880-exec-3] [127.0.0.1-5385277c1ecbbf5a] INFO  org.hibernate.engine.internal.StatisticalLoggingSessionEventListener - Session Metrics {
    70000 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    2731000 nanoseconds spent preparing 21 JDBC statements;
    11467000 nanoseconds spent executing 21 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    28387000 nanoseconds spent executing 2 flushes (flushing a total of 18 entities and 17 collections);
    674000 nanoseconds spent executing 2 partial-flushes (flushing a total of 2 entities and 2 collections)
}

【问题讨论】:

    标签: java hibernate caching jpa


    【解决方案1】:

    您可以通过 Query.setFlushMode 控制每个查询对象的刷新模式类型。

    “持久化提供者负责确保对持久化上下文中所有实体的状态的所有更新,这些更新可能会影响查询的结果,对查询的处理是可见的。持久化提供者的实现可以通过刷新来实现这些实体到数据库或通过其他方式。”

    看看FlushModeType

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      相关资源
      最近更新 更多