我尝试使用Spring构建一个H2数据库,但是遇到了错误。到目前为止,我已经按照this通道的步骤进行了操作,但是遇到了这个错误:
2022-12-30T22:04:01.054+11:00 WARN 18528 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "INSERT INTO courses VALUES (id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "INSERT INTO courses VALUES (id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
(the错误继续...)
型号
package com.example.SpringPractice.model;
// imports
@Entity
@Table(name = "courses")
public class Course {
@Id
@GeneratedValue
private Long id;
private String code;
private String coursename;
}
储存库
package com.example.SpringPractice.repository;
import com.example.SpringPractice.model.Course;
import org.springframework.data.repository.CrudRepository;
public interface CourseRepository extends CrudRepository<Course, Long> {
}
依赖性
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
主要
package com.example.SpringPractice;
// imports
@SpringBootApplication
public class SpringPracticeApplication {
@Autowired
private CourseRepository courseRepository;
public static void main(String[] args) {
SpringApplication.run(SpringPracticeApplication.class, args);
}
@PostConstruct
private void postInit() {
System.out.println("All courses: " + courseRepository.findAll());
}
}
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url = jdbc:h2:mem:course-db
SQL语句
INSERT INTO courses VALUES (id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')
我试过改变application.properties
中的依赖关系(javax到jakarta)。
1条答案
按热度按时间lx0bsm1f1#
SQL语句中的错误。
我错过了错误日志中的“原因”,这有助于回答问题。
修复了SQL语句: