前提・実現したいこと
ThymeleafでPOSTしたデータをDBへ登録したい
データをDBへ登録しようとする、エラーで表示されません
どのようにすればいいかご教授いただけないでしょうか。
発生している問題・エラーメッセージ
エラーで表示できません
WARN 15000 --- [io-55000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1364, SQLState: HY000
ERROR 15000 --- [io-55000-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : (conn=591) Field 'user_no' doesn't have a default value
ERROR 15000 --- [io-55000-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: could not execute statement; nested exception is org.hibernate.exception.JDBCConnectionException: could not execute statement] with root cause
java.sql.SQLException: Field 'user_no' doesn't have a default value
### 該当のソースコード ```UserController /** * ユーザー情報 Controller */ @Controller public class UserController { /** * ユーザー情報 Service */ @Autowired UserService userService; /** * ユーザー情報一覧画面を表示 * @param model Model * @return ユーザー情報一覧画面 */ @RequestMapping(value = "/user/list", method = RequestMethod.GET) public String displayList(Model model) { List<User> userlist = userService.searchAll(); model.addAttribute("userlist", userlist); return "user/list"; } /** * ユーザー新規登録画面を表示 * @param model Model * @return ユーザー情報一覧画面 */ @RequestMapping(value = "/user/add", method = RequestMethod.GET) public String displayAdd(Model model) { model.addAttribute("userRequest", new UserRequest()); return "user/add"; } /** * ユーザー新規登録 * @param userRequest リクエストデータ * @param model Model * @return ユーザー情報一覧画面 */ @RequestMapping(value = "/user/create", method = RequestMethod.POST) public String create(@ModelAttribute UserRequest userRequest, Model model) { // ユーザー情報の登録 userService.create(userRequest); return "redirect:/user/list"; } }
UserRequest
/** * ユーザー情報 リクエストデータ */ @Data public class UserRequest implements Serializable { /** * No */ private String user_no; /** * 名前 */ private String user_name; /** * 役職 */ private String post_name; public String getUser_no(){ return user_no; } public String getUser_name(){ return user_name; } public String getPost_name(){ return post_name; } }
User
/** * ユーザー情報 Entity */ @Entity @Data @Table(name = "mst_user") public class User implements Serializable { /** * No */ @Id @Column(name = "user_no") @GeneratedValue(strategy = GenerationType.IDENTITY) private String user_no; /** * 名前 */ @Column(name = "user_name") private String user_name; /** * 役職 */ @Column(name = "post_name") private String post_name; public String getUser_no(){ return user_no; } public void setUser_no(String string){ this.user_no = string; } public String getUser_name(){ return user_name; } public void setUser_name(String user_name){ this.user_name = user_name; } public String getPost_name(){ return post_name; } public void setPost_name(String post_name){ this.post_name = post_name; } }
UserRepository
/** * ユーザー情報 Repository */ @Repository public interface UserRepository extends JpaRepository<User, Long> { }
UserService
/** * ユーザー情報 Service */ @Service @Transactional(rollbackFor = Exception.class) public class UserService { /** * ユーザー情報 Repository */ @Autowired UserRepository userRepository; /** * ユーザー情報 全検索 * @return 検索結果 */ public List<User> searchAll() { return userRepository.findAll(); } /** * ユーザー情報新規登録 * @param user ユーザー情報 */ public void create(UserRequest userRequest) { userRepository.save(CreateUser(userRequest)); } /** * ユーザーTBLエンティティの生成 * @param userRequest ユーザー情報リクエストデータ * @return ユーザーTBLエンティティ */ private User CreateUser(UserRequest userRequest) { User mst_user = new User(); mst_user.setUser_no(userRequest.getUser_no()); mst_user.setUser_name(userRequest.getUser_name()); mst_user.setPost_name(userRequest.getPost_name()); return mst_user; } }
add
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <title>ユーザー新規登録</title> <link href="/css/add.css" rel="stylesheet"></link> <meta charset="utf-8" /> </head> <body> <h1>ユーザー新規登録</h1> <form th:action="@{/user/create}" th:object="${userRequest}" method="post"> <table> <tr> <th class="cell_title">No</th> <th class="cell_required">※</th> <td><input type="text" th:field="*{user_no}"></td> <tr> <th class="cell_title">名前</th> <th class="cell_required">※</th> <td><input type="text" th:field="*{user_name}"></td> </tr> <tr> <th class="cell_title">役職</th> <th class="cell_required"></th> <td><input type="text" th:field="*{post_name}"></td> </tr> </table> <div class="btn_area_center"> <input type="submit" value="登録" class="btn"> </div> </form> </body> </html>
list
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> <head> <title>ユーザー情報一覧</title> <link href="/css/list.css" rel="stylesheet"></link> <meta charset="utf-8" /> </head> <body> <h1>ユーザー情報一覧</h1> <div> <a th:href="@{/user/add}">新規登録はこちら</a> </div> <table> <thead> <tr> <th>No</th> <th>名前</th> <th>役職</th> </tr> </thead> <tbody> <tr th:each="user : ${userlist}" th:object="${user}"> <td class="center" th:text="*{user_no}"></td> <td th:text="*{user_name}"></td> <td th:text="*{post_name}"></td> </tr> </tbody> </table> </body> </html>
application
spring.datasource.url=jdbc:mariadb://localhost:3306/a spring.datasource.username=**** spring.datasource.password=******** spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spring.jpa.open-in-view= true server.port=55000
試したこと
補足情報(FW/ツールのバージョンなど)
Eclipse Eclipse 4.5 Mars・jdk1.8.0_241・Spring boot 4.14.0・MariaDB 5.5.62
まだ回答がついていません
会員登録して回答してみよう