前提・実現したいこと
商品情報を登録を押してDBに保存してform画面に戻りたいのですががテーブルがないとなります
発生している問題・エラーメッセージ
There was an unexpected error (type=Internal Server Error, status=500). could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement Caused by: java.sql.SQLSyntaxErrorException: Table 'demo.inquiry2' doesn't exist at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1092) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1040) at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1347) at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1025) at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:197) ... 108 more
該当のソースコード
package com.example.demo.form; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import lombok.Data; @Data @Entity @Table(name = "inquiry2") public class InquiryForm2 implements Serializable { private static final long serialVersionUID = -6647247658748349084L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @NotBlank @Size(max = 10) private String commodity; @NotNull private Integer price; @NotBlank @Size(max = 400) private String information; public void clear() { commodity = null; price = null; information = null; } }
<!doctype html> <html xmlns:th="http://www.thymeleaf.org" lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>ポートフォリオサイト</title> <link rel="stylesheet" media="all" th:href="@{/css/ress.min.css}" /> <link rel="stylesheet" media="all" th:href="@{/css/style.css}" /> <script th:src="@{/js/jquery-2.1.4.min.js}"></script> <script th:src="@{/js/style.js}"></script> <!-- Favicon --> <link rel="icon" type="image/png" th:href="@{/img/favicon.png}"> </head> <body> <header> <div class="container"> <div class="row"> <div class="col span-12"> <div class="head"> <h1> <a th:href="@{/}">PORTFOLIO</a> </h1> </div> </div> </div> <div class="row"> <div class="col span-12"> <nav> <div id="open"> <img th:src="@{/img/button.png}"> </div> <div id="close"> <img th:src="@{/img/button2.png}"> </div> <div id="navi"> <ul> <li><a th:href="@{/}">ホーム</a></li> <li><a th:href="@{/form}">お問い合わせ</a></li> <li><a th:href="@{/form2}">商品登録</a></li> </ul> </div> </nav> </div> </div> </div> </header> <div class="mainimg"> <img th:src="@{/img/subimg.jpg}" alt="お問い合わせ画像"> </div> <main> <article> <div class="container"> <div class="row"> <div class="col span-12"> <div class="breadcrumb"> <ul> <li><a th:href="@{/demo}">ホーム</a> > 商品登録</li> </ul> </div> <th:block th:if="message != null"> <h2 th:class="msg" th:text="${message}"></h2> </th:block> <h2 class="underline">商品情報</h2> <form th:action="@{/form2}" th:object="${inquiryForm2}" method="post"> <p> <label for="name">商品名</label> <input class="full-width" type="text" id="commodity" name="commodity" maxlength="10" th:field="*{commodity}"> <span th:if="${#fields.hasErrors('commodity')}" th:errors="*{commodity}" th:class="msg"></span> </p> <p> <label for="email">値段</label> <input class="full-width" type="text" id="price" name="price" th:field="*{price}"><span th:if="${#fields.hasErrors('price')}" th:errors="*{price}" th:class="msg"></span> </p> <p> <label for="message">商品情報</label> <textarea class="full-width" id="information" name="information" th:field="*{information}"></textarea> <span th:if="${#fields.hasErrors('information')}" th:errors="*{information}" th:class="msg"></span> </p> <p> <input class="button" type="submit" value="登録"> </p> </form> </div> </div> </div> </article> </main> <footer> <div class="container"> <div class="row"> <div class="col span-12"> <h5>お知らせ</h5> <p>電話での問合せは承っておりません。</p> <p>回答にお時間頂くことがございます。予めご了承ください。</p> </div> </div> </div> </footer> <p id="pagetop"> <a href="#">TOP</a> </p> </body> </html>
package com.example.demo.repositries; import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import com.example.demo.form.InquiryForm2; @Repository public interface InquiryRepository2 extends JpaRepository<InquiryForm2, String> { Optional<InquiryForm2> findById(String id); List<InquiryForm2> findAll(); }
CREATE TABLE IF NOT EXISTS inquiry2( id INT PRIMARY KEY, commodity VARCHAR(50), price INT, information VARCHAR(50) );
package com.example.demo.controllers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.example.demo.form.InquiryForm; import com.example.demo.form.InquiryForm2; import com.example.demo.repositries.InquiryRepository; import com.example.demo.repositries.InquiryRepository2; @Controller @RequestMapping("/") public class RootController { @Autowired InquiryRepository repository; @GetMapping public String index() { return "root/index"; } @GetMapping("/form") public String form(InquiryForm inquiryForm) { return "root/form"; } @PostMapping("/form") public String form(@Validated InquiryForm inquiryForm, BindingResult bindingResult, Model model) { if (bindingResult.hasErrors()) { return "root/form"; } // RDBと連携できることを確認しておきます。 repository.saveAndFlush(inquiryForm); inquiryForm.clear(); model.addAttribute("message", "お問い合わせを受け付けました。"); return "root/form"; } @Autowired InquiryRepository2 repository2; @GetMapping("/form2") public String form2(InquiryForm2 inquiryForm2) { return "root/form2"; } @PostMapping("/form2") //@Validated バリデーションが実行 //BindingResult エラーを格納する public String form2(@Validated InquiryForm2 inquiryForm2, BindingResult bindingResult2, Model model2) { if (bindingResult2.hasErrors()) { return "root/form2"; } // repository2.saveAndFlush(inquiryForm2); inquiryForm2.clear(); model2.addAttribute("message", "商品登録が完了しました"); return "root/form2"; } }
@Entityで自動でテーブルが作られるを思ったのですが作れてないのでSQL文を書きました
@Entityここでつくれてない時点でおかしいのでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/08 07:22
2021/05/08 07:56
2021/05/08 09:09
2021/05/08 09:34