org.springframework.beans.factory.UnsatisfiedDependencyException:エラーの解決方法について
Q&A
Closed
解決したいこと
spring bootを使用し,社員情報管理システムの作成をしております.
最初は正常に作動しておりましたが,DBのテーブルを誤って削除をしてしまい作り直した際,下記の様なエラーが発生しました.
解決方法などをご教授いただければと思います.
発生している問題・エラー
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 java.lang.IllegalArgumentException: Not a managed type: class jp.co.sss.sys.entity.Employee
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.9.jar:5.3.9]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.9.jar:5.3.9]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) [spring-boot-2.5.4.jar:2.5.4]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) [spring-boot-2.5.4.jar:2.5.4]
at jp.co.sss.sys.CmindSysApplication.main(CmindSysApplication.java:12) [classes/:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.5.4.jar:2.5.4]
Caused by: 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 java.lang.IllegalArgumentException: Not a managed type: class jp.co.sss.sys.entity.Employee
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.9.jar:5.3.9]
... 25 more
Caused by: java.lang.IllegalArgumentException: Not a managed type: class jp.co.sss.sys.entity.Employee
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:582) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:75) ~[spring-data-jpa-2.5.4.jar:2.5.4]
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.5.4.jar:2.5.4]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:228) ~[spring-data-jpa-2.5.4.jar:2.5.4]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:178) ~[spring-data-jpa-2.5.4.jar:2.5.4]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:161) ~[spring-data-jpa-2.5.4.jar:2.5.4]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:72) ~[spring-data-jpa-2.5.4.jar:2.5.4]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:319) ~[spring-data-commons-2.5.4.jar:2.5.4]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:323) ~[spring-data-commons-2.5.4.jar:2.5.4]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.5.4.jar:2.5.4]
at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.5.4.jar:2.5.4]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:329) ~[spring-data-commons-2.5.4.jar:2.5.4]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.5.4.jar:2.5.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.9.jar:5.3.9]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.9.jar:5.3.9]
... 25 more
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
mypage.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<link th:href="@{/css/style.css}" rel="stylesheet" />
<link th:href="@{/css/layout.css}" rel="stylesheet" />
<title>マイページ変更・確認画面</title>
</head>
<body>
<!-- ヘッダー -->
<header th:include="layout/header :: head">
</header>
<!-- サイドバー -->
<aside th:include="layout/aside :: side"></aside>
<!-- メイン -->
<article class="mypage">
<form action="" th:action="@{/mypage}" th:field="${userUpdate}"
method="post">
<h3 class="page_title">マイページ変更・確認画面</h3>
<table class="table employee">
<tr th:each="userInfo : ${userInfo}" th:object="${userInfo}">
<tr>
<th class="cell_title">社員番号</th>
<td th:text="*{userInfo.empId}"></td>
</tr>
<tr>
<th class="cell_title">名前</th>
<td><input type="text" name="empName"
th:value="${userInfo.empName}" /></td>
</tr>
<tr>
<th class="cell_title">パスワード</th>
<td><input type="password" name="password"
th:value="${userInfo.password}" /></td>
</tr>
<tr>
<th class="cell_title">生年月日</th>
<td><input type="text" name="birthday"
th:value="${#dates.format(userInfo.birthday,'yyyy-MM-dd')}" /></td>
</tr>
<tr>
<th class="cell_title">性別</th>
<td><input type="radio" name="gender"
th:value="${userInfo.gender}"
th:switch="*{userInfo.gender == 1}"
th:checked="${userInfo.gender == 1}" /> 男 <input type="radio"
name="gender" th:switch="*{userInfo.gender == 2}"
th:checked="${userInfo.gender == 2}"> 女</td>
</tr>
</table>
<div class="btn_area_center">
<input type="submit" value="変更確定" class="btn">
</div>
</form>
</article>
<!-- フッダー -->
<footer th:include="layout/footer :: foot"></footer>
</body>
</html>
IndexController.java
package jp.co.sss.sys.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import jp.co.sss.sys.entity.Employee;
import jp.co.sss.sys.form.LoginForm;
import jp.co.sss.sys.repository.EmployeeRepository;
@Controller
@SessionAttributes(types = Employee.class)
public class IndexController {
@Autowired
EmployeeRepository empRepository;
LoginForm loginform;
/**
* ログイン画面を表示する
* @param loginForm
* @return login.html
*/
@RequestMapping(path = "/login", method = RequestMethod.GET)
public String login( LoginForm loginForm,BindingResult br,Model model) {
return "login";
}
@Autowired
HttpSession session;
// 処理
/**
* 入力された値を元にログイン認証し、トップ画面に遷移する
*
* @param req
* @param res
* @param loginForm
* @return top.html
*/
@RequestMapping(path = "/top", method = RequestMethod.POST)
public String login(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
//ログインした人の情報
String empId = req.getParameter("empId");
String password = req.getParameter("password");
Employee employee = empRepository.findByEmpIdAndPassword(empId, password);
//セッションデータ設定
session.setAttribute("userInfo",employee);
//ログインユーザー情報
model.addAttribute("employee",employee);
//ログインチェック
if(employee == null) {
//存在しない場合
return "login";
}else {
//存在した場合
//社員情報一覧
List<Employee> empAll= empRepository.findAll();
model.addAttribute("empAll",empAll);
return "top";
}
}
@RequestMapping(path = "/top", method = RequestMethod.GET)
public String top(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
List<Employee> empAll= empRepository.findAll();
model.addAttribute("empAll",empAll);
return "top";
}
@RequestMapping(path = "/mypage", method = RequestMethod.POST)
public String empUser(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) throws ParseException {
session = req.getSession();
String empName = req.getParameter("empName");
String password = req.getParameter("password");
String date = req.getParameter("birthday");
String savegender = req.getParameter("gender");
SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
Date birthday = sdFormat.parse(date);
int gender = Integer.parseInt(savegender);
Employee userInfo = (Employee) session.getAttribute("userInfo");
userInfo.setEmpName(empName);
userInfo.setPassword(password);
userInfo.setBirthday(birthday);
userInfo.setGender(gender);
Employee updateEmployee = empRepository.save(userInfo);
return "edit_fin";
}
// TODO 自動生成されたメソッド・スタブ
//ログインユーザー情報
//マイページリンク押下,既存情報の出力
@RequestMapping(path = "/mypage", method = RequestMethod.GET)
public String empLink(@Validated LoginForm loginForm, HttpServletRequest req, HttpServletResponse res,BindingResult br,Model model,HttpSession session) {
session = req.getSession();
Object userInfo=session.getAttribute("userInfo");
model.addAttribute("userInfo",userInfo);
return "mypage";
}
}
EmployeeRepository.java
package jp.co.sss.sys.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import jp.co.sss.sys.entity.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, String> {
Employee findByEmpIdAndPassword(String empId, String password);
}
Employee.java
package jp.co.sss.sys.entity;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
@Table(name = "employee")
public class Employee {
/*
* 社員番号
*/
@Id
@Column(name ="emp_id" )
@NotEmpty(message = "社員番号は入力必須項目です")
@Size(max = 5, message = "社員番号は5文字以内で入力してください")
private String empId;
@Column (name="emp_name")
@NotEmpty(message = "パスワードは入力必須項目です")
@Size(max = 16, message = "パスワードは16文字以内で入力してください")
private String empName;
@Column (name="password")
private String password;
@Column (name="birthday")
private Date birthday;
@Column (name="gender")
private int gender;
@Column (name="delete_at")
private Boolean delete;
public void setEmpId(String empId) {
this.empId = empId;
}
public String getEmpId() {
return empId;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpName() {
return empName;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getBirthday() {
return birthday;
}
public void setGender(int gender) {
this.gender = gender;
}
public int getGender() {
return gender;
}
public void setDelete(Boolean delete) {
this.delete = delete;
}
public Boolean getDelete() {
return delete;
}
}
自分で試したこと
EmployeeRepository.java内においてクエリメソッドの確認
IndexController.java内に多いてsaveメソッドの確認
DB内テーブルに誤りがないか確認
0 likes