Hi Frederik!

Sorry for my ignorance, I tougth that the right way to save a Second
object was:

pm.makePersistent(mySecondObject);
myFirstObject.getList().add(mySecondObject);
pm.makePersistent(myFirstObject);

while if I understood your explanation I don't have to invoke
pm.makePersistent() on mySeconObject, is it?

I also put in all my getter methods a check on null object, in that
way:
class Second {
 @Persistent
 @PrimaryKey
 Key id;
 @Persistent
 First owner;
 @Persistent
 List<String> tags;

 First getOwner() {
  if (owner==null)
   owner = new First();
  return owner;
 }

 List<String> getTags() {
  if (tags==null)
   tags= new ArrayList<String>();
  return tags;
 }

 ...
}

Do I have to remove all the check of the type if (obj==null)
obj=new...?

Thank you very much for your help!

Best
cghersi

On 27 Ago, 10:58, Frederik Pfisterer <pfiste...@gmail.com> wrote:
> Hi,
>
> the problem is this line:
> owner = new First();
>
> Beware that the only way you should ever store a Second object is:
> myFirstObject.getList().add(mySecondObject);
> pm.makePersistent(myFirstObject);
>
> since Second.owner is mapped it's automatically populated by the
> persistance manager.
>
> Hope this helps,
> Fred
>
> On 26 Aug., 17:38, cghersi <cristiano.ghe...@gmail.com> wrote:
>
>
>
> > Hi Diego,
>
> > thank you but unfortunately I strictly followed what stated in that
> > page, and the result is the problem I posted!!
>
> > Any other hint?
>
> > Thank you very much,
> > Best
> > Cghersi
>
> > On 26 Ago, 16:09, Diego Fernandes <penet...@gmail.com> wrote:
>
> > > Hi,
> > > i may find something 
> > > herehttp://code.google.com/intl/en/appengine/docs/java/datastore/relation...
>
> > > []'s
> > > Diego
>
> > > On 26 ago, 04:42, cghersi <cristiano.ghe...@gmail.com> wrote:
>
> > > > Hi everybody,
>
> > > > I'm struggling with a strange problem with JDO.
> > > > I've got two PersistenCapable classes, one having a Collection of
> > > > objects of the second, something like this:
>
> > > > class First {
> > > > �...@persistent
> > > > �...@primarykey
> > > >  Long id;
>
> > > > �...@persistent(mappedby="owner")
> > > >  ArrayList<Second> list = new ArrayList<Second>();
>
> > > >  ArrayList<Second> getList() {
> > > >   if (list == null)
> > > >    list=new ArrayList<Second>();
> > > >   return list;
> > > >  }
>
> > > > ...
>
> > > > }
>
> > > > class Second {
> > > > �...@persistent
> > > > �...@primarykey
> > > >  Key id;
>
> > > > �...@persistent
> > > >  First owner;
>
> > > >  First getOwner() {
> > > >   if (owner==null)
> > > >    owner = new First();
> > > >   return owner;
>
> > > >  ...
>
> > > > }
>
> > > > In another class I need to print the owner of all my First objects, so
> > > > I do:
> > > > First obj = ...;
> > > > ArrayList<Second> list = obj.getList();
> > > > for (Second s : list) {
> > > >  System.out.println(s.getOwner());
>
> > > > }
>
> > > > In this loop, I find some Second object having null owner, and I
> > > > cannot understand why.
> > > > Now I have several questions about my data modelling:
> > > > 1) Do I need to mark any field with (defaultFetchGroup = "true")
> > > > annotation?
> > > > 2) Does the check on null object (e.g. if (owner==null) owner = new
> > > > First();) in the getter methods results in any strange behavior?
> > > > 3) Does the assignment on definition of objects (e.g.
> > > > ArrayList<Second> list = new ArrayList<Second>();) results in any
> > > > strange behavior?
> > > > 4) Do I need to add any other annotation to owner field of Second
> > > > class?
>
> > > > Thank you very much for your help!!
> > > > Best regards
> > > > cghersi

-- 
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