使用ebean ORM和java的Play Framework 2.3.8制作注释树

使用ebean ORM和java的Play Framework 2.3.8制作注释树,java,mysql,playframework,ebean,Java,Mysql,Playframework,Ebean,我正在学习“边做边学”的游戏框架。我正试着写一个简单的博客(使用官方网站上的信息),结果被卡住了 我正试图为一篇文章建立一个评论树。到目前为止,我设计的模型类如下: 后课堂: @Entity public class Post extends Model { @Id public Long id; public String title; public Date postedAt; @Column(columnDefinition = "TEXT") public String conten

我正在学习“边做边学”的游戏框架。我正试着写一个简单的博客(使用官方网站上的信息),结果被卡住了

我正试图为一篇文章建立一个评论树。到目前为止,我设计的模型类如下:

后课堂:

@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;
public class ChildComment extends Model{
@Id
public Long id;
public String content;

@ManyToOne
public Comment comment;
}

控制器类应用程序。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());
}
公共类应用程序扩展控制器{
公共静态表单postForm=Form.Form(Post.class);
公共静态结果帖子(){
返回ok(views.html.index.render(Post.all(),postForm));
}
公共静态结果索引(){
返回重定向(routes.Application.posts());
}
公共静态结果newPost(){
Form filledForm=postForm.bindFromRequest();
if(filledForm.hasErrors()){
返回badRequest(views.html.index.render(Post.all(),filledForm));
}否则{
创建(filledForm.get());
返回重定向(routes.Application.posts());
}
}
公共静态结果deletePost(长id){
删除后(id);
返回重定向(routes.Application.posts());
}
}

我知道我必须使用一对多关系来完成任务(在模型类中,我认为我做得很正确),但我被控制器中逻辑的实现卡住了,无法管理注释和注释的注释。任何线索或建议都很好


p.S.我正在使用MySql数据库创建您可以做的评论

在控制器中传递帖子id和注释数据

然后在控制器中

//post_id and commentData received from view
Post post=Post.findByPostId(post_id);                 //find post of that comment where findByPostId() is a function in model
Comment comment=new Comment(commentData,null,post);   //Create a new Comment Object
Comment cm=Comment.save(comment);                     //where save() saves the Comment object in data base and return the saved object
List <Comments> allCommentsOnPost=post.getComments(); //get all comments on that post
allComments.add(cm);                                  //add new comment to list
post.setComments(allCommentsOnPost);                  //set the new list in Post object
post.update(post_id);                                 //update post entity
//从视图接收的post\u id和commentData
Post Post=Post.findByPostId(Post_id)//查找该注释的帖子,其中findByPostId()是模型中的函数
Comment Comment=新注释(commentData,null,post)//创建新的注释对象
Comment cm=Comment.save(Comment)//其中save()将注释对象保存在数据库中并返回保存的对象
列出allCommentsOnPost=post.getComments()//获取该帖子的所有评论
所有评论。添加(cm)//向列表中添加新注释
post.setComments(allCommentsOnPost)//在Post对象中设置新列表
职位更新(职位id)//更新邮政实体
类似地,要保存子注释并传递注释id,请从视图中保存childCommentData

//comment_id and childCommentData received from view
Comment cm=Comment.findByCommentId(comment_id);                //find comment from id ,findByCommentId() defined in Comment entity

ChildComment childCom=new ChildComment(childCommentData,cm);    //create new object of ChildComment
ChildComment childComment=ChildComment.save(childCom);                                       //persist the child comment object in db ,save() is a function in model which saves ChildComment object and return it

List<ChildComments> allChildComments=cm.getChildComments();     //getting list of all the ChildComments .
allChildComments.add(childComment);                             //add new comment to list

cm.setChildComments(allChildComments);                          //set all the child comments in Comment Entity

cm.update(comment_id);                                          //update the Comments entity in db
//从视图接收到的注释id和childCommentData
Comment cm=Comment.findByCommentId(Comment\u id)//从注释实体中定义的id findByCommentId()查找注释
ChildComment childCom=新的ChildComment(childCommentData,cm)//创建ChildComment的新对象
ChildComment=ChildComment.save(childCom)//在数据库中持久化子注释对象,save()是模型中的一个函数,用于保存并返回子注释对象
列出allChildComments=cm.getChildComments()//正在获取所有儿童评论的列表。
添加(childComment)//向列表中添加新注释
cm.setChildComments(所有ChildComments)//设置注释实体中的所有子注释
cm.更新(注释id)//更新数据库中的注释实体
注意:我在这两种情况下都创建了新的Comment和ChildComment对象 您还可以分别使用bindRequest().get()来获取实体对象


实际上,您不需要为儿童使用两个类-这样您就可以只使用两个级别的注释,而只需将字段添加到您的
注释
模型中:

@ManyToOne
public Comment parent;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="parent")
public List<Comment> children;
@ManyToOne
公众评论家长;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy=“parent”)
公开儿童名单;
这样,理论上你可以拥有无限的树枝


在创建新注释的过程中,您可以添加父注释的ID-如果为空,则表示注释位于根级别(没有父注释)

这很好。谢谢你,先生。现在我必须弄清楚如何在控制器中执行逻辑。我对Java和Play框架非常陌生。我来自.NET初级课程:)