简体   繁体   中英

JPA generates incorrect SQL query for MySql8 with columdefinition

I am using Springboot JPAs to generate a MySql8 table. It works fine unless I try to use the columndefinition in the Column annotation.

 @Column(columnDefinition = "boolean default false")
    private Boolean example;

I tried replacing boolean with TINYINT but MySql8 should support boolean as far as I am aware.

I get an error during the table generation:

Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' boolean default false at line 5

To me it looks like the generated SQL Syntax has additional quotation marks but I am unsure how to fix the issue.

 create table `exampleTable` (
       `id` bigint not null auto_increment,
        `example` `boolean default false`,
        primary key (`id`)
    )

EDIT: I found my Issue in this question: Spring JPA globally_quoted_identifiers incorrectly quoting column type TEXT

if you want to set the default value of the example to be false then you can write it like this and remove columnDefintion from there

private Boolean example=false;

Disable quotes by adding spring.jpa.properties.hibernate.globally_quoted_identifiers=false to application.properties file

But after disabling, you will not be able to use column and table names that match mysql keywords (eg desc ). Workaround by adding an underscore:

@Column(name = "desc_")
String desc;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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