前提・実現したいこと
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
1package com.example.demo; 2 3import java.util.List; 4 5import javax.servlet.http.HttpSession; 6 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.stereotype.Controller; 9import org.springframework.web.bind.annotation.ModelAttribute; 10import org.springframework.web.bind.annotation.RequestMapping; 11import org.springframework.web.bind.annotation.RequestMethod; 12 13@Controller 14public class HelloController { 15 16 @Autowired 17 private SystemUserService userService; 18 19 @RequestMapping(value = "/") 20 private String index() { 21 return "index"; 22 } 23 24 @RequestMapping(value = "/form/edit") 25 private String edit() { 26 return "/form/edit"; 27 } 28 29 @RequestMapping("/form/editCheck") 30 public String editCheck(@ModelAttribute("form") Form form) { 31 32 return "/form/editCheck"; 33 } 34 35 @RequestMapping("/form/finish") 36 public String finish(HttpSession session) { 37 SystemUser sessionEditForm = (SystemUser) session.getAttribute("form"); 38 39 this.userService.save(sessionEditForm); 40 41 return "/form/finish"; 42 } 43 44 @RequestMapping(value = "/test", method = RequestMethod.GET) 45 public List<SystemUser> test() { 46 return userService.findAll(); 47 } 48 49 @ModelAttribute 50 Form setupForm() { 51 return new Form(); 52 } 53 54} 55
SystemUser
1package com.example.demo; 2 3import javax.persistence.Column; 4import javax.persistence.Entity; 5import javax.persistence.GeneratedValue; 6import javax.persistence.GenerationType; 7import javax.persistence.Table; 8 9import org.springframework.data.annotation.Id; 10 11 12@Entity 13@Table(name = "systemuser") 14public class SystemUser { 15 16 @Id 17 @GeneratedValue(strategy=GenerationType.IDENTITY) 18 @Column 19 private int id; 20 21 @Column 22 private String name; 23 24 @Column 25 private int age; 26 27 public int getId() { 28 return id; 29 } 30 public void setId(int id) { 31 this.id = id; 32 } 33 public String getName() { 34 return name; 35 } 36 public void setName(String name) { 37 this.name = name; 38 } 39 public int getAge() { 40 return age; 41 } 42 public void setAge(int age) { 43 this.age = age; 44 } 45 46} 47
SystemUserRepository
1package com.example.demo; 2 3import org.springframework.data.jpa.repository.JpaRepository; 4import org.springframework.stereotype.Repository; 5 6@Repository 7public interface SystemUserRepository extends JpaRepository<SystemUser, Integer> { 8 9 10 11} 12
SystemUserService
1package com.example.demo; 2 3import java.util.List; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Service; 7import org.springframework.transaction.annotation.Transactional; 8 9@Service 10@Transactional 11public class SystemUserService { 12 13 @Autowired 14 SystemUserRepository userRepositry; 15 16 public SystemUser save(SystemUser entity) { 17 18 return this.userRepositry.save(entity); 19 } 20 21 public List<SystemUser> findAll() { 22 return userRepositry.findAll(); 23 } 24 25} 26
apprication
1spring.datasource.driver-class-name=org.postgresql.Driver 2spring.datasource.url=jdbc:postgresql://localhost:5432/mydb 3spring.datasource.username=username 4spring.datasource.password=password
pom
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>2.3.3.RELEASE</version> 10 <relativePath /> <!-- lookup parent from repository --> 11 </parent> 12 <groupId>com.example</groupId> 13 <artifactId>demo</artifactId> 14 <version>0.0.1-SNAPSHOT</version> 15 <name>demo</name> 16 <description>Demo project for Spring Boot</description> 17 18 <properties> 19 <java.version>1.8</java.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-test</artifactId> 31 <scope>test</scope> 32 <exclusions> 33 <exclusion> 34 <groupId>org.junit.vintage</groupId> 35 <artifactId>junit-vintage-engine</artifactId> 36 </exclusion> 37 </exclusions> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-thymeleaf</artifactId> 42 </dependency> 43 <!-- データベースアクセス --> 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-data-jpa</artifactId> 47 </dependency> 48 <dependency> 49 <groupId>org.postgresql</groupId> 50 <artifactId>postgresql</artifactId> 51 <scope>runtime</scope> 52 </dependency> 53 </dependencies> 54 55 <build> 56 <plugins> 57 <plugin> 58 <groupId>org.springframework.boot</groupId> 59 <artifactId>spring-boot-maven-plugin</artifactId> 60 </plugin> 61 </plugins> 62 </build> 63 64 65</project> 66
試したこと
テーブル作成時のSQLはこちらです。
CREATE TABLE systemuser (
id SERIAL NOT NULL,
age INT,
name VARCHAR(20)
);
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/27 20:50