Hi Max,

I updated my code to use getters & setters and set both children and
parent relationships. The tests still fail as before:

public class RelationshipTest extends LocalServiceTestCase {

  @Test  // PASSES
  public void saveParentWithChildAdded() throws Throwable {
    Child child = new Child();
    Parent parent = new Parent();
    parent.addChild(child);
    savePojo(parent);
    assertNotNull(child.key);
    assertNotNull(parent.key);
  }

  @Test  // FAILS
  public void saveParentThenAddChild() throws Throwable {
    Child child = new Child();
    Parent parent = new Parent();
    savePojo(parent);
    assertNotNull(parent.key);
    parent.addChild(child);
    child.setParent(parent);
    savePojo(parent);
    assertNotNull(parent.key);
    assertNotNull(child.key);  // THIS FAILS
  }

  @Test  // FAILS
  public void saveChildWithParentSet() throws Exception {
    Child child = new Child();
    Parent parent = new Parent();
    savePojo(parent);
    child.setParent(parent);
    parent.addChild(child);
    savePojo(child);  // Shouldn't this work??
    assertNotNull(parent.key);
    assertNotNull(child.key);    // THIS FAILS
  }

  @Test  // FAILS, obviously
  public void saveParentWithParentSet() throws Exception {
    Child child = new Child();
    Parent parent = new Parent();
    savePojo(parent);
    child.setParent(parent);
    parent.addChild(child);
    savePojo(parent);
    assertNotNull(parent.key);
    assertNotNull(child.key);    // THIS FAILS
  }

  private void savePojo(Object pojo) throws Exception {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    pm.setDetachAllOnCommit(true);
    pm.setCopyOnAttach(false);
    try {
      pm.makePersistent(pojo);
      pm.close();
    } catch (Exception e) {
      pm.close();
      throw e;
    }
  }

  @PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable="true")
  public static class Parent {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Key key;
    @Persistent(mappedBy = "parent")
    private List<Child> children;

    public Parent() {
      children = Lists.newArrayList();
    }

    public List<Child> getChildren() {
      return children;
    }
    public void addChild(Child child) {
      children.add(child);
    }
  }

  @PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable="true")
  public static class Child {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Key key;

    @Persistent
    private Parent parent;

    public Parent getParent() {
      return parent;
    }

    public void setParent(Parent parent) {
      this.parent = parent;
    }
  }
}

Any ideas?

Dobromir

On Aug 14, 8:11 pm, Max Ross <[email protected]> wrote:
> You may find this page 
> helpful:http://www.datanucleus.org/products/accessplatform_1_1/jdo/orm/one_to...
>
> The key point is that you need to set both sides of a bidirectional
> relationship.  Also, if you're accessing members directly (as in the below
> example) you need to annotate the class performing this access with
> @PersistenceAware.  Otherwise JDO can't see that you're modifying the fields
> and doesn't know it needs to perform any updates.  However, it's much easier
> to just always access members via setters and getters.
>
> Hope this helps,
> Max
>
>
>
> On Fri, Aug 14, 2009 at 4:31 PM, Dobromir <[email protected]> wrote:
>
> > Here's another test that fails and which gets to the heart of my
> > problem: how do I add a Child to a Parent that's already been saved?
>
> > �...@test  // FAILS - help!
> >  public void saveParentThenAddChild() throws Throwable {
> >     Child child = new Child();
> >    Parent parent = new Parent();
> >     savePojo(parent);
> >    assertNotNull(parent.key);
> >     parent.children = Lists.newArrayList(child);
> >    savePojo(parent);
> >     assertNotNull(parent.key);
> >    assertNotNull(child.key);  // THIS FAILS
> >  }
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to