开发者

Play: What does adding a "many" object to a "one" list property do?

This question probably has a lot to do with Hibernate, but since Play obfuscates Hibernate and i don't know it anyway...

Take the following sample code: User has many Posts.

public class User extends Model{
    ...
    @OneToMany(mappedBy="author")
    public List<Post> posts;
}

public class Post ext开发者_运维技巧ends Model{
    ...
    @ManyToOne
    public User author;
}

When I create a post newPost with the User=John, why do I have to call John.add(newPost)?

John and newPost are already in the database. Even without the .add call, the tables have foreign key restraints. What is actually happening when I call the .add method?


Well it depends on the code within the .add method. But with Hibernate you need to maintain both sides of the mapping in a bi-directional mapping.

So, your add method is likely to be adding the Post to the list of posts, so that both sides of the mapping are maintained (I am assuming when you created the Post you set the author at that point).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜