通过 @ConversationScoped 方法提供 EntityManager

发布于 2024-12-08 03:01:10 字数 1542 浏览 0 评论 0原文

我尝试在JBoss7中运行由maven archetype groupId: org.fluttercode.knappsack , artifactID: jee6-sandbox-archetype 生成的简单JEE6应用程序。 (经历了这个turial,抱歉,德语)

但是,当调用欢迎 JSF 时,我收到以下错误消息:

org.jboss.weld.exceptions.IllegalProductException: WELD-000053 Producers 
  cannot declare passivating scope and return a non-serializable class:  
  [method] @Produces @DataRepository @ConversationScoped 
  public org.rap.jee6project.bean.DataRepositoryProducer.getEntityManager()
org.jboss.weld.bean.AbstractProducerBean.checkReturnValue(AbstractProducerBean.java:264)
org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:362)
org.jboss.weld.context.AbstractContext.get(AbstractContext.java:122)

事实上,DataRepositoyProducer 类应该返回 EntityManager实例,定义注释如下:

@Stateless
public class DataRepositoryProducer {


private EntityManager entityManager;

@Produces @DataRepository @ConversationScoped
public EntityManager getEntityManager() {
    return entityManager;
}

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

} 

如果我使用 @RequestScoped,应用程序将按承诺运行。我想知道为什么其他完成本教程的人没有遇到这个问题?以及如何正确修复它(使用@RequestScoped意味着为每个用户请求重新创建bean,对吧?,我预计这不是很有效)

官方JEE6教程说:“使用会话,应用程序的Beans,或会话范围必须是可序列化的,但使用请求范围的 bean 不必是可序列化的”。然而,这似乎不是这里的问题,因为服务器抱怨的不是 bean 不可序列化,而是生产者 bean 的产品。

I tried to run the simple JEE6 application generated by maven archetype groupId: org.fluttercode.knappsack , artifactID: jee6-sandbox-archetype in JBoss7.
(went through this turial, sorry, in German)

However, when calling the welcome JSF, I get the following error message:

org.jboss.weld.exceptions.IllegalProductException: WELD-000053 Producers 
  cannot declare passivating scope and return a non-serializable class:  
  [method] @Produces @DataRepository @ConversationScoped 
  public org.rap.jee6project.bean.DataRepositoryProducer.getEntityManager()
org.jboss.weld.bean.AbstractProducerBean.checkReturnValue(AbstractProducerBean.java:264)
org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:362)
org.jboss.weld.context.AbstractContext.get(AbstractContext.java:122)

Indeed, the DataRepositoyProducer class which is supposed to return an EntityManager instance, is defined an annotated as follws:

@Stateless
public class DataRepositoryProducer {


private EntityManager entityManager;

@Produces @DataRepository @ConversationScoped
public EntityManager getEntityManager() {
    return entityManager;
}

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

} 

If I use @RequestScoped, the application runs as promised. I wonder why other people who went through this tutorial didn't experience this problem? And how to fix it properly (using @RequestScoped means that the bean is recreated for each user request, right?, which I expect to be not very efficient)

The official JEE6 Tutorial says: "Beans that use session, application, or conversation scope must be serializable, but beans that use request scope do not have to be serializable" . However, that does not seem to be the problem here, since the server is not comlaining about the bean not serializable but the product of the producer bean.

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

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

发布评论

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

评论(3

寄居人 2024-12-15 03:01:11

它应该是..

@Stateful
@ConversationScoped
public class ProducerCreator implements Serializable{
    @PersistenceConText
    private EntityManager entityManager;
    ....
}

如果你想在每个对话中使用相同的实体上下文它应该是

@PersistenceContex(type = PersistenceContextType.EXTENDED)

最后,如果你想要有服务层,应该创建有状态并注入到对话bean

It should be..

@Stateful
@ConversationScoped
public class ProducerCreator implements Serializable{
    @PersistenceConText
    private EntityManager entityManager;
    ....
}

and if you want to use the same entity context in of each conversation it should be

@PersistenceContex(type = PersistenceContextType.EXTENDED)

finally, If you want to have service layer, should create stateful and inject to conversation bean

等风来 2024-12-15 03:01:11

我在 jboss7 上运行演示时遇到了同样的问题。

只需在 getEntityManager() 处删除 @ConversationScoped 就可以让我部署它。

尽管存在一些缺陷:

javax.servlet.ServletException: javax.faces.component.StateHolderSaver cannot be cast to [Ljava.lang.Object;    
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) 
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

我不确定它是否相关,但我猜是这样。

I had same problem running the demo on a jboss7.

Just remove @ConversationScoped at getEntityManager() did the trick to me to let it deploy.

Even though there are some flaws:

javax.servlet.ServletException: javax.faces.component.StateHolderSaver cannot be cast to [Ljava.lang.Object;    
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) 
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

I don't know exactly, if it is related, but I guess so.

两个我 2024-12-15 03:01:11

请记住:EntityManager 不可序列化,因此无法存储在 ConversationScope

Remember: EntityManager is not serializable, so it cannot be stored in ConversationScope

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