1

I have a class Data which implements Serializable interface. This class has such fields

    private boolean q = false;
    private String a = "";
    private List<Someclass> m = Collections.emptyList();
    private List<Object[]> d = Collections.emptyList();

Values assigned to these members are default values. Class Someclass also implements Serializable and it has such columns

   private Types sqlType;
    private int columnWidth;
    private String columnName;

Types is an enum which also implements serializable.

In Data class I have List<Object[]> d in which I will save data fethced from database through jdbc(when iterating ResultSet i use getObject() method). I use such construction, because it can run any query(query's structure is not known). In List<Someclass> m I hold metada of query. So when I try to fetch rows with simple query I get

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.: instance = [Ljava.lang.Object;@127053a9

Why it occures? All my transfer objects are serializable.

edit Ok, Object is not Serializable so it can not be passed to and returned from the server. But what I should use in this case. Generics will not help me, because I don't know the type at compile time

maks
  • 5,911
  • 17
  • 79
  • 123
  • Regardless of the fact that `Object` is not serializable, it is better to use `ArrayList` in RPC calls instead of `List`. See [http://stackoverflow.com/a/3060233/595072](http://stackoverflow.com/a/3060233/595072) – dimchez Dec 08 '11 at 17:05
  • We use `ArrayList` instead of `ArrayList`. You will get GWT compilation warnings, but that's fine. – Strelok Dec 09 '11 at 03:02

2 Answers2

2

In order for the class to be serializable, essentially everything you can get to from it has to also be serializable. In this case Object is not serializable, which makes Object[] not serializable, which makes List not serializable, which makes Data not serializable.

Think about it this way: If you can't serialize a given Object in the Object[], how are you going to serialize the Object[]? And if you can't serialize that, how are you going to serialize a list of that? And if you can't serialize that list, how are you going to serialize something that contains that list?

Now, it could be that everything in your Object[] ends up being serializable -- but the way you've typed it, the compiler can't guarantee that.

EDIT: Regarding what you should use instead, I would create some sort of wrapper class that will hold the SQL query's results, and make that one serializable.

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • but i've tried to serialize data object to file and it serializes well. Does gwt use another kind of serialization? – maks Dec 08 '11 at 16:51
  • 2
    @maks Yes, GWT RPC does not do normal Java serialization. See http://code.google.com/webtoolkit/doc/latest/FAQ_Server.html#Does_the_GWT_RPC_system_support_the_use_of_java.io.Serializable and http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunication.html#DevGuideSerializableTypes – ben_w Dec 08 '11 at 16:55
0

You can't use List here because Object[] is not serializable.

You need to create a different, Serializable class that can hold any column value you get from your query.

Perhaps something like:

public abstract class ColumnValue implements Serializable
{
    public abstract Object getValue();
}

public class IntColumnValue extends ColumnValue
{
    private Integer _intValue;
    public Object getValue()
    {
         return _intValue;
    }
}

.
.
.

You create a sub-class of ColumnValue for each column type that might be returned. When you have an array of Object[], you move that to an array of ColumnValue[], choosing the correct sub-type for each value based on the meta-data from the query.

antlersoft
  • 14,636
  • 4
  • 35
  • 55
  • In your example you declare a member variable _intValue as integer, but as I said user can enter any sql query, so I don't now what type it will hold before runtime – maks Dec 08 '11 at 16:56