通过 @ConversationScoped 方法提供 EntityManager
我尝试在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该是..
如果你想在每个对话中使用相同的实体上下文它应该是
最后,如果你想要有服务层,应该创建有状态并注入到对话bean
It should be..
and if you want to use the same entity context in of each conversation it should be
finally, If you want to have service layer, should create stateful and inject to conversation bean
我在 jboss7 上运行演示时遇到了同样的问题。
只需在 getEntityManager() 处删除 @ConversationScoped 就可以让我部署它。
尽管存在一些缺陷:
我不确定它是否相关,但我猜是这样。
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:
I don't know exactly, if it is related, but I guess so.
请记住:
EntityManager
不可序列化,因此无法存储在ConversationScope
中Remember:
EntityManager
is not serializable, so it cannot be stored inConversationScope