You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate调用存储过程后无法读取新创建行的解决方案咨询

Hey, I’ve run into this exact scenario multiple times—super frustrating when Hibernate’s Session cache doesn’t pick up changes made directly via stored procedures! Since you don’t have a specific entity instance to use session.refresh() on, here are several practical solutions to fix this:

1. Clear the Session’s First-Level Cache

The simplest fix is to call session.clear() (or entityManager.clear() if you’re using JPA). This wipes out all cached entities in the Session, forcing Hibernate to hit the database directly for your subsequent query.

⚠️ A quick heads-up: This clears the entire Session cache, so make sure any pending changes you want persisted have already been committed (like your stored procedure execution) before running this.

Example code:

// Execute your stored procedure to create rows in Table X
callYourStoredProcedure();

// Clear the Session cache to discard stale data
session.clear();

// Now your query will fetch fresh data from the database
List<XEntity> newEntries = session.createQuery("FROM XEntity WHERE ...", XEntity.class)
    .getResultList();

2. Force Your Query to Bypass the First-Level Cache

If clearing the entire cache feels too heavy, you can instruct Hibernate to skip the first-level cache for your specific query. Use Hibernate’s query hints to achieve this:

For Hibernate Session API:

List<XEntity> newEntries = session.createQuery("FROM XEntity WHERE ...", XEntity.class)
    .setHint("org.hibernate.cacheMode", CacheMode.IGNORE)
    .getResultList();

For JPA EntityManager:

List<XEntity> newEntries = entityManager.createQuery("SELECT x FROM XEntity x WHERE ...", XEntity.class)
    .setHint("javax.persistence.cache.storeMode", CacheStoreMode.BYPASS)
    .getResultList();

CacheMode.IGNORE (or CacheStoreMode.BYPASS for JPA) tells Hibernate to ignore the cached data entirely—your query will pull directly from the database, and won’t store the results in the cache either. Perfect for this use case.

3. Use Hibernate’s Native Query to Execute the Stored Procedure

If you were using raw JDBC to run the stored procedure, switching to Hibernate’s native query API can help align your operations with Hibernate’s state management. While it won’t auto-refresh the cache, it keeps your code consistent and makes it easier to follow up with cache adjustments:

// Execute stored procedure via Hibernate's native query
session.createNativeQuery("CALL your_stored_procedure(:param1, :param2)")
    .setParameter("param1", value1)
    .setParameter("param2", value2)
    .executeUpdate();

// Follow up with either session.clear() or a cache-bypassing query
session.clear();
List<XEntity> newEntries = session.createQuery("FROM XEntity WHERE ...", XEntity.class)
    .getResultList();

4. Adjust Transaction Isolation Level (Edge Case)

In rare cases, if your transaction is using a stricter isolation level like REPEATABLE_READ, it might be preventing Hibernate from seeing the new rows. Switching to READ_COMMITTED (the default for most databases) can resolve this, though this is less likely to be the root cause than cache issues.

You can set this directly on your @Transactional annotation:

@Transactional(isolation = Isolation.READ_COMMITTED)
public void yourMethod() {
    // Your stored procedure call and query logic here
}

内容的提问来源于stack exchange,提问作者Richard Silvertass

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:20:37