Hello,

In my application I have a class which has a field of type
Set<com.google.appengine.api.datastore.Key>. I want to store objects
of that class in datastore, but I can't make it work.

At first my class looked like this:
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Kuku {
        @PrimaryKey
        @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
        protected Key id;
        public Key getId() {
                return id;
        }
        @Persistent
        protected Set<Key> mySet = new HashSet<Key>();
        public int getMySetSize() {
                return mySet.size();
        }
        public void addToSet(Key what) {
                mySet.add(what);
        }
}

In some JSP page I tried to create a new object of that class, then
store it in a datastore, then fetch it back from datastore and add
some elements to its mySet, the fetch it again and see how many
elements there are in mySet:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="psobolewski.Kuku" %>
<%@ page import="psobolewski.PMF" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<html>
<body>
<%
Kuku kuku;
javax.jdo.PersistenceManager pm;
Key kukuId;
String result;
kuku = new Kuku();
pm = PMF.get().getPersistenceManager();
pm.makePersistent(kuku);
kukuId = kuku.getId();
pm.close();
%>
<%
pm = PMF.get().getPersistenceManager();
kuku = pm.getObjectById(Kuku.class, kukuId);
kuku.addToSet(kukuId.getChild(kukuId.getKind(), 0));
kuku.addToSet(kukuId.getChild(kukuId.getKind(), 1));
kuku.addToSet(kukuId.getChild(kukuId.getKind(), 2));
pm.close();
pm = PMF.get().getPersistenceManager();
kuku = pm.getObjectById(Kuku.class, kukuId);
result = "size: " + kuku.getMySetSize();
pm.close();
%>
<%= result %>
</body>
</html>

However, when I try to run it, I get java.lang.NullPointerException in
this place:
public void addToSet(Key what) {
        mySet.add(what);
}

Then I decided to change my class this way:
@Persistent(defaultFetchGroup = "true")
protected Set<Key> mySet = new HashSet<Key>();

However, this didn't help - I still get this NullPointerException. So
I decided to change my class this way:
@Persistent(defaultFetchGroup = "true", serialized = "true")
protected Set<Key> mySet = new HashSet<Key>();

Now I don't get NullPointerException any more, but the reported size
of mySet is zero, as if I haven't added any elements to it.

What do I do wrong? How can I keep such field in a datastore?



--

You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.


Reply via email to