0

I'm trying to populate my database as I start my project:

  1. Spring-Boot

  2. H2 embedded database

this is the script:

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

here is my entity in 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:

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. 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?

4
  • 3
    'VET' is a string. it is NOT a field identifier. That'd be JUST VET, without the quotes. Commented Oct 19, 2016 at 14:19
  • 1
    You have used single quotes around VET instead of reverse quotes Commented Oct 19, 2016 at 14:19
  • 2
    INSERT INTO VET(id, firstName, lastName) VALUES (1, 'AAAA', 'BBBB'); Commented Oct 19, 2016 at 14:20
  • 1
    Identifiers either need double quotes " or no quotes at all (which is highly recommended) Commented Oct 19, 2016 at 14:32

1 Answer 1

3

You shouldn't have quotes around your column names or table names.

INSERT INTO VET(id, first_name, last_name) VALUES (1, 'AAAA', 'BBBB')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.