JPQLでクエリの定義をして名前を昇順で取得する
現在SpringBootを使い、テーブルに登録した名前を昇順に出力したいのですが、組み込みデータベースを使用し、エンティティクラスを作成したのにテーブルが見つからないとエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/Users/arinko/Downloads/hajiboot-jpa/target/classes/data.sql]: INSERT INTO customers(first_name, last_name) VALUES('Nobita', 'Nobi'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: テーブル "CUSTOMERS" が見つかりません Table "CUSTOMERS" not found; SQL statement: INSERT INTO customers(first_name, last_name) VALUES('Nobita', 'Nobi') [42102-200] at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:282) ~[spring-jdbc-5.3.9.jar:5.3.9] at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254) ~[spring-jdbc-5.3.9.jar:5.3.9] at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49) ~[spring-jdbc-5.3.9.jar:5.3.9] at org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer.runScripts(DataSourceScriptDatabaseInitializer.java:89) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.runScripts(AbstractScriptDatabaseInitializer.java:145) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applyScripts(AbstractScriptDatabaseInitializer.java:107) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.applyDataScripts(AbstractScriptDatabaseInitializer.java:101) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.initializeDatabase(AbstractScriptDatabaseInitializer.java:76) ~[spring-boot-2.5.4.jar:2.5.4] at org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer.afterPropertiesSet(AbstractScriptDatabaseInitializer.java:65) ~[spring-boot-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] ... 17 common frames omitted Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: テーブル "CUSTOMERS" が見つかりません
Customer.java
Java
1package com.example.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}
CustomerRepository.java
Java
1package com.example.repository; 2 3import com.example.domain.Customer; 4import org.springframework.data.jpa.repository.JpaRepository; 5import org.springframework.data.jpa.repository.Query; 6 7import java.util.List; 8 9public interface CustomerRepository extends JpaRepository<Customer, Integer> { 10 @Query("SELECT x FROM Customer x ORDER BY x.firstName, x.lastName") 11 List<Customer> findAllOrderByName(); 12} 13
###HajibootJpaApplication.java
Java
1package com.example; 2 3import com.example.domain.Customer; 4import com.example.repository.CustomerRepository; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.boot.CommandLineRunner; 7import org.springframework.boot.SpringApplication; 8import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 9import org.springframework.context.annotation.ComponentScan; 10 11@EnableAutoConfiguration 12@ComponentScan 13public class HajibootJpaApplication implements CommandLineRunner { 14 @Autowired 15 CustomerRepository customerRepository; 16 17 @Override 18 public void run(String... strings) throws Exception { 19 // データ追加 20 Customer created = customerRepository.save(new Customer(null, "Hidetosi", "Dekisugi")); 21 System.out.println(created + " is created!"); 22 // データ表示 23 customerRepository.findAllOrderByName() 24 .forEach(System.out::println); 25 } 26 27 public static void main(String[] args) { 28 SpringApplication.run(HajibootJpaApplication.class, args); 29 } 30 } 31
application.properties
spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy spring.datasource.url=jdbc:log4jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE logging.level.jdbc=OFF logging.level.jdbc.sqltiming=DEBUG
pom.xmll
<?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.5.4</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>hajiboot-jpa</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hajiboot-jpa</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.lazyluke</groupId> <artifactId>log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
###data.sql
INSERT INTO customers(first_name, last_name) VALUES('Nobita', 'Nobi'); INSERT INTO customers(first_name, last_name) VALUES('Takeshi', 'Goda'); INSERT INTO customers(first_name, last_name) VALUES('Suneo', 'Honekawa'); INSERT INTO customers(first_name, last_name) VALUES('Shizuka', 'Minamoto');
補足情報(FW/ツールのバージョンなど)
SpringToolSuite4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/13 13:59 編集
2021/10/13 14:12
2021/10/14 01:51