Wednesday, April 6, 2011

Type 'com.theapplicationbox.gwt.jpa.entities.Person' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized

I had this exception today when using Gilead 1.3.2.1839 + GWT 2.2 (and other technologies, but not important: Hibernate 3.3.2.GA + Hibernate EntityManager 3.4.0.GA(JPA 1.0)+Spring 3.0.5:)


Type 'com.theapplicationbox.gwt.jpa.entities.Person' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.
 Everything concerning Gilead was set correctly in my GWT project in Eclipse, that is:
  •  the application.gwt.xml (place in com.theapplicationbox) contained the Gilead configuration
<?xml version="1.0" encoding="UTF-8"?>
<module rename-to='application'>
    <!-- Inherit the core Web Toolkit stuff.                        -->
    <inherits name='com.google.gwt.user.User' />


    <!-- Inherit the default GWT style sheet.  You can change       -->
    <!-- the theme of your GWT application by uncommenting          -->
    <!-- any one of the following lines.                            -->
    <inherits name='com.google.gwt.user.theme.standard.Standard' />
    <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
    <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->


    <!-- Other module inherits                                        -->
    <!-- Gilead inherits, as in the docs: http://noon.gilead.free.fr/gilead/index.php?page=stateless -->
    <!--  adapted from "net.sf.gilead.Adapter4Gwt15" to "net.sf.gilead.Gilead4Gwt" -->
    <inherits name='net.sf.gilead.Gilead4Gwt' />
    <!-- Uncomment if your Domain class use JPA mapping annotations -->
    <inherits name='net.sf.gilead.emul.java5.ejb3.Ejb3' />


    <!-- Specify the app entry point class.                         -->
    <!--<entry-point class='com.theapplicationbox.gwt.client.ExampleEntyPoint' />-->
    <!-- Specify the paths for translatable code                    -->
    <source path='gwt/client' />
    <source path='gwt/shared' />
    <source path='middletier/common/interfaces' />
    <source path='jpa/entities' />


    <entry-point class="com.theapplicationbox.gwt.client.entrypoints.application.ApplicationEntryPoint"></entry-point>
</module>
  •  jpa/entities folder which contains Person.java is declared as source folder
  <source path='jpa/entities' />
  • the entity class had a default constructor, implemented Serializable and inherited LightEntity of Gilead:
    import net.sf.gilead.pojo.gwt.LightEntity;
    public class Person extends LightEntity implements Serializable {
    public Person(){}
    ...
    }

So there was no reason i got this.
But...

My service interface generating this problem were:
public interface IPersonService<T> extends RemoteService {


    List<T> getAll() throws IllegalArgumentException;


    public List<T> saveModifications(List<T> persons);
}
and
public interface IPersonServiceAsync<T> {
    void getAll(AsyncCallback<List<T>> callback) throws IllegalArgumentException;


    void saveModifications(List<T> persons, AsyncCallback<List<T>> callback);
}
The problem is that i shouldn't have used generics HERE in GWT. So:

  • either foget about <T> in the interface and method definitions and just use List<Person> in the methods definitions
  • keep the genericity but in a new couple of interfaces called IGenericService and IGenericServiceAync and then inherit from them:
public interface IPersonService extends IGenericService<Person> {
}
and
public interface IPersonServiceAsync extends IGenericServiceAsync<Person> {
}
   And in your code make sure you use the IPersonServiceAsync, not the generic version.


The answer by Bruno Marchesson to a similar pb (see bellow) says:
Looks like the RPC service signature declares that it returns an array of Object, which is forbidden by GWT serialization policy (too generic).
You should return an array of Pais instead.

The same happened in my case behind the scenes, even though i didn't have List<Object> as return type, but it finally got down to that and here is why.

The genericity is useful at compile time but the java class file bytes will not contain any type parameters information like T.
This is called type erasure. Instead, List<T> is replaced by List<Object> and here we are in exactly the same case.

The answer to the Exception
The answer is explained in this post by Bruno Marchesson in response to this original post.
The person in the original post signaled this well-known exception:

Caused by: com.google.gwt.user.client.rpc.SerializationException: Type '[Ljava.lang.Object;' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.

How i found these answers
I looked on the Gilead forum with these keywords "was not included in the set of types which can be serialized by this SerializationPolicy" and tried randomly until these 2 posts: the original and the response.

Also this post says a very interesting advice:

 I've found you have to be as
specific as possible when delaring your RPC methods and serializable
class fields. 

This post i founded googling with:
"Type * was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized"

No comments:

Post a Comment

Use "hibernate.hbm2ddl.auto=update" to update your schema (add remove columns, etc.) without losing data in tables.

Use <property name="hibernate.hbm2ddl.auto" value="update" /> to update your schema (add remove columns, etc...