前提・実現したいこと
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
1 /** 2 * ユーザー情報 リクエストデータ 3 */ 4@Data 5public class UserRequest implements Serializable { 6 /** 7 * No 8 */ 9 private String user_no; 10 /** 11 * 名前 12 */ 13 private String user_name; 14 /** 15 * 役職 16 */ 17 private String post_name; 18 19 20 public String getUser_no(){ 21 return user_no; 22 } 23 24 public String getUser_name(){ 25 return user_name; 26 } 27 28 public String getPost_name(){ 29 return post_name; 30 31 } 32}
User
1/** 2 * ユーザー情報 Entity 3 */ 4@Entity 5@Data 6@Table(name = "mst_user") 7public class User implements Serializable { 8 /** 9 * No 10 */ 11 @Id 12 @Column(name = "user_no") 13 @GeneratedValue(strategy = GenerationType.IDENTITY) 14 private String user_no; 15 /** 16 * 名前 17 */ 18 @Column(name = "user_name") 19 private String user_name; 20 /** 21 * 役職 22 */ 23 @Column(name = "post_name") 24 private String post_name; 25 26 27 public String getUser_no(){ 28 return user_no; 29 } 30 31 public void setUser_no(String string){ 32 this.user_no = string; 33 } 34 35 public String getUser_name(){ 36 return user_name; 37 } 38 39 public void setUser_name(String user_name){ 40 this.user_name = user_name; 41 } 42 43 public String getPost_name(){ 44 return post_name; 45 46 } 47 48 public void setPost_name(String post_name){ 49 this.post_name = post_name; 50 } 51 } 52
UserRepository
1/** 2 * ユーザー情報 Repository 3 */ 4@Repository 5public interface UserRepository extends JpaRepository<User, Long> { 6}
UserService
1/** 2 * ユーザー情報 Service 3 */ 4@Service 5@Transactional(rollbackFor = Exception.class) 6public class UserService { 7 /** 8 * ユーザー情報 Repository 9 */ 10 @Autowired 11 UserRepository userRepository; 12 /** 13 * ユーザー情報 全検索 14 * @return 検索結果 15 */ 16 public List<User> searchAll() { 17 return userRepository.findAll(); 18 } 19 /** 20 * ユーザー情報新規登録 21 * @param user ユーザー情報 22 */ 23 public void create(UserRequest userRequest) { 24 userRepository.save(CreateUser(userRequest)); 25 } 26 /** 27 * ユーザーTBLエンティティの生成 28 * @param userRequest ユーザー情報リクエストデータ 29 * @return ユーザーTBLエンティティ 30 */ 31 private User CreateUser(UserRequest userRequest) { 32 User mst_user = new User(); 33 mst_user.setUser_no(userRequest.getUser_no()); 34 mst_user.setUser_name(userRequest.getUser_name()); 35 mst_user.setPost_name(userRequest.getPost_name()); 36 37 return mst_user; 38 } 39}
add
1<!DOCTYPE html> 2<html xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:th="http://www.thymeleaf.org"> 4<head> 5<title>ユーザー新規登録</title> 6<link href="/css/add.css" rel="stylesheet"></link> 7<meta charset="utf-8" /> 8</head> 9<body> 10 <h1>ユーザー新規登録</h1> 11 <form th:action="@{/user/create}" th:object="${userRequest}" 12 method="post"> 13 <table> 14 <tr> 15 <th class="cell_title">No</th> 16 <th class="cell_required">※</th> 17 <td><input type="text" th:field="*{user_no}"></td> 18 <tr> 19 <th class="cell_title">名前</th> 20 <th class="cell_required">※</th> 21 <td><input type="text" th:field="*{user_name}"></td> 22 </tr> 23 <tr> 24 <th class="cell_title">役職</th> 25 <th class="cell_required"></th> 26 <td><input type="text" th:field="*{post_name}"></td> 27 </tr> 28 </table> 29 <div class="btn_area_center"> 30 <input type="submit" value="登録" class="btn"> 31 </div> 32 </form> 33</body> 34</html>
list
1<!DOCTYPE html> 2<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"> 3<head> 4<title>ユーザー情報一覧</title> 5<link href="/css/list.css" rel="stylesheet"></link> 6<meta charset="utf-8" /> 7</head> 8<body> 9 <h1>ユーザー情報一覧</h1> 10 <div> 11 <a th:href="@{/user/add}">新規登録はこちら</a> 12 </div> 13 <table> 14 <thead> 15 <tr> 16 <th>No</th> 17 <th>名前</th> 18 <th>役職</th> 19 </tr> 20 </thead> 21 <tbody> 22 <tr th:each="user : ${userlist}" th:object="${user}"> 23 <td class="center" th:text="*{user_no}"></td> 24 <td th:text="*{user_name}"></td> 25 <td th:text="*{post_name}"></td> 26 </tr> 27 </tbody> 28 </table> 29</body> 30</html>
application
1spring.datasource.url=jdbc:mariadb://localhost:3306/a 2spring.datasource.username=**** 3spring.datasource.password=******** 4spring.datasource.driver-class-name=org.mariadb.jdbc.Driver 5spring.jpa.open-in-view= true 6server.port=55000
試したこと
補足情報(FW/ツールのバージョンなど)
Eclipse Eclipse 4.5 Mars・jdk1.8.0_241・Spring boot 4.14.0・MariaDB 5.5.62
あなたの回答
tips
プレビュー