Seam3/Weld - 找不到实体管理器

发布于 2024-11-06 07:22:09 字数 7548 浏览 0 评论 0原文

我有一个项目(EJB 3.0,打包为 EAR),带有视图(JSF 2.0)和持久性(JPA 2.0,Hibernate)层。为了避免延迟初始化支持和其他问题,我尝试在 Seam 3.0.0.Final 中引入 Seam 管理的持久性上下文。 为此,我编写了以下生成器类:

import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

import org.jboss.seam.solder.core.ExtensionManaged;

class PersistenceProvider {
    @ExtensionManaged
    @Produces
    @PersistenceUnit
    @ConversationScoped
    private EntityManagerFactory producerField;
}

并将 EntityManager 字段上的 @PersistenceContext 注释更改为 @Inject。

如果我现在尝试使用 EntityManager,我将得到以下堆栈跟踪:

<WARNUNG: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.el.EvaluationException: java.lang.RuntimeException: java.lang.RuntimeException: Could not find EntityManagerFactory bean with qualifiers[@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()]
 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
 at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
 at javax.faces.component.UICommand.broadcast(UICommand.java:315)
 at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
 at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
 at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
 at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
 at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
 at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
 at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
 at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
 at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
 at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
 at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
 at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
 at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
 at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
 at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
 at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
 at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
 at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
 at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
 at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
 at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
 at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
 at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Could not find EntityManagerFactory bean with qualifiers[@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()]
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.create(ManagedPersistenceContextBeanLifecycle.java:126)
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.create(ManagedPersistenceContextBeanLifecycle.java:46)
 at org.jboss.seam.solder.bean.ImmutableBean.create(ImmutableBean.java:87)
 at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:121)
 at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
 at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:124)
 at org.jboss.weld.proxies.SMPC-org$jboss$seam$persistence$ManagedPersistenceContextExtension-de$wintersolutions$fitnessbuddy$persistence$cdi$PersistenceProvider$producerField[@javax$enterprise$context$ConversationScoped()@javax$enterprise$inject$Produces()@javax$persistence$PersistenceUnit(name=,unitName=)@org$jboss$seam$solder$core$ExtensionManaged()]_$$_WeldClientProxy.persist(SMPC-org$jboss$seam$persistence$ManagedPersistenceContextExtension-de$wintersolutions$fitnessbuddy$persistence$cdi$PersistenceProvider$producerField[@javax$enterprise$context$ConversationScoped()@javax$enterprise$inject$Produces()@javax$persistence$PersistenceUnit(name=,unitName=)@org$jboss$seam$solder$core$ExtensionManaged()]_$$_WeldClientProxy.java)
 at de.wintersolutions.fitnessbuddy.view.controller.ExerciseController.saveExercise(ExerciseController.java:54)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:737)
 at javax.el.BeanELResolver.invoke(BeanELResolver.java:467)
 at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:254)
 at com.sun.el.parser.AstValue.invoke(AstValue.java:228)
 at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
 at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
 at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
 at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
 ... 33 more
Caused by: java.lang.RuntimeException: Could not find EntityManagerFactory bean with qualifiers[@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()]
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.getEntityManagerFactory(ManagedPersistenceContextBeanLifecycle.java:210)
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.create(ManagedPersistenceContextBeanLifecycle.java:104)
 ... 53 more

在 Glassfish 3.1 上调试应用程序为我提供了以下见解

实体管理器是类型的代理

entityManager SMPC-org$jboss$seam$persistence$ManagedPersistenceContextExtension-com$example$PersistenceProvider$producerField[@javax$enterprise$context$ConversationScoped()@javax$enterprise$inject$Produces()@javax$persistence$PersistenceUnit(name=,unitName=)@org$jboss$seam$solder$core$ExtensionManaged()]_$$_WeldClientProxy

The Producer 字段无法在 beans-Field 中找到BeanManager 的范围位于方法 org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.getEntityManagerFactory 的范围内,但位于注入到我的 DAO 中的 BeanManager 中。

我尝试了一些变体,例如限定生产者字段,尝试在视图层服务中注入实体管理器,并将持久单元的unitName和/或名称添加到生产者字段的@PersistenceUnit注释中。

有什么建议我在这里做错了什么吗?

I have a project (EJB 3.0, packaged as EAR) with a view (JSF 2.0) and persistence (JPA 2.0, Hibernate) layer. To avoid the lazy initialization support and other issues I tried to introduce a Seam Managed Persistence Context with Seam 3.0.0.Final.
For this i wrote the following producer class:

import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

import org.jboss.seam.solder.core.ExtensionManaged;

class PersistenceProvider {
    @ExtensionManaged
    @Produces
    @PersistenceUnit
    @ConversationScoped
    private EntityManagerFactory producerField;
}

And changed the @PersistenceContext Annotation on my EntityManager fields to @Inject.

If Iam trying to use the EntityManager now I'm getting the following stacktrace:

<WARNUNG: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
javax.faces.el.EvaluationException: java.lang.RuntimeException: java.lang.RuntimeException: Could not find EntityManagerFactory bean with qualifiers[@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()]
 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
 at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
 at javax.faces.component.UICommand.broadcast(UICommand.java:315)
 at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
 at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
 at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
 at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 at javax.faces.webapp.FacesServlet.service(FacesServlet.java:409)
 at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
 at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
 at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
 at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
 at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
 at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
 at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
 at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
 at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
 at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
 at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
 at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
 at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
 at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
 at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
 at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
 at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
 at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
 at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
 at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Could not find EntityManagerFactory bean with qualifiers[@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()]
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.create(ManagedPersistenceContextBeanLifecycle.java:126)
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.create(ManagedPersistenceContextBeanLifecycle.java:46)
 at org.jboss.seam.solder.bean.ImmutableBean.create(ImmutableBean.java:87)
 at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:121)
 at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
 at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:124)
 at org.jboss.weld.proxies.SMPC-org$jboss$seam$persistence$ManagedPersistenceContextExtension-de$wintersolutions$fitnessbuddy$persistence$cdi$PersistenceProvider$producerField[@javax$enterprise$context$ConversationScoped()@javax$enterprise$inject$Produces()@javax$persistence$PersistenceUnit(name=,unitName=)@org$jboss$seam$solder$core$ExtensionManaged()]_$_WeldClientProxy.persist(SMPC-org$jboss$seam$persistence$ManagedPersistenceContextExtension-de$wintersolutions$fitnessbuddy$persistence$cdi$PersistenceProvider$producerField[@javax$enterprise$context$ConversationScoped()@javax$enterprise$inject$Produces()@javax$persistence$PersistenceUnit(name=,unitName=)@org$jboss$seam$solder$core$ExtensionManaged()]_$_WeldClientProxy.java)
 at de.wintersolutions.fitnessbuddy.view.controller.ExerciseController.saveExercise(ExerciseController.java:54)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:737)
 at javax.el.BeanELResolver.invoke(BeanELResolver.java:467)
 at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:254)
 at com.sun.el.parser.AstValue.invoke(AstValue.java:228)
 at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
 at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
 at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
 at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
 ... 33 more
Caused by: java.lang.RuntimeException: Could not find EntityManagerFactory bean with qualifiers[@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()]
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.getEntityManagerFactory(ManagedPersistenceContextBeanLifecycle.java:210)
 at org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.create(ManagedPersistenceContextBeanLifecycle.java:104)
 ... 53 more

Debugging the applicaton on Glassfish 3.1 provided me with the following insights

The entity manager is a proxy of the type

entityManager SMPC-org$jboss$seam$persistence$ManagedPersistenceContextExtension-com$example$PersistenceProvider$producerField[@javax$enterprise$context$ConversationScoped()@javax$enterprise$inject$Produces()@javax$persistence$PersistenceUnit(name=,unitName=)@org$jboss$seam$solder$core$ExtensionManaged()]_$_WeldClientProxy

The producer field can't be found in the beans-Field of the BeanManager in the scope of the method org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.getEntityManagerFactory but in a BeanManager injected into my DAOs.

I tried some variations like qualifiying the producer field, trying to inject a entity manager in the view layer services and adding the unitName and/or name of my persistence unit to the @PersistenceUnit Annotation of the producer field.

Any suggestions what Im doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-11-13 07:22:09

似乎 EJB 的 MDB 中没有活动的会话(或会话)范围。

您需要手动激活一个。您可以通过使用以下 messageHandler 实现 MDB 来完成此操作:

@Inject
private BoundConversationContext boundConversationContext;

public void onMessage(Message message) {
    BoundRequest boundRequest = null;
    try {
        boundRequest = new MutableBoundRequest(new HashMap<String, Object>(), new HashMap<String, Object>());
        boundConversationContext.associate(boundRequest);
        if (!boundConversationContext.isActive())
            boundConversationContext.activate();
        // DO YOUR BUSINESS LOGIC HERE
    } finally {
        if (boundConversationContext != null) {
            if (boundRequest != null)
                boundConversationContext.dissociate(boundRequest);
            if (boundConversationContext.isActive()) {
                boundConversationContext.deactivate();
            }
        }
    }   
}

当我尝试将 MDB 与 Seam 持久性集成时,这对我在 Jboss 6 AS 上有用。

Seems like when there is no Conversation (or Session) scope active in EJB's MDB.

You need manually active one. You can do it by implementing your MDB with the following messageHandler:

@Inject
private BoundConversationContext boundConversationContext;

public void onMessage(Message message) {
    BoundRequest boundRequest = null;
    try {
        boundRequest = new MutableBoundRequest(new HashMap<String, Object>(), new HashMap<String, Object>());
        boundConversationContext.associate(boundRequest);
        if (!boundConversationContext.isActive())
            boundConversationContext.activate();
        // DO YOUR BUSINESS LOGIC HERE
    } finally {
        if (boundConversationContext != null) {
            if (boundRequest != null)
                boundConversationContext.dissociate(boundRequest);
            if (boundConversationContext.isActive()) {
                boundConversationContext.deactivate();
            }
        }
    }   
}

That worked for me on Jboss 6 AS, when tried to integrate an MDB with seam persistence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文