Home > OS >  Exception when creating H2 database with generated SQL query in Spring project
Exception when creating H2 database with generated SQL query in Spring project

Time:11-22

I use spring and H2 database in my project and I create simple entity, but when i try to start my application i get this: Syntax error in SQL statement "CREATE TABLE T_TRANSACTION (ID BIGINT GENERATED BY DEFAULT AS IDENTITY, AMOUNT INTEGER NOT NULL, FROM[*] VARCHAR(255), TO VARCHAR(255), PRIMARY KEY (ID))"; expected "identifier"; SQL statement: create table t_transaction (id bigint generated by default as identity, amount integer not null, from varchar(255), to varchar(255), primary key (id))

My entity:

@Entity
@Table(name = "t_transaction")
class Transaction{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private val id: Long? = null

    var from: String? = null

    var to: String? = null

    var amount = 0
}

Application properties:

spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:Db;DB_CLOSE_DELAY=-1

Also I use the same id generation method in other entity, but i dont get same error.

CodePudding user response:

from is a reserved SQL keyword. Try changing it to something else.

If you see the query generated, it's treating FROM as differently like FROM[*] VARCHAR(255) which is the hint.

  • Related