gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:42:26 25 4
gpt4 key购买 nike

我正在通过边做边学的方法学习 Play 框架。我正在尝试制作一个简单的博客(使用官方网站上的信息)并陷入困境。

我正在尝试制作一个帖子的评论树。到目前为止,我设计的模型类如下:

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;

}

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

}

我知道我必须使用一对多关系来完成任务(在模型类中我认为我做得正确),但我陷入了逻辑的实现 Controller 来管理评论和评论的评论。任何线索或建议都会很棒。

附注我使用的是 MySql 数据库

最佳答案

要创建评论,您可以执行以下操作。

在 Controller 中传递帖子 ID 和评论数据

然后在 Controller 中

//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

类似地,要保存子评论,请从 View 中传递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

Note:I am creating new Comment and ChildComment object in both the cases respectively you can also use bindRequest().get() to get the Entity object

关于java - 使用 Play Framework 2.3.8 使用 ebean ORM 和 java 制作注释树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28671775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com