前提・実現したいこと
SpringとPostgreを連携させようとしたいのですが上手くいきません。
Postgreを入れてServiceやentityを作っている最中にビルドができなくなったので、連携時の何かが原因だと思いますが。。
発生している問題・エラーメッセージ
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2020-08-27 22:35:49.753 ERROR 5475 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'systemUserRepository' defined in com.example.demo.SystemUserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.example.demo.SystemUser Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: com.example.demo.SystemUser
該当のソースコード
HelloController
package com.example.demo; import java.util.List; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloController { @Autowired private SystemUserService userService; @RequestMapping(value = "/") private String index() { return "index"; } @RequestMapping(value = "/form/edit") private String edit() { return "/form/edit"; } @RequestMapping("/form/editCheck") public String editCheck(@ModelAttribute("form") Form form) { return "/form/editCheck"; } @RequestMapping("/form/finish") public String finish(HttpSession session) { SystemUser sessionEditForm = (SystemUser) session.getAttribute("form"); this.userService.save(sessionEditForm); return "/form/finish"; } @RequestMapping(value = "/test", method = RequestMethod.GET) public List<SystemUser> test() { return userService.findAll(); } @ModelAttribute Form setupForm() { return new Form(); } }
SystemUser
package com.example.demo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Table; import org.springframework.data.annotation.Id; @Entity @Table(name = "systemuser") public class SystemUser { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column private int id; @Column private String name; @Column private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
SystemUserRepository
package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface SystemUserRepository extends JpaRepository<SystemUser, Integer> { }
SystemUserService
package com.example.demo; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class SystemUserService { @Autowired SystemUserRepository userRepositry; public SystemUser save(SystemUser entity) { return this.userRepositry.save(entity); } public List<SystemUser> findAll() { return userRepositry.findAll(); } }
apprication
spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url=jdbc:postgresql://localhost:5432/mydb spring.datasource.username=username spring.datasource.password=password
pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- データベースアクセス --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
試したこと
テーブル作成時のSQLはこちらです。
CREATE TABLE systemuser (
id SERIAL NOT NULL,
age INT,
name VARCHAR(20)
);
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
まだ回答がついていません
会員登録して回答してみよう