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!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.
What exactly same toys ? If you add 2 Toys you have 2 Toys. The values of their fields is irrelevant, just the identity matters.
精彩评论