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

Posted

技术标签:

【中文标题】Seam3/Weld - 找不到实体管理器【英文标题】:Seam3/Weld - Could not find Entity Manager 【发布时间】:2011-08-22 07:44:20 【问题描述】:

我有一个带有视图(JSF 2.0)和持久性(JPA 2.0,Hibernate)层的项目(EJB 3.0,打包为 EAR)。为了避免延迟初始化支持和其他问题,我尝试在 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 中找不到生产者字段,但在注入我的 DAO 的 BeanManager 中。

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

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

【问题讨论】:

【参考方案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 上对我有用。

【讨论】:

以上是关于Seam3/Weld - 找不到实体管理器的主要内容,如果未能解决你的问题,请参考以下文章

ld 链接器找不到 so 库

Composer 自动加载器找不到类

宝塔python项目管理器虚拟环境bin没有activate导致计划任务运行不了python项目管理器找不到activate解决办法

pycharm解析器找不到

ld 链接器找不到 so 库

链接器找不到 Lua 库定义