简体   繁体   English

JPA 为带有列定义的 MySql8 生成不正确的 SQL 查询

[英]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.

相关问题 JPA子查询在SQL查询中生成ID两次 - JPA subquery generates ID twices in SQL query Hibernate 使用 MySQL 生成无效的 SQL 查询 - Hibernate generates invalid SQL query with MySQL Spring Boot JPA虽然带有MYSQL8的Hibernate没有批量执行 - Spring Boot JPA though Hibernate with MYSQL8 is not executing as batches 谁能说出为什么此JPA条件查询在执行时(休眠)会生成无效的SQL语句,以及如何解决它? - Can anyone tell why this JPA criteria query generates an invalid SQL statement when executed (Hibernate) and how to fix it? JPA生成的SQL语句不正确 - Incorrect SQL statements generated by JPA JPA 2.0(Hibernate)使用@JoinTable为@OneToMany生成了不正确的联接表PK - JPA 2.0 (Hibernate) generates incorrect join table PK for @OneToMany with @JoinTable JPA实体类和SQL查询可从MySQL检索数据 - JPA entity class and SQL query to retrieve Data from MySQL Hibernate生成模棱两可的SQL查询 - Hibernate generates ambiguous SQL query 带有MS SQL Server 2012/2014的JPA(休眠)生成错误的查询; 没有参数标记 - JPA (Hibernate) with MS SQL server 2012/2014 generating incorrect query; no parameter markers 为什么 MySQL 和 JPA 会发生“java.sql.SQLException: Incorrect string value...” - Why "java.sql.SQLException: Incorrect string value..." is happened with MySQL and JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM