As ArtemGr notes, Collections are lazily loaded. If you know that you always
want the sellers field, then you can add it to the default fetch group so
that it's always fetched:

@Persistent(defaultFetchGroup = "true")
private Set<Key> sellers;

...

- Jason

On Fri, Oct 16, 2009 at 11:23 AM, ArtemGr <artem...@gmail.com> wrote:

>
> JDO lazily loads collection fields.
> Try accessing the fields from inside a JDO transaction.
> If that works, then this is the case.
> You can use "fetch groups" and "detach" your objects to access
> collection fields outside of transaction.
>
> On 15 окт, 11:30, dragonballs <dragonba...@gmail.com> wrote:
> > I got an issue when persisting collection fileds.
> >
> > A sample code is:
> >
> > @PersistenceCapable(identityType = IdentityType.APPLICATION)
> > public class Customer {
> >
> >     @PrimaryKey
> >     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> >     private Key key;
> >
> >     @Persistent
> >     private Set<Key> sellers;
> >
> >     public Customer() {
> >         this.sellers = new HashSet<Key>();
> >     }
> >
> >     public Customer(String userId) {
> >         this.key = KeyFactory.createKey(getClass().getSimpleName(),
> > userId);
> >         this.sellers = new HashSet<Key>();
> >     }
> >
> >     public Key getKey() {
> >         return key;
> >     }
> >
> >     public Key[] getSellers() {
> >         return sellers.toArray(new Key[sellers.size()]);
> >     }
> >
> >     public boolean addSeller(Key key) {
> >         return sellers.add(key);
> >     }
> >
> >     public boolean removeSeller(Key key) {
> >         return sellers.remove(key);
> >     }
> >
> > }
> >
> > The issue is when I first create a Customer object and persist it, its
> > sellers field is supposed to be an empty ArrayList. But next time when
> > I retrieve it from persistence and attempt to access the list content
> > (getSellers()), a NullPointerException is thrown.
> >
> > A workaround is to check whether sellers is null in each methods that
> > access it, but this is really annoying...
> >
>

--~--~---------~--~----~------------~-------~--~----~
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-java@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