Java Spring H2数据库:通过JDBC语句执行DDL时出错

k5hmc34c  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(261)

我尝试使用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)。

lx0bsm1f

lx0bsm1f1#

SQL语句中的错误。
我错过了错误日志中的“原因”,这有助于回答问题。

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "INSERT INTO courses VALUES (id, code, coursename) [*]VALUES (1, CS150, Intro to Computer Science)"; SQL statement:

修复了SQL语句:

INSERT INTO courses(id, code, coursename) VALUES (1, 'CS150', 'Intro to Computer Science')

相关问题