【发布时间】:2015-02-23 10:36:52
【问题描述】:
我正在通过边做边学的方式学习 Play Framework。我正在尝试制作一个简单的博客(使用来自官方网站的信息)并且卡住了。
我正在尝试制作一个帖子的 cmets 树。 到目前为止,我设计的模型类如下:
Post 类:
@Entity
public class Post extends Model {
@Id
public Long id;
public String title;
public Date postedAt;
@Column(columnDefinition = "TEXT")
public String content;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="post")
public List<Comment> comments;
public static Finder<Long, Post> find = new Finder(Long.class, Post.class);
public static List<Post> all() {
return find.all();
}
public static void create(Post post) {
post.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
}
评论类:
@Entity
public class Comment extends Model {
@Id
public Long id;
public String content;
public static Finder<Long, Comment> find = new Finder(Long.class, Comment.class);
public static List<Comment> all() {
return find.all();
}
public static void create(Comment comment) {
comment.save();
}
public static void delete(Long id) {
find.ref(id).delete();
}
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="post")
public List<ChildComment> childComments;
@ManyToOne
public Post post;
}
ChildComment 类:
public class ChildComment extends Model{
@Id
public Long id;
public String content;
@ManyToOne
public Comment comment;
}
控制器类Application.java
public class Application extends Controller {
public static Form<Post> postForm = Form.form(Post.class);
public static Result posts() {
return ok(views.html.index.render(Post.all(), postForm));
}
public static Result index() {
return redirect(routes.Application.posts());
}
public static Result newPost() {
Form<Post> filledForm = postForm.bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest(views.html.index.render(Post.all(), filledForm));
} else {
Post.create(filledForm.get());
return redirect(routes.Application.posts());
}
}
public static Result deletePost(Long id) {
Post.delete(id);
return redirect(routes.Application.posts());
}
}
我知道我必须使用一对多关系来完成任务(并且在模型类中我认为我做对了),但我被困在逻辑的实现中控制器来管理 cmets 和 cmets 的 cmets。任何线索或建议都会很棒。
附注我正在使用 MySql 数据库
【问题讨论】:
标签: java mysql playframework ebean