向关联添加约束

时间:2011-12-20 22:37:03

标签: jpa playframework jpa-2.0

这是我在User和Post之间有一对多关系的交易

@Entity
public class User extends Model {
...
  @OneToMany(cascade = CascadeType.ALL, targetEntity = Post.class, mappedBy = "author")
  @OrderBy("createdAt DESC")
  public List<Post> posts;
...
}

@Entity
public class Post extends Model {
...
  public int privacy; // 0=public, 1=private
  @ManyToOne
  public User author;
...
}

工作得很漂亮......现在我想从用户那里检索公开帖子?当然迭代结果并删除私人帖子不是一种选择,我最终需要做一些分页,这将成为一个令人头痛的问题。是否有一些注释可以帮助我解决这个问题?

1 个答案:

答案 0 :(得分:1)

这样的东西?

User author = User.findById(1);  
int pageNumber = 1;
int pageSize = 10;
Post.find("FROM Post WHERE author = ? AND privacy = 0", author).fetch(pageNumber, pageSize);