実現したいこと
更新機能の実装をしたい
前提
tymeleaf
Spring Boot
Spring data JPA
等を使い社員情報管理システムを作成しております.
更新機能を実装中に以下のエラーが発生いたしました.
発生している問題・エラーメッセージ
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexController': Unsatisfied dependency expressed through field 'empRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository' defined in jp.co.sss.sys.repository.EmployeeRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract jp.co.sss.sys.entity.Employee jp.co.sss.sys.repository.EmployeeRepository.saveEmpName(java.lang.String,java.lang.String,int)! Reason: Failed to create query for method public abstract jp.co.sss.sys.entity.Employee jp.co.sss.sys.repository.EmployeeRepository.saveEmpName(java.lang.String,java.lang.String,int)! No property saveEmpName found for type Employee!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract jp.co.sss.sys.entity.Employee jp.co.sss.sys.repository.EmployeeRepository.saveEmpName(java.lang.String,java.lang.String,int)! No property saveEmpName found for type Employee!
該当のソースコード
EmployeeRepository.java
1package jp.co.sss.sys.repository; 2import org.springframework.data.jpa.repository.JpaRepository; 3import org.springframework.stereotype.Repository; 4import org.springframework.transaction.annotation.Transactional; 5import jp.co.sss.sys.entity.Employee; 6/** 7 * リポジトリークラス 8 * 9 * 10 */ 11@Transactional 12@Repository 13 14public interface EmployeeRepository extends JpaRepository<Employee, String> { 15 16 Employee findByEmpIdAndPassword(String empId, String password); 17 18 Employee saveEmpName(String empName, String password, int gender); 19} 20
Employee.java
1package jp.co.sss.sys.entity; 2import java.util.Date; 3import javax.persistence.Column; 4import javax.persistence.Entity; 5import javax.persistence.Id; 6import javax.persistence.Table; 7import javax.validation.constraints.NotEmpty; 8import javax.validation.constraints.Size; 9 10//import org.hibernate.annotations.NamedQuery; 11/** 12 * エンティティクラス 13 * 14 * 15 */ 16@Entity 17 18@Table(name = "employee") 19public class Employee { 20 21 22 /* 23 * 社員番号 24 */ 25 @Id 26 @Column(name ="emp_id" ) 27 @NotEmpty(message = "社員番号は入力必須項目です") 28 @Size(max = 5, message = "社員番号は5文字以内で入力してください") 29 private String empId; 30 31 @Column (name="emp_name") 32 @NotEmpty(message = "パスワードは入力必須項目です") 33 @Size(max = 16, message = "パスワードは16文字以内で入力してください") 34 private String empName; 35 36 @Column (name="password") 37 private String password; 38 39 @Column (name="birthday") 40 private Date birthday; 41 42 @Column (name="gender") 43 private int gender; 44 45 @Column (name="delete_at") 46 private Boolean delete; 47 48 public void setEmpId(String empId) { 49 this.empId = empId; 50 } 51 public String getEmpId() { 52 return empId; 53 } 54 55 56 public void setEmpName(String empName) { 57 this.empName = empName; 58 } 59 public String getEmpName() { 60 return empName; 61 } 62 63 64 public void setPassword(String password) { 65 this.password = password; 66 } 67 public String getPassword() { 68 return password; 69 } 70 71 72 public void setBirthday(Date birthday) { 73 this.birthday = birthday; 74 } 75 public Date getBirthday() { 76 return birthday; 77 } 78 79 80 public void setGender(int gender) { 81 this.gender = gender; 82 } 83 public int getGender() { 84 return gender; 85 } 86 87 88 public void setDelete(Boolean delete) { 89 this.delete = delete; 90 } 91 public Boolean getDelete() { 92 return delete; 93 } 94 95 96 97 98 99} 100 101 102 103 104
IndexController.java
1package jp.co.sss.sys.controller; 2import java.text.ParseException; 3import java.util.List; 4 5import javax.servlet.http.HttpServletRequest; 6import javax.servlet.http.HttpServletResponse; 7import javax.servlet.http.HttpSession; 8 9import org.springframework.beans.factory.annotation.Autowired; 10import org.springframework.stereotype.Controller; 11import org.springframework.ui.Model; 12import org.springframework.validation.BindingResult; 13import org.springframework.validation.annotation.Validated; 14import org.springframework.web.bind.annotation.PathVariable; 15import org.springframework.web.bind.annotation.RequestMapping; 16import org.springframework.web.bind.annotation.RequestMethod; 17import org.springframework.web.bind.annotation.SessionAttributes; 18 19import jp.co.sss.sys.entity.Employee; 20import jp.co.sss.sys.form.LoginForm; 21import jp.co.sss.sys.repository.EmployeeRepository; 22 23/** 24 * コントローラークラス 25 * 26 * 27 */ 28 29@Controller 30@SessionAttributes(types = Employee.class) 31public class IndexController { 32 33 @Autowired 34 EmployeeRepository empRepository; 35 LoginForm loginform; 36 37 /** 38 * ログイン画面を表示する 39 * @param loginForm 40 * @return login.html 41 */ 42 @RequestMapping(path = "/login", method = RequestMethod.GET) 43 public String login( LoginForm loginForm,BindingResult br,Model model) { 44 return "login"; 45 } 46 @Autowired 47 HttpSession session; 48 49 50 51 // 処理 52 53 54 /** 55 * 入力された値を元にログイン認証し、トップ画面に遷移する 56 * 57 * @param req 58 * @param res 59 * @param loginForm 60 * @return top.html 61 */ 62 @RequestMapping(path = "/top", method = RequestMethod.POST) 63 public String login(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) { 64 //ログインした人の情報 65 String empId = req.getParameter("empId"); 66 String password = req.getParameter("password"); 67 68 69 Employee employee = empRepository.findByEmpIdAndPassword(empId, password); 70 71 //セッションデータ設定 72 session.setAttribute("userInfo",employee); 73 //ログインユーザー情報 74 model.addAttribute("employee",employee); 75 76 //ログインチェック 77 if(employee == null) { 78 //存在しない場合 79 return "login"; 80 81 }else { 82 83 //存在した場合 84 //社員情報一覧 85 List<Employee> empAll= empRepository.findAll(); 86 model.addAttribute("empAll",empAll); 87 88 89 90 91 return "top"; 92 93 } 94 } 95 96 @RequestMapping(path = "/top", method = RequestMethod.GET) 97 public String top(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) { 98 List<Employee> empAll= empRepository.findAll(); 99 model.addAttribute("empAll",empAll); 100 101 return "top"; 102 103 } 104 105 106 107 //ユーザー更新入力情報 th:object empPost 108 @RequestMapping(path = "/mypage", method = RequestMethod.POST) 109 public String empUser(@Validated @PathVariable("emp_id")LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) throws ParseException { 110 session = req.getSession(); 111 112 String empName = req.getParameter("empName"); 113 String password = req.getParameter("password"); 114 115 String savegender = req.getParameter("gender"); 116 117 int gender = Integer.parseInt(savegender); 118 119 120// 121 122 123 124 125 126 Employee updateEmployee=empRepository.saveEmpName(empName,password,gender); 127 128 return "edit_fin"; 129 130 131 } 132 // Employee updateEmployee = empRepository.saveAll(savegender, savegender, null, gender); 133 134 135 //ログインユーザー情報 136 137 138 139 140 141 142 //マイページリンク押下,既存情報の出力 143 @RequestMapping(path = "/mypage", method = RequestMethod.GET) 144 public String empLink(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) { 145 session = req.getSession(); 146 Object userInfo=session.getAttribute("userInfo"); 147 model.addAttribute("userInfo",userInfo); 148 149 150 151 152 153 154 155 156 return "mypage"; 157 158 159 } 160 161} 162 163 164 165 166 167 168 169 170 171 172 173
mypage.html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8" /> 5<link th:href="@{/css/style.css}" rel="stylesheet" /> 6<link th:href="@{/css/layout.css}" rel="stylesheet" /> 7<title>マイページ変更・確認画面</title> 8</head> 9<body> 10 <!-- ヘッダー --> 11 12 <header th:include="layout/header :: head"> 13 14 15 </header> 16 17 <!-- サイドバー --> 18 <aside th:include="layout/aside :: side"></aside> 19 <!-- メイン --> 20 <article class="mypage"> 21 <form action="" th:action="@{/mypage}" th:field="${userUpdate}" 22 method="post"> 23 <h3 class="page_title">マイページ変更・確認画面</h3> 24 25 26 <table class="table employee"> 27 28 <tr th:each="userInfo : ${userInfo}" th:object="${userInfo}"> 29 <tr> 30 <th class="cell_title">社員番号</th> 31 <td th:text="*{userInfo.empId}"></td> 32 </tr> 33 <tr> 34 <th class="cell_title">名前</th> 35 <td><input type="text" name="empName" 36 th:value="${userInfo.empName}" /></td> 37 38 </tr> 39 <tr> 40 <th class="cell_title">パスワード</th> 41 <td><input type="password" name="password" 42 th:value="${userInfo.password}" /></td> 43 44 </tr> 45 <tr> 46 <th class="cell_title">生年月日</th> 47 <td><input type="text" name="birthday" 48 th:value="${userInfo.birthday}" /></td> 49 50 </tr> 51 <tr> 52 <th class="cell_title">性別</th> 53 <td><input type="radio" name="gender" 54 th:value="${userInfo.gender}" 55 th:switch="*{userInfo.gender == 1}" 56 th:checked="${userInfo.gender == 1}" /> 男 <input type="radio" 57 name="gender" th:switch="*{userInfo.gender == 2}" 58 th:checked="${userInfo.gender == 2}"> 女</td> 59 60 61 </tr> 62 </table> 63 <div class="btn_area_center"> 64 <input type="submit" value="変更確定" class="btn"> 65 </div> 66 </form> 67 </article> 68 <!-- フッダー --> 69 <footer th:include="layout/footer :: foot"></footer> 70 71</body> 72</html>
試したこと
IndexController.javaとEmployeeRepository.java内にあります,
saveEmpNameメソッドの引数を変更,
saveEmpNameメソッドのメソッド名の変更
⇨元々save()でした
補足情報(FW/ツールのバージョンなど)
Java SE 16
eclipce 2022
![guest](/img/icon/icnUserSample.jpg)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/03/22 01:23