简体   繁体   English

使用Spring Boot运行脚本时出现SQL错误

[英]SQL error while running script with spring boot

I'm trying to populate my database as I start my project: 启动项目时,我试图填充数据库:

  1. Spring-Boot 春季启动

  2. H2 embedded database H2嵌入式数据库

this is the script: 这是脚本:

INSERT INTO 'VET' VALUES (1, 'AAAA', 'BBBB');

here is my entity in Java: 这是我在Java中的实体:

@Entity
public class Vet extends BaseClass{

//  @Id
//  @GeneratedValue
//  private int id;
    @NotNull(message="{NotNull}")
    @Size(min=2,max=15,message="{Size}")
    @Column(name = "first_name")
    private String firstName;

    @NotNull(message="{NotNull}")
    @Size(min=2,max=15,message="{Size}")
    @Column(name = "last_name")
    private String lastName;

    ...
}

@MappedSuperclass
public class BaseClass {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

}

and as I start Spring, this is the StackTrace: 在我启动Spring时,这是StackTrace:

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO 'VET'[*] VALUES (1, 'AAAA', 'BBBB')"; expected "identifier"; SQL statement:
INSERT INTO 'VET' VALUES (1, 'AAAA', 'BBBB') [42001-192]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.message.DbException.getSyntaxError(DbException.java:205) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.command.Parser.readIdentifierWithSchema(Parser.java:3130) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.command.Parser.readTableOrView(Parser.java:5365) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.command.Parser.parseInsert(Parser.java:1053) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.command.Parser.parsePrepared(Parser.java:413) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.command.Parser.parse(Parser.java:317) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.command.Parser.parse(Parser.java:289) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.command.Parser.prepareCommand(Parser.java:254) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.engine.Session.prepareLocal(Session.java:560) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.engine.Session.prepareCommand(Session.java:501) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1202) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:170) ~[h2-1.4.192.jar:1.4.192]
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:158) ~[h2-1.4.192.jar:1.4.192]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:473) ~[spring-jdbc-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    ... 67 common frames omitted

Of course the name of the script is data.sql and its located in resources folder, it's obviously reading the script accordingly to the stack trace. 当然,脚本的名称是data.sql ,它位于resources文件夹中,显然是根据堆栈跟踪读取脚本。 I don't understand the part with identifier 我不了解带有标识符的部分

////EDIT ////编辑

ok so I tried something like this: 好的,所以我尝试了这样的事情:

INSERT INTO VET('id', 'first_name', 'last_name') VALUES (1, 'AAAA', 'BBBB');

and that's the error 那就是错误

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO VET('id'[*], 'first_name', 'last_name') VALUES (1, 'AAAA', 'BBBB')"; expected "identifier"; SQL statement:
INSERT INTO VET('id', 'first_name', 'last_name') VALUES (1, 'AAAA', 'BBBB') [42001-192]

but when I go with: 但是当我选择:

INSERT INTO VET VALUES (1, 'AAAA', 'BBBB');

build is successful 建立成功

so for educational purposes only why does the first option give errors? 因此,仅出于教育目的,为什么第一个选项会出错?

列名或表名不应带有引号。

INSERT INTO VET(id, first_name, last_name) VALUES (1, 'AAAA', 'BBBB')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM