将entityManager注入自定义Hibernate Interceptor类(Inject entityManager into custom Hibernate Interceptor cla

编程入门 行业动态 更新时间:2024-10-27 21:23:54
将entityManager注入自定义Hibernate Interceptor类(Inject entityManager into custom Hibernate Interceptor class)

我想在Hibernate拦截器类上注入Hibernate中的entityManager。 我正在使用EJB和JBoss。 事务是JTA,提供者是org.hibernate.ejb.HibernatePersistence。

我试着这样做:

@Stateless(name = "HistoricInterceptor") @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public class HistoricInterceptorImpl extends EmptyInterceptor implements HistoricInterceptor { @PersistenceContext(name = "windi") private EntityManager em; // overriden methods, etc }

但是对entityManager的引用始终为null。

这种行为有望吗? 如何从Interceptor类中访问entityManager?

I want to inject the entityManager from Hibernate on an Hibernate Interceptor Class. I'm using EJBs and JBoss. The transaction is JTA and the provider is the org.hibernate.ejb.HibernatePersistence.

I tried to do it like that:

@Stateless(name = "HistoricInterceptor") @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public class HistoricInterceptorImpl extends EmptyInterceptor implements HistoricInterceptor { @PersistenceContext(name = "windi") private EntityManager em; // overriden methods, etc }

But the reference to entityManager is always null.

Is this behaviour expected? How can I access an entityManager from within an Interceptor class?

最满意答案

我放弃了那个确切的解决方案。 相反,我创建了另一个具有EJB Annotations的类。 拦截器将通过使用在应用程序上下文中查找EJB类的提供程序类来调用该EJB。 在那里,entityManager正确关联。

I had given up on that exact solution. Instead, I've created another class that has the EJB Annotations. The interceptor will call that EJB by using a provider class that lookup up EJB classes on the context of the application. There, the entityManager is correctly associated.

更多推荐

本文发布于:2023-08-04 14:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   Hibernate   entityManager   Interceptor   class

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!