开发者

Seam3/Weld - Could not find Entity Manager

开发者 https://www.devze.com 2023-03-04 16:05 出处:网络
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 Mana

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(Defa开发者_StackOverflow中文版ultProtocolChain.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消