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

时间:2011-05-10 09:27:21

标签: java jpa seam cdi seam3

我有一个项目(EJB 3.0,打包为EAR),带有视图(JSF 2.0)和持久性(JPA 2.0,Hibernate)层。为了避免延迟初始化支持和其他问题,我尝试使用Seam 3.0.0.Final引入Seam Managed Persistence Context。 为此,我编写了以下生产者类:

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

在方法范围org.jboss.seam.persistence.ManagedPersistenceContextBeanLifecycle.getEntityManagerFactory中BeanManager的beans-Field中找不到producer字段,但是在注入到我的DAO中的BeanManager中。

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

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

1 个答案:

答案 0 :(得分:1)

似乎在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与接缝持久性集成时,这对Jboss 6 AS起了作用。