开发者

Owned One-to-Many Relationships

I have two classes:

public class Dog {
  开发者_如何学JAVA  @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent(mappedBy = "dog")
    @Element(dependent = "true")
    private List<Toy> toys;
}

public class Toy {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private Name name;

    @Persistent
    private Dog dog;
}

1) If I do this

Toy toy = new Toy();
toy.setDog(dog); // dog is a Dog class
pm.makePersistent(toy);

Can I get this Toy from the Dog through getToys()?

2) If I do this twice

Toy toy = new Toy();
toy.setName("AAA");
toy.setDog(dog);
pm.makePersistent(toy);

Will the two exactly same toys double in the datestore?

Thanks!


  1. If you do "toy.setDog" you haven't added the toy to the dogs List of toys ... i.e the relation is bidirectional and you have to set both sides.

  2. What exactly same toys ? If you add 2 Toys you have 2 Toys. The values of their fields is irrelevant, just the identity matters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜