簡體   English   中英

使用帶有ebean ORM和Java的Play Framework 2.3.8制作注釋樹

[英]Making a tree of comments with Play Framework 2.3.8 with ebean ORM and java

我正在通過邊做邊學的方法來學習Play框架。 我正在嘗試制作一個簡單的博客(使用官方網站上的信息)並陷入困境。

我正在嘗試對帖子發表評論。 到目前為止,我將模型類設計如下:

郵政課:

@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();
}

}

Comment類:

@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());
}

}

我知道我必須使用一對多關系來完成任務(在模型類中,我認為我做對了),但是我陷入了控制器中邏輯的實現,無法管理注釋和注釋。評論。 任何線索或建議都會很棒。

PS我正在使用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

同樣,要保存子評論,請從視圖中傳遞comment_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

注意:我分別在兩種情況下都創建了新的Comment和ChildComment對象,您也可以使用bindRequest()。get()獲取Entity對象

實際上,您不需要為孩子使用兩個類-這樣,您僅需要兩個注釋級別,而只需將字段添加到Comment模型即可:

@ManyToOne
public Comment parent;

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="parent")
public List<Comment> children;

這樣,您理論上就可以擁有無​​限的樹枝。

在創建新評論期間,您可以添加父評論的ID-如果為空,則表示該評論位於根級別 (無父)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM