前提・実現したいこと
JavaでSpringbootを使って、postrgreSQLのID管理のシステムを作っています。テーブルに列を追加して、id,first_name,last_nameのカラムに情報いれるという機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hajibootJpaApplication': Unsatisfied dependency expressed through field 'customerRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository' defined in com.example.hajibootjpa.repository.CustomerRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.example.hajibootjpa.repository.CustomerRepository.findAllorderByName(org.springframework.data.domain.Pageable)! No property name found for type Customer! Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository' defined in com.example.hajibootjpa.repository.CustomerRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.example.hajibootjpa.repository.CustomerRepository.findAllorderByName(org.springframework.data.domain.Pageable)! No property name found for type Customer! Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.example.hajibootjpa.repository.CustomerRepository.findAllorderByName(org.springframework.data.domain.Pageable)! No property name found for type Customer! Caused by: org.springframework.data.mapping.PropertyReferenceException: No property name found for type Customer!
該当のソースコード
HajibootJpaApplicationjavaclass
1package com.example.hajibootjpa; 2 3import com.example.hajibootjpa.domain.Customer; 4import com.example.hajibootjpa.repository.CustomerRepository; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.boot.CommandLineRunner; 7import org.springframework.boot.SpringApplication; 8import org.springframework.boot.autoconfigure.SpringBootApplication; 9import org.springframework.data.domain.Page; 10import org.springframework.data.domain.PageRequest; 11import org.springframework.data.domain.Pageable; 12import org.springframework.data.domain.Sort; 13 14@SpringBootApplication 15public class HajibootJpaApplication implements CommandLineRunner { 16 17 @Autowired 18 CustomerRepository customerRepository; 19 20 @Override 21 public void run(String... strings) throws Exception { 22 Customer created = customerRepository.save(new Customer(null, "Hidetoshi", "Dekisugi")); 23 System.out.println(created + " is created!"); 24 25 26 customerRepository.findAllOrderByName() 27 .forEach(System.out::println); 28} 29 30 31 public static void main(String[] args) { 32 SpringApplication.run(HajibootJpaApplication.class, args); 33 34 } 35} 36 37
Customerjavaclass
1package com.example.hajibootjpa.domain; 2 3import lombok.AllArgsConstructor; 4import lombok.Data; 5import lombok.NoArgsConstructor; 6 7import javax.persistence.*; 8 9@Entity 10@Table(name = "customers") 11@Data 12@NoArgsConstructor 13@AllArgsConstructor 14public class Customer { 15 @Id 16 @GeneratedValue 17 private Integer id; 18 @Column(nullable = false) 19 private String firstName; 20 @Column(nullable = false) 21 private String lastName; 22} 23 24
CustomerRepositoryjavainterface
1 2package com.example.hajibootjpa; 3 4import com.example.hajibootjpa.domain.Customer; 5import com.example.hajibootjpa.repository.CustomerRepository; 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.boot.CommandLineRunner; 8import org.springframework.boot.SpringApplication; 9import org.springframework.boot.autoconfigure.SpringBootApplication; 10import org.springframework.data.domain.Page; 11import org.springframework.data.domain.PageRequest; 12import org.springframework.data.domain.Pageable; 13import org.springframework.data.domain.Sort; 14 15@SpringBootApplication 16public class HajibootJpaApplication implements CommandLineRunner { 17 18 @Autowired 19 CustomerRepository customerRepository; 20 21 @Override 22 public void run(String... strings) throws Exception { 23 Customer created = customerRepository.save(new Customer(null, "Hidetoshi", "Dekisugi")); 24 System.out.println(created + " is created!"); 25 26 customerRepository.findAllOrderByName() 27 .forEach(System.out::println); 28} 29 30 31package com.example.hajibootjpa.repository; 32 33import com.example.hajibootjpa.domain.Customer; 34import org.springframework.data.domain.Page; 35import org.springframework.data.domain.Pageable; 36import org.springframework.data.jpa.repository.Query; 37import org.springframework.data.jpa.repository.JpaRepository; 38 39public interface CustomerRepository extends JpaRepository <Customer, 40 Integer> { 41 @Query("SELECT X FROM Customer X ORDER BY X.firstName, X.lastName") 42 Page<Customer> findAllOrderByName(Pageable pageable); 43} 44
datasql
1INSERT INTO customers(first_name, last_name) VALUES('Nobita','Nobi'); 2INSERT INTO customers(first_name, last_name) VALUES('Takeshi','Gota'); 3INSERT INTO customers(first_name, last_name) VALUES('Suneo','Namekawa'); 4INSERT INTO customers(first_name, last_name) VALUES('Shizuka','Minamoto'); 5
pomxml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>hajiboot-jpa</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>hajiboot-jpa</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.4.1</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 22 <properties> 23 <projext.build.sourceEncoding>UTF-8</projext.build.sourceEncoding> 24 <projext.reporting.outputEncoding>UTF-8</projext.reporting.outputEncoding> 25 <java.version>1.8</java.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-data-jpa</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>com.h2database</groupId> 36 <artifactId>h2</artifactId> 37 <scope>runtime</scope> 38 </dependency> 39 <dependency> 40 <groupId>org.projectlombok</groupId> 41 <artifactId>lombok</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>org.springframework.boot</groupId> 45 <artifactId>spring-boot-starter-test</artifactId> 46 <scope>test</scope> 47 </dependency> 48 </dependencies> 49 50 <build> 51 <plugins> 52 <plugin> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-maven-plugin</artifactId> 55 <configuration> 56 <excludes> 57 <exclude> 58 <groupId>org.projectlombok</groupId> 59 <artifactId>lombok</artifactId> 60 </exclude> 61 </excludes> 62 </configuration> 63 </plugin> 64 </plugins> 65 </build> 66 67</project> 68
試したこと
H2というデーターベースで「drop」が実行される。エラーについて下記参照。
2021-01-25 10:47:27.866 INFO 3380 --- [ Thread-2] jdbc.sqlonly : drop table customers if exists
2021-01-25 10:47:27.869 INFO 3380 --- [ Thread-2] jdbc.sqltiming : drop table customers if exists {executed in 3 msec}
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。