开发者

ArrayList throwing exception on retrieval from google datastore (with gwt, java)

开发者 https://www.devze.com 2022-12-28 05:48 出处:网络
I\'m using Google Web Toolkit with java and google datastore as database. The entity class has arraylist and on trying to retrieve the data from data base I\'m ge开发者_运维百科tting the exception:

I'm using Google Web Toolkit with java and google datastore as database. The entity class has arraylist and on trying to retrieve the data from data base I'm ge开发者_运维百科tting the exception:

Type 'org.datanucleus.sco.backed.ArrayList' 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'm using JPA.

Entity code:

 package com.ver2.DY.client;

 import java.io.Serializable;
 import java.util.ArrayList;

 import javax.jdo.annotations.IdGeneratorStrategy;
 import javax.jdo.annotations.PersistenceCapable;
 import javax.jdo.annotations.Persistent;
 import javax.jdo.annotations.PrimaryKey;

 import com.google.gwt.user.client.rpc.IsSerializable;

 @PersistenceCapable
 public class ChatInfo implements Serializable, IsSerializable{
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long topicId;

  @Persistent
  private String chatTopic;

  @Persistent
  private ArrayList<String> messages = new ArrayList<String>();

  @Persistent
  private boolean isFirstPost;

  public ChatInfo()
  {

  }


  public Long getTopicId() {    
   return topicId;
  }
  public void setTopicId(Long topicId) {
   this.topicId = topicId;
  }
  public String getChatTopic() {
   return chatTopic;
  }
  public void setChatTopic(String chatTopic) {
   this.chatTopic = chatTopic;
  }
  public ArrayList<String> getMessages() {
   return messages;
  }
  public void addMessage(String newMsg) {
    messages.add(newMsg);
  }

  public boolean isFirstPost() {
   return isFirstPost;
  }
  public void setFirstPost(boolean isFirstPost) {
   this.isFirstPost = isFirstPost;
  }

 }

Method in db class:

@Transactional
  public ChatInfo[] getAllChat() {
   PersistenceManager pm = PMF.get().getPersistenceManager();
   List<ChatInfo> chats = null;
   ChatInfo[] infos = null;
   String query = "select from " + ChatInfo.class.getName();
   try{
    chats = (List<ChatInfo>) pm.newQuery(query).execute();


   infos = new ChatInfo[chats.size()];
   for(int i=0;i<chats.size();i++)
   {
    infos[i] = new ChatInfo();
    infos[i] = (ChatInfo) chats.get(i);
   }
   }
   finally{
    pm.close();
   }
   return infos;

  }

It is a bit strange because earlier I was able to insert and retrieve the data but it now throwing an exception. On searching the web I could find that I need to convert the Arraylist from some DataNucleus type to java util but not sure how to do that.


The exception is caused by the fact that the class is not in your GWT RPC whitelist (list of classes which are serializable).

I am unable to see any import of the org.datanucleas.sco.backed.ArrayList in any of the example code you have given.

Check out this question for more info on GWT and JPA.


This issue is described here and a couple of bugs have been opened on it. Essentially, when being retrieved from the datastore you're not getting a java.util.ArrayList back, you're getting an org.datanucleus.sco.backed.ArrayList instead. Everything seems fine until you call an RPC when, unsurprisingly, it's unable to serialize this type.

The hacky fix appears to be converting the ArrayList to the right type after retrieving the object but before calling any RPCs that use it. At least until the bugs are fixed.

0

精彩评论

暂无评论...
验证码 换一张
取 消