简体   繁体   English

引用包含实体管理器的EJB的命名类的序列化

[英]Serialization of a named class with a reference to EJB, which holds the entity manager

I use Java EE 6 with Glassfish 3.1.2.2. 我将Java EE 6与Glassfish 3.1.2.2一起使用。 I have a EJB project there with stateless session beans and a WEB project with named, session scoped classes. 我在那里有一个带有无状态会话Bean的EJB项目,以及一个带有命名的,会话范围的类的WEB项目。

For example: 例如:

Named class 命名班

@Named
@SessionScoped
public class MyWebController implements Serializable {

@EJB
private MyBean myBean;
}

EJB class EJB类

@Stateless
public class MyBean {

@PersistenceContext
private EntityManager em;
}

The project is running, but when I do code analysis, I get the following message. 该项目正在运行,但是当我进行代码分析时,会收到以下消息。

Non-transient non-serializable instance field in serializable class 可序列化类中的非瞬态不可序列化实例字段

This Serializable class defines a non-primitive instance field which is neither transient, Serializable, or java.lang.Object, and does not appear to implement the Externalizable interface or the readObject() and writeObject() methods. 该Serializable类定义了一个非基本实例字段,该实例字段既不是临时的,Serializable的也不是java.lang.Object,并且似乎没有实现Externalizable接口或readObject()和writeObject()方法。 Objects of this class will not be deserialized correctly if a non-Serializable object is stored in this field. 如果不可序列化的对象存储在此字段中,则不会正确反序列化此类的对象。

Does somebody know, who can I refactor my code to avoid this? 有人知道,谁可以重构我的代码来避免这种情况?

Not an expert on these topics but try this one: 不是这些主题的专家,而是尝试以下主题:

1) Make sure your are using the correct @SessionScoped annotation: 1)确保您使用的是正确的 @SessionScoped批注:

@Named
@javax.enterprise.context.SessionScoped
public class MyWebController implements Serializable {

  @EJB
  private MyBean myBean;

}

2) Try with EJB serializable too: 2)也尝试使用EJB可序列化:

@Stateless
public class MyBean implements Serializable {
    ...
}

3) If this solution keeps broken. 3)如果此解决方案不可行。 Try with @Inject instead of @EJB 尝试使用@Inject而不是@EJB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM