鑑於兩行代碼:JPA - Hibernate Session被關閉,但EntityManager的工作
entityManager.find(MyEntity.class, myId);
((Session) getEntityManager().getDelegate()).load(MyEntity.class, myId);
第一條指令工作正常,但第二個罰球org.hibernate.SessionException:會話被關閉!任何想法 ?
語境:春季批次3.0.6/3.5.0冬眠
信息:EntityManager的注入:
@PersistenceContext
protected EntityManager entityManager;
更新1
我可以在查找方法的調用見到this.getSession(),如果會話爲空,則打開新會話(在EntityManagerImpl.getRawSession中)
更新2
在第二種情況中的getSession方法不叫,但一類叫做SharedEntityManagerCreator是關閉會話:
if(isNewEm) {
EntityManagerFactoryUtils.closeEntityManager(target1);
}
你是否在同一個流程中逐一調用上述兩行?如果你交換它們會發生什麼? –
我的目標是僅使用hibernate會話,因爲我需要Query類的滾動方法。所以這是一個簡單的測試。我在不同的測試用例中運行每條指令。 – fego