2016-04-17 16 views
0

私はミニフォーラムを作ろうとしています。どのようにする必要がありますか、まずあなたは投稿を作成し、トピックのメッセージを書く、それはすべての投稿にリダイレクトし、あなたが作成した新しい投稿を追加し、ユーザーは投稿をクリックし、次のページに記入されたすべてのメッセージを表示します。コメントシステムを作った、私はすべてのコメントがコメントを持っている必要がありますので、問題は私のコードの下にあります、私はそのコメントの仕事をしようとしたとき、それはこのように動作します:存在するコメントなので、変更して失敗しました。誰かが私に説明してくれ、Hibernateで正しい方法を教えてください。スプリングブート+休止状態コントローラ

PostControllerは:メソッドseeMessageに注意を払う、それはidによって投稿を見つけて、メッセージを表示します。

package com.pandora.controllers; 


import com.pandora.domain.Post; 
import com.pandora.services.CommentService; 
import com.pandora.services.PostService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 


@Controller 
@RequestMapping("/posts") 
public class PostController { 

    @Autowired 
    private PostService postService; 

    @Autowired 
    private CommentService commentService; 

    @RequestMapping 
    public String findAll(Model model) { 
     model.addAttribute("posts", postService.findAll()); 
     return "post/post"; 
    } 

    @RequestMapping(value = "/click", method = RequestMethod.GET) 
    public String click() { 
     return "post/new"; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String addPost(@ModelAttribute Post post) { 
     postService.addPost(post); 
     return "redirect:/posts"; 
    } 

    @RequestMapping(value = "/{title}/find", method = RequestMethod.GET) 
    public String findByName(@PathVariable String title) { 
     postService.findByTitle(title); 
     return "redirect:/posts"; 
    } 

    @RequestMapping(value = "/{id}/see", method = RequestMethod.GET) 
    public String seeMessage(@PathVariable long id, Model model) { 
     Post post = postService.findById(id); 
     System.out.println("the id is " + id); 
     System.out.println("the value of comments are " +post.getComments().size()); 

     model.addAttribute("post", post); 
//  model.addAttribute("postMessage", post.getComments()); 

     return "post/postmessage"; 

    } 

    @RequestMapping(value = "/{id}/delete") 
    public String delete(@PathVariable long id) { 
     postService.delete(id); 
     return "redirect:/posts"; 
    } 

    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public String login(){ 
     return "login"; 
    } 

} 

CommentControllerは次のようになります。そして、ここで最初の、私はそれを行って、この後、私は設立のポストにコメントを追加するには、持っているもののidポストから見つけ、メソッドでaddCommentに注意を払います。

package com.pandora.controllers; 

import com.pandora.domain.Comment; 

import com.pandora.domain.Post; 
import com.pandora.services.CommentService; 
import com.pandora.services.PostService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import java.util.ArrayList; 
import java.util.List; 

@Controller 
@RequestMapping("/comments") 
public class CommentController { 

    @Autowired 
    private CommentService commentService; 

    @Autowired 
    private PostService postService; 

    @RequestMapping(value = "/post/{postId}/comment", method = RequestMethod.POST) 
    public String addComment(@PathVariable long postId, @ModelAttribute Comment comment){ 

     Post post = postService.findById(postId); 

     System.out.println(post.getId()); 
     System.out.println(comment.getMessage()); 

     List comments = new ArrayList(); 

     comments.add(commentService.addComment(comment)); 

     post.setComments(comments); 

     return "redirect:/posts"; 
    } 

    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET) 
    public String delete(@PathVariable long id){ 
     commentService.delete(id); 
     return "redirect:/posts"; 
    } 

} 

ポストエンティティは次のとおりです。

package com.pandora.services; 

import com.pandora.domain.Post; 
import com.pandora.domain.repositories.PostRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import java.util.List; 

@Service 
public class PostService { 

    @Autowired 
    private PostRepository postRepository; 

    public Post addPost(Post post){ 
     return postRepository.saveAndFlush(post); 
    } 

    public List findAll(){ 
     return postRepository.findAll(); 
    } 

    public Post findByTitle(String title){ 
     return postRepository.findByTitle(title); 
    } 

    public Post findById(long id){ 
     return postRepository.findOne(id); 
    } 

    public void delete(long id){ 
     postRepository.delete(id); 
    } 

} 

コメントエンティティは次のとおりです:

package com.pandora.domain; 

import lombok.Getter; 
import lombok.NoArgsConstructor; 
import lombok.Setter; 

import javax.persistence.*; 
import java.util.List; 

@Entity 
@Setter 
@Getter 
@NoArgsConstructor 
public class Post { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "post") 
    private List comments; 

    @Column(length = 10000) 
    private String message; 

    private String title; 
} 

PostServiceがある

package com.pandora.domain; 

import lombok.Getter; 
import lombok.NoArgsConstructor; 
import lombok.Setter; 

import javax.persistence.*; 

@Entity 
@Setter 
@Getter 
@NoArgsConstructor 
public class Comment { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    @ManyToOne(fetch = FetchType.EAGER) 
    private Post post; 

    private String message; 

} 

とCommentServiceは次のとおりです。

package com.pandora.services; 

import com.pandora.domain.Comment; 
import com.pandora.domain.repositories.CommentRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import java.util.List; 

@Service 
public class CommentService { 

    @Autowired 
    private CommentRepository commentRepository; 

    public Comment addComment(Comment comment){ 
     return commentRepository.saveAndFlush(comment); 
    } 

    public List findAll(){ 
     return commentRepository.findAll(); 
    } 

    public void delete(long id){ 
     commentRepository.delete(id); 
    } 

    public Comment findOne(long id){ 
     return commentRepository.findOne(id); 
    } 

} 

idで選択する投稿を表示するhtmlはここに注意してください。各divコンテナ。私はPostControllerのコメントから消えたリストから取得します。しかし、それは私がそれを行うとき、それは常に空ですので、それは動作しません。なぜそれが空であるのか分からない。

<!DOCTYPE html> 
 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" 
 
     layout:decorator="layout"> 
 

 
<div layout:fragment="content"> 
 

 
    <h2 align="center"> 
 
     <h2 align="left"> 
 
      <div th:text="${post.title}"></div> 
 
     </h2> 
 
    </h2> 
 

 
    <h4 align="center"> 
 
     <div th:text="${post.message}"></div> 
 
    </h4> 
 
    <hr/> 
 
    <h4>Comments:</h4> 
 
    <br/> 
 

 
    <h4> 
 
     <div th:each="comments : ${post}"> 
 
      
 
      <label for="username"><div th:inline="text">[[${#httpServletRequest.remoteUser}]]: </div> </label> 
 
      <div th:each="comment : ${comments.comments}"> 
 
       <div id="username" th:text="${comment}"></div> 
 
      </div> 
 
     </div> 
 
    </h4> 
 

 
    <br/> 
 
    <form method="post" name="comment_form" id="comment_form" th:action="@{'/comments/post/{id}/comment'(id=${post.id}) }" role="form"> 
 
     <div class="form-group"> 
 
      <label for="message">Comment</label> 
 
      <textarea rows="5" class="form-control" id="message" name="message"/> 
 
     </div> 
 
     <button type="submit" id="submit" class="btn btn-primary">Submit</button> 
 
    </form> 
 

 
</div> 
 

 
</html>

答えて

1

シナリオに基づいて、これは私が理解です:あなたはポストと、それに関連したいくつかのコメントがあります。ただし、特定の投稿のコメントを取得しようとすると、(ほかの投稿からも)すべてのコメントが表示されます。

と仮定すると、投稿があり、コメントを1つの投稿に関連付ける必要がある場合にのみコメントが表示されるため、コメントを別のエンティティとして扱わないことをお勧めします。 @Embeddableはこれを助けるかもしれない。

私の推薦は、このような何かをしようとするだろう:

はとして組み込み可能なコメントを加えます。この後

@Entity 
@Setter 
@Getter 
@NoArgsConstructor 
public class Post { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private long id; 

    @ElementCollection(fetch=FetchType.EAGER) 
    private List<Comment> comments = new ArrayList<>(); 

    @Column(length = 10000) 
    private String message; 

    private String title; 
} 

@Embeddable 
public class Comment { 
    private String message; 
    ... 
    ... 
    //Any other properties you might need to add 
} 

そしてポストエンティティで、以下の変更を行い

CommentServiceとCommentRepositoryが必要なくなるため、CommentServiceとCommentRepositoryを緩めたいかもしれません。これが役に立ったら教えてください。

+0

どのように動作するのですか?投稿コメントに埋め込まれていれば、とにかくIDで投稿を見つけて、保存されているコメントをすべて表示する必要があります。どのようにしてこのようにしても修正する方法はありますが、私はちょうど正確に理解できません。CommentControllerのメソッド "addComment"を見て、html投稿からクリックするとコメントを追加します。質問は正確にコントローラのためにどのくらい保存するのですか?だから私は最初にID投稿で見つけて、この投稿IDに正確にコメントを追加しなければならないのです。 – Dave

+0

と、これらのコメントをすべてPostControllerのメソッド "seeMessage"で取得すると、 – Dave

1

コントローラも変更する必要があります。このようなことを考えてみると、今はコメントがない投稿があります。ユーザーその後のPostControllerメソッドにコメントを投稿は言う:

@RequestMapping(value = "/post/{postId}/addComment", method = RequestMethod.POST) 
      public String addPost(@PathVariable("postId")long id, @ModelAttribute Comment comment) { 
       postService.addComment(id, comment); 
       return "redirect:/to_whatever"; 
      } 

そして、あなたのPostServiceであなたは、以下の機能を追加したい場合があります:

public Post addComment(long id, Comment comment){ 
     Post post = postRepository.findOne(id); 
     post.getComments().add(comment); 
     return postRepository.saveAndFlush(post); 
    } 
は、それに応じてUIポストコールを変更し

。また、これによって検索部分も達成されます。あなたが行う必要があるのは、投稿(またはその他の一意の識別子)で投稿を見つけ、post.getComments()を実行するだけです。

Embeddableは、投稿とコメントの間にOneToManyの種類の関係を確立します。 this for more detailsを参照してください。

データベースレベルでは、このようなものがあります:

POST TABLE 
post_id  post_name 
    1   A 
    2   B 



POST_COMMENTS TABLE 
post_id  message 
    1   C 
    1   D 
    2   E 
    2   F 

をこの問題をクリア願っています。