質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

0回答

875閲覧

springboot/jpaのデータベースで条件に一致しているものを全件取得したい

退会済みユーザー

退会済みユーザー

総合スコア0

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2021/08/13 05:23

前提・実現したいこと

springboot/jpaで掲示板のような物を作成しています。
しかし、DBの検索が上手くいかず苦戦しています。

発生している問題・エラーメッセージ

指定のIDに対して、コメント用のDBにあるthreadIdと一致しているデータを全件取得したいが、調べても解決できなかった。

該当のソースコード

コントローラ

java

1package com.example.demo.controller; 2 3import javax.servlet.http.HttpServletRequest; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.context.annotation.Scope; 7import org.springframework.stereotype.Controller; 8import org.springframework.ui.Model; 9import org.springframework.validation.BindingResult; 10import org.springframework.validation.annotation.Validated; 11import org.springframework.web.bind.annotation.GetMapping; 12import org.springframework.web.bind.annotation.PathVariable; 13import org.springframework.web.bind.annotation.PostMapping; 14 15import com.example.demo.db.CommentForm; 16import com.example.demo.db.InputForm; 17import com.example.demo.repositry.CommentRepository; 18import com.example.demo.repositry.DatabaseRepository; 19 20@Scope("session") 21@Controller 22public class ForumController { 23 24 @Autowired 25 private DatabaseRepository repository; 26 27 @Autowired 28 private CommentRepository commentRepo; 29 30 @GetMapping("/") 31 public String getHome(CommentForm commentForm, InputForm inputForm, Model model) { 32 33 Iterable<InputForm> inputList = repository.findAll(); 34 model.addAttribute("inputList", inputList); 35 36 Iterable<CommentForm> commentList = commentRepo.findAll(); 37 model.addAttribute("commentList", commentList); 38 39 return "/home"; 40 } 41 42 @PostMapping("/createThread") 43 public String createThread(@Validated InputForm inputForm, BindingResult error, Model model, 44 CommentForm commentForm) { 45 if (error.hasErrors()) { 46 Iterable<InputForm> inputList = repository.findAll(); 47 model.addAttribute("inputList", inputList); 48 49 Iterable<CommentForm> commentList = commentRepo.findAll(); 50 model.addAttribute("commentList", commentList); 51 return "/home"; 52 } 53 54 repository.saveAndFlush(inputForm); 55 56 return "/relay"; 57 } 58 59 @GetMapping("/getThread/{id}") 60 public String getThread(@PathVariable("id") Long id, CommentForm commentForm, InputForm inputForm, Model model) { 61 commentForm.setThreadId(id); 62 63 Iterable<InputForm> inputList = repository.findAll(); 64 model.addAttribute("inputList", inputList); 65 66 //すれっどIDと一致してるコメントを入れる 67 Iterable<CommentForm> commentList = commentRepo.findAll(); 68 model.addAttribute("commentList", commentList); 69 70 return "/home"; 71 } 72 73 @PostMapping("/postComment") 74 public String postComment(@Validated CommentForm commentForm, BindingResult error, InputForm inputForm, 75 Model model) { 76 if (error.hasErrors()) { 77 78 Iterable<InputForm> inputList = repository.findAll(); 79 model.addAttribute("inputList", inputList); 80 81 Iterable<CommentForm> commentList = commentRepo.findAll(); 82 model.addAttribute("commentList", commentList); 83 return "/home"; 84 } 85 86 commentRepo.saveAndFlush(commentForm); 87 88 return "/relay"; 89 } 90}

コメントエンティティ

java

1package com.example.demo.db; 2 3import javax.persistence.Entity; 4import javax.persistence.GeneratedValue; 5import javax.persistence.Id; 6import javax.validation.constraints.NotBlank; 7 8@Entity 9public class CommentForm { 10 11 @Id 12 @GeneratedValue 13 Long id; 14 public Long getId() { 15 return id; 16 } 17 public void setId(Long id) { 18 this.id = id; 19 } 20 21 22 Long threadId; 23 public Long getThreadId() { 24 return threadId; 25 } 26 public void setThreadId(Long threadId) { 27 this.threadId = threadId; 28 } 29 30 @NotBlank(message = "未入力です。") 31 String comment; 32 public String getComment() { 33 return comment; 34 } 35 public void setComment(String comment) { 36 this.comment = comment; 37 } 38} 39

スレッドエンティティ

java

1package com.example.demo.db; 2 3import javax.persistence.Entity; 4import javax.persistence.GeneratedValue; 5import javax.persistence.Id; 6import javax.validation.constraints.NotBlank; 7 8 9/** 10 * データベースとのやり取りで使用するレコード単位の情報と 11 * バリデーションメッセージの設定 12 * @author goto.shinichiro 13 * 14 */ 15@Entity 16public class InputForm { 17 18 @Id 19 @GeneratedValue 20 Long id; 21 22 public Long getId() { 23 return id; 24 } 25 26 public void setId(Long id) { 27 this.id = id; 28 } 29 30 @NotBlank(message = "未入力です。") 31 String threadName; 32 33 public String getThreadName() { 34 return threadName; 35 } 36 37 public void setThreadName(String threadName) { 38 this.threadName = threadName; 39 } 40}

表示部分

html

1<!DOCTYPE html> 2<html lang="ja" xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:th="http://www.thymeleaf.org"> 4 5<head> 6<link rel="stylesheet" th:href="@{/style.css}"> 7<title>みかんふぉーらむ</title> 8<a id="jump2"></a> 9<h1 class=title>みかんふぉーらむ</h1> 10</head> 11 12<div class="header"> 13<a href="#jump2">TOPに移動</a><br> 14<a href="#jump">コメント入力欄に移動</a> 15</div> 16 17<div class="comment" th:object="${commentList}"> 18 <tbody th:if="${commentList.size()>=1}"> 19 <tr th:each="commentList:${commentList}" th:object="${commentList}"> 20 <p th:text="*{comment}" class="comment"></p> 21 <br> 22 </tr> 23 </tbody> 24</div> 25 26 27 28<div class="inputComment"> 29 <form method="post" th:action="@{/postComment}" 30 th:object="${commentForm}" onsubmit="return false;"> 31 <td><textarea th:field="*{comment}" class="inputComment"></textarea></td> <span 32 th:if="${#fields.hasErrors('comment')}" th:errors="*{comment}" 33 class="error"></span><br> <input type="button" 34 onclick="submit();" value="かきこむ" id="inputComment"/> 35 <a id="jump"></a> 36 </form> 37</div> 38 39<body> 40 <form method="post" th:action="@{/createThread}" 41 th:object="${inputForm}" onsubmit="return false;"> 42 <input type="text" th:field="*{threadName}" class="threadForm" /> <span 43 th:if="${#fields.hasErrors('threadName')}" th:errors="*{threadName}" 44 class="error"></span><br> <input type="button" value="すれっどをつくる" 45 class="threadCreate" onclick="submit();" /> 46 </form> 47</body> 48 49 50 51<body th:object="${inputList}"> 52 <p class=threadList>すれっどいちらん</p> 53 <div class="threadList"> 54 <tbody th:if="${inputList.size()>=1}"> 55 <tr th:each="inputList:${inputList}" th:object="${inputList}"> 56 <a th:text="*{threadName}" class="scroll" 57 th:href="@{/getThread/{id}(id=*{id})}"></a> 58 <br> 59 </tr> 60 </tbody> 61 </div> 62</body> 63 64</html>

試したこと

@Query、findById、findAllByIdで失敗

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問