簡體   English   中英

彈簧注入的EntityManager的訪問會話

[英]Accessing session of spring injected EntityManager

在我的應用程序中,我已經通過Spring注入了EntityManager。 如何訪問EntityManager會話? 當我嘗試這樣做時:

Session session = ((EntityManagerImpl) entityManager).getActiveSession();

我遇到了強制轉換異常,因為spring注入了entityManager的代理。 請讓我知道該怎么做。

注意:我正在使用Eclipselink JPA。

謝謝。

您應該使用EntityManager的unwrap方法來訪問特定於提供程序的實現。

PS:我在EclipseLink javadoc中找不到任何EntityManagerImpl。 它在哪里?

您可以按照@JBNizet的建議使用unwrap 但是,這要求您使用符合JPA 2.0的API。 但是,如果您不得不使用JPA 1.0,請嘗試JpaHelper類 它的getEntityManager方法可用於獲取活動會話。

JpaHelper.getEntityManager(entityManager).getActiveSession()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM