0

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;
}
oriuken
  • 653
  • 3
  • 7
  • 19
  • Please post the full StackTrace. Also, maybe [this post](http://forum.primefaces.org/viewtopic.php?f=3&t=8770) could help you with the problem: ava.io.NotSerializableException: org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl (JPA with JSF). – Luiggi Mendoza Jul 19 '12 at 05:10
  • What exactly is the reason that you're keeping a reference to `EntityManager` in the session scoped JSF managed bean? Are you trying to simulate a stateful EJB or so? – BalusC Jul 20 '12 at 12:27
  • Thanks for the reply !, I'm not trying to sumulate a stateful EJB, I have a reference when i'm creating the JPA controller inside the getter property. I added the code in the Question. – oriuken Jul 20 '12 at 17:26

2 Answers2

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

baba.kabira
  • 3,111
  • 2
  • 26
  • 37
  • Thanks for the reply ! I don't think that assigning the reference back to bean manually in deserialization hook method is a good idea, I prefer to let JSF deal with that. I think it must be an EntityManager reference as you say, maybe in the getter property of the jpaController like I show in the question. – oriuken Jul 20 '12 at 17:34
0

You only add this:

transient EntityManagerFactory entityManagerFactory;

But if have any other Object that implement entityManagerFactory, this object must also be defined as. For Example

transient static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("porpertiesConexion");
transient EntityManager em;    


public void beginTransaction() {           
    em = emf.createEntityManager();  //
        //code....
} 
anderson j mariño o.
  • 1,739
  • 2
  • 6
  • 7