I'm frustrated, I'm doing wrong please?

26 views
Skip to first unread message

Kenneth Burgos Nivar

unread,
Feb 17, 2013, 1:58:38 PM2/17/13
to siena-...@googlegroups.com
Play 1.2.5 + GAE + Siena 2.0.7

@Entity
public class Parent extends Model {

    @Id(Generator.AUTO_INCREMENT)
    public Long id;

    public String name;

    @Owned(mappedBy = "myParent")
    public Many<Child> children;

    public Parent(String name) {
        this.name = name;
    }

    public static Query<Parent> all() {
        return Model.all(Parent.class);
    }
}

@Entity
public class Child extends Model {

    @Id(Generator.AUTO_INCREMENT)
    public Long id;

    public String name;

    public Parent myParent;

    public Child(String name) {
        this.name = name;
    }

    public static Query<Child> all() {
        return Model.all(Child.class);
    }
}

--------
public class BasicTest extends UnitTest {

    @Test
    public void parentChild() {
        Parent parentGod = new Parent("God");

        Child childA = new Child("Angel1");
        Child childB = new Child("Angel2");
        Child childC = new Child("Angel3");

        childA.myParent = parentGod;
        childB.myParent = parentGod;
        childC.myParent = parentGod;

        childA.save(); childB.save(); childC.save();

        parentGod.children.asList().addAll(childA, childB, childC);

        parentGod.save();

        Parent parentGodFromDb = Parent.all().filter("name","God").get();

        assertEquals("God",parentGodFromDb.name);
        assertEquals("God",parentGodFromDb.children.asList().get(0).name);    //here is my problem, the children are empty every time
    }

-The property myParent in Child class is always empty myparent :(
-The children I can not read
from Parent class :(

Please, you suggest me a way to do this optimized. :)

Thank you.


Emanuele Ziglioli

unread,
Feb 17, 2013, 8:47:00 PM2/17/13
to siena-...@googlegroups.com
public class BasicTest extends UnitTest {

    @Test
    public void parentChild() {
        Parent parentGod = new Parent("God");

-The property myParent in Child class is always empty myparent :(


I bet it is because you don't seem to do parent.save() before assigning it to children
 
-The children I can not read from Parent class :(

same

Kenneth Burgos Nivar

unread,
Feb 17, 2013, 9:50:51 PM2/17/13
to siena-...@googlegroups.com
    @Test
    public void parentChild() {
        Parent parentGod = new Parent("God");

        parentGod.save();                             //how you sold me.


        Child childA = new Child("Angel1");
        Child childB = new Child("Angel2");
        Child childC = new Child("Angel3");

        childA.myParent = parentGod;
        childB.myParent = parentGod;
        childC.myParent = parentGod;

        childA.save(); childB.save(); childC.save();

        parentGod.children.asList().addAll(childA, childB, childC);

        parentGod.save();

        Parent parentGodFromDb = Parent.all().filter("name","God").get();

        assertEquals("God",parentGodFromDb.name);
        assertEquals("God",parentGodFromDb.children.asList().get(0).myParent.name);
    }

And get this error:

A java lang.IndexOutOfBoundsException has been caught, Index: 0, Size: 0
In /test/BasicTest.java, line 36:
assertEquals("God",parentGodFromDb.children.asList().get(0).myParent.name);

:(, I cant continue my tutorial.

Emanuele Ziglioli

unread,
Feb 18, 2013, 3:21:12 AM2/18/13
to siena-...@googlegroups.com
Not sure where this error comes from and I'm having trouble running code on this laptop tonight.
Anyway, instead of:
       assertEquals("God",parentGodFromDb.children.asList().get(0).myParent.name);
try:
       parentGodFromDb.children.asList().forceSync().get(0).myParent.get();
       assertEquals("God", parentGodFromDb2.name);


or 
       assertEquals("God", parentGodFromDb.children.asList().forceSync().get(0).myParent);

The point is any reference needs to be loaded before its fields are filled, other than id
I don't know why your list is empty. I never use asList() because I always iterate, and/or filter, or get keys first then batch using Objectify's cache

Emanuele Ziglioli

unread,
Feb 18, 2013, 3:22:37 AM2/18/13
to siena-...@googlegroups.com


or 
       assertEquals("God", parentGodFromDb.children.asList().forceSync().get(0).myParent);


sorry I meant:

 
assertEquals( parentGodparentGodFromDb.children.asList().forceSync().get(0).myParent);

equals() only looks at the ids, so you don't actually need to load the whole object 

Kenneth Burgos Nivar

unread,
Feb 18, 2013, 10:47:22 AM2/18/13
to siena-...@googlegroups.com
It worked, I really appreciate the time and attention devoted to me without knowing me.

In the same order, if not much to ask, I thought how I try to do this can be improved using Batch and that you said, but I have not found a good example on this issue, if you had a simple example for me to build on a good technique, the technique of a professional like you :), thereby put his name in the comments of the code which use this technique. Anyway, if you can not really appreciate the work you have been doing with all the problems that published in this forum, is a service worthy of admiration and be rewarded.

Thank you.

(sorry if I have bad spelling, not speak English and use the google translator)


El domingo, 17 de febrero de 2013 14:58:38 UTC-4, Kenneth Burgos Nivar escribió:

Emanuele Ziglioli

unread,
Feb 25, 2013, 4:25:45 AM2/25/13
to siena-...@googlegroups.com
ide nada, tío!

este forum empezó en Español y si quiere seguimos así ;-)
Reply all
Reply to author
Forward
0 new messages