Collections are lazy loaded. You will need to access the collection before
adding items to it. You can do this by doing something like this:
public void addToSet(Key what) {
if(mySet != null) {
mySet.size(); // This forces the set to be loaded
mySet.add(what);
} else {
mySet = new HashSet<Key>();
}
}
Let me know if that works for you.
We're changing the behavior of the default fetch groups so that everything
except child objects are eager loaded (blobs, text, collections).
On Mon, Nov 30, 2009 at 12:12 PM, Ikai Lan <[email protected]> wrote:
> Collections are lazy loaded. You will need to access the collection before
> adding items to it. You can do this by doing something like this:
>
>
> public void addToSet(Key what) {
> if(mySet != null) {
>
> mySet.add(what);
>
> }
>
> On Thu, Nov 26, 2009 at 4:04 AM, Benedykt <[email protected]>wrote:
>
>> 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
>> [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<google-appengine-java%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/google-appengine-java?hl=en.
>>
>>
>>
>
>
> --
> Ikai Lan
> Developer Programs Engineer, Google App Engine
>
--
Ikai Lan
Developer Programs Engineer, Google App Engine
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.