如何在MultipleLinesSqlCommandExtractor中转义单引号

huangapple 未分类评论48阅读模式
英文:

How to escape single quote with MultipleLinesSqlCommandExtractor

问题

使用Spring Boot项目时,我使用import.sql文件来初始化数据库。
今天,我没有任何问题,它正常工作。
这是原始文件的摘录:

insert into ref_building (code, label, description) values ("METAL", "Métal", "Permet d'extraire");
insert into ref_building (code, label, description) values ("PETROLE", "Pétrole", "Permet d'extraire");

为了提高SQL文件的可读性,我决定在YAML属性中使用MultipleLinesSqlCommandExtractor,如下所示:

spring:
   jpa :
     properties:
      hibernate:
        connection:
          charSet: UTF-8
        hbm2ddl:
          import_files_sql_extractor: org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor

在我添加了这个之后,Spring Boot出现了错误。

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "insert into ref_building [...]

经过分析,问题是单引号("Permet d'extraire")。

所以我以这种方式转义了单引号:

insert into ref_building (code, label, description) values ("METAL", "Métal", "Permet d''extraire");
insert into ref_building (code, label, description) values ("PETROLE", "Pétrole", "Permet d''extraire");

我在启动时没有错误,但在我的数据库中,我发现一个值:Permet d''extraire(带有两个单引号)。

我不明白如何正确转义这个问题(以及为什么在使用MultipleLinesSqlCommandExtractor时出现问题)。

英文:

With a Spring Boot project, I use an import.sql file to initialize a database.
Today, I don't have any problem, and it works properly.
This is an extract of the original file :

insert into ref_building (code, label, description) values ("METAL", "Métal", "Permet d'extraire");
insert into ref_building (code, label, description) values ("PETROLE", "Pétrole", "Permet d'extraire");

To improve the readability of the SQL file, I decide to use MultipleLinesSqlCommandExtractor in the YAML properties, like this :

spring:
   jpa :
     properties:
      hibernate:
        connection:
          charSet: UTF-8
        hbm2ddl:
          import_files_sql_extractor: org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor

After I add this, I have an error with Spring Boot.

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "insert into ref_building [...]

After analysis, the problem is the single quote ("Permet d'extraire")

So I escape the single quote in this way:

insert into ref_building (code, label, description) values ("METAL", "Métal", "Permet d''extraire");
insert into ref_building (code, label, description) values ("PETROLE", "Pétrole", "Permet d''extraire");

I don't have error at launch, but in my database I find a value : Permet d''extraire (with two single quote)

I don't understand how to escape this correctly (and why it doesn't work specifically with MultipleLinesSqlCommandExtractor).

huangapple
  • 本文由 发表于 2020年5月4日 03:50:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61580730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定