Can't Serialize Session Beans - Warning thrown

萝らか妹 提交于 2019-12-20 05:50:48

问题


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

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...

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

Thanks !

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

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;
}

That is because I defined the jpaController constructor like this:

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

回答1:


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.

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

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.

I suggest have a look at this article on Serilization.

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

To solve it define the reference as transient

transient EntityManagerFactory entityManagerFactory

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 !!!!



来源:https://stackoverflow.com/questions/11553335/cant-serialize-session-beans-warning-thrown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!