我想在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.
更多推荐
发布评论