使用 EntityManager 从 EJB 访问 Hibernate 会话
是否可以从 EntityManager 获取 Hibernate Session 对象?我想访问一些 hibernate 特定的 API...
我已经尝试过类似的操作:
org.hibernate.Session hSession =
( (EntityManagerImpl) em.getDelegate() ).getSession();
但是一旦我调用 EJB 中的方法,我就会得到“在 EJB 上调用期间发生系统异常”并出现 NullPointerException
我使用 glassfish 3.0.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Bozho 和 partenon 是正确的,但是:
在 JPA 2 中,首选机制是 entityManager.unwrap(类)
我认为你的异常是因为你试图转换为实现类而引起的(也许你正在处理 JDK 代理)。转换为接口,一切都应该没问题(在 JPA 2 版本中,不需要转换)。
Bozho and partenon are correct, but:
In JPA 2, the preferred mechanism is entityManager.unwrap(class)
I think your exception is caused because you are trying to cast to an implementation class (perhaps you were dealing with a JDK proxy). Cast to an interface, and everything should be fine (in the JPA 2 version, no casting is needed).
从 Hibernate EntityManager 文档来看,首选的方法是:
From Hibernate EntityManager docs, the preferred way of doing it is:
很简单:
As simple as:
如果您的
EntityManager
已正确注入(使用@PersistenceContext
)并且不为 null,则以下内容应该有效:If your
EntityManager
is properly injected (using@PersistenceContext
) and is not null, then the following should work: