[英]JPA generates incorrect SQL query for MySql8 with columdefinition
I am using Springboot JPAs to generate a MySql8 table.我正在使用 Springboot JPA 生成 MySql8 表。 It works fine unless I try to use the columndefinition in the Column annotation.
它工作正常,除非我尝试在 Column 注释中使用 columndefinition。
@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.我尝试用 TINYINT 替换 boolean,但据我所知,MySql8 应该支持 boolean。
I get an error during the table generation:我在表生成期间收到错误:
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax;
Caused by: java.sql.SQLSyntaxErrorException: 你的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '
boolean default false
at line 5检查与您的 MySQL 服务器版本对应的手册,了解在第 5 行的 '
boolean default false
附近使用的正确语法
To me it looks like the generated SQL Syntax has additional quotation marks but I am unsure how to fix the issue.在我看来,生成的 SQL 语法有额外的引号,但我不确定如何解决该问题。
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编辑:我在这个问题中发现了我的问题: Spring JPA global_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如果你想将示例的默认值设置为 false 那么你可以这样写并从那里删除 columnDefintion
private Boolean example=false;私人 Boolean 示例=假;
Disable quotes by adding spring.jpa.properties.hibernate.globally_quoted_identifiers=false
to application.properties
file通过将
spring.jpa.properties.hibernate.globally_quoted_identifiers=false
添加到application.properties
文件来禁用引号
But after disabling, you will not be able to use column and table names that match mysql keywords (eg desc
).但禁用后,您将无法使用匹配 mysql 关键字的列名和表名(例如
desc
)。 Workaround by adding an underscore:通过添加下划线解决方法:
@Column(name = "desc_")
String desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.