无法序列化会话Bean-引发警告 [英] Can't Serialize Session Beans - Warning thrown

查看:475
本文介绍了无法序列化会话Bean-引发警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EclipseLink(JPA 2.0)在netbeans中运行带有JSF + Primefaces + tomcat 6.0.32的环境.

I'm running an enviroment with JSF + Primefaces + tomcat 6.0.32 in netbeans using EclipseLink (JPA 2.0).

我的应用程序运行良好,但是每次运行它时,都会收到很多警告,提示无法序列化我的会话Bean,并向我显示每个会话Bean这样的块:

My application works fine, but everytime I run it, I get a lot of warnings saying that cannot Serializate my session beans, and shows me blocks like this for every session bean:

18-jul-2012 23:05:46 org.apache.catalina.session.StandardSession writeObject
ADVERTENCIA: No puedo serializar atributo de sesión facturacionController para sesión 62A53325838E1E7C6EB6607B1E7965E6
java.io.NotSerializableException: org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518)
    ... and so on...

问题是我的会话bean已经实现了Serializable. 那么我该怎么解决呢?

The thing is that my session beans already implements Serializable. So what can I do to solve this ?

谢谢!

----添加了信息07/20/2012 ----

---- added info 07/20/2012 ----

从会话bean引用EntityManager的唯一点是当我在getter属性中创建jpaController时,如下所示:

The only point where I'm making a reference to EntityManager from the session bean is when I create the jpaController in the getter property, like this:

private JpaController getJpaController() {
    if (jpaController == null) {
        jpaController = new JpaController(Persistence.createEntityManagerFactory("myPersistenceUnit"));
    }
    return jpaControllerPedido;
}

那是因为我这样定义了jpaController构造函数:

That is because I defined the jpaController constructor like this:

public JpaController(EntityManagerFactory emf) {
    this.emf = emf;
}

推荐答案

使类可序列化并不意味着该类中的所有内容都可以被序列化.您的类中的所有引用(依赖项/属性),它们本身都应该可序列化,并依次进行引用.

Making a class Serializable does not means everything in it will be serializable. All references(dependencies/properties) in your class, they themselves should be serializable and in turn their references.

根据上述异常,看来您的会话bean引用了EntityManagerFactoryImpl对象,该对象不可序列化,因此会出错.

As per above exception it seems your session bean is having reference to EntityManagerFactoryImpl object which is not serializable and hence the error.

要解决此问题,您可以将其定义为transient,而不是将其序列化,但是唯一的问题是在反序列化期间,您将必须构建对象或手动分配引用.

To solve this you can define it as transient than it wont be serialized but only problem will be during de-serialization you will have to build the object or assign reference manually.

我建议在系列化.

如何解决此问题,我不执行JPA,因此无法确定是否存在某些相同的序列化类,

How to solve this, I don't do JPA so cannot tell if there is some serialized class for same,

要解决此问题,请将引用定义为transient

To solve it define the reference as transient

transient EntityManagerFactory entityManagerFactory

并按如下所述通过反序列化钩子方法将引用手动分配回bean.

and assign the reference back to bean manually in deserialization hook method as described below.

private void readObject(java.io.ObjectInputStream stream)
        throws java.io.IOException, ClassNotFoundException
    {
        stream.defaultReadObject();

        // assign reference manually.
        this.entityManagerFactory =  //get from factory;
    }

希望这有帮助!!!!

Hope this helps !!!!

这篇关于无法序列化会话Bean-引发警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆