Java 在hibernate中导入import.sql失败

Java 在hibernate中导入import.sql失败,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我想在每次应用程序运行时自动删除该表并创建一个新表,同时自动插入预定义数据。我已经在import.sql中准备了数据。我已经在application.properties中设置了spring.jpa.hibernate.ddl auto=create drop。但是,为什么会出现以下错误?我可以手动插入它 2015-11-20 20:53:57.242 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport

我想在每次应用程序运行时自动删除该表并创建一个新表,同时自动插入预定义数据。我已经在
import.sql
中准备了数据。我已经在
application.properties
中设置了
spring.jpa.hibernate.ddl auto=create drop
。但是,为什么会出现以下错误?我可以手动插入它

2015-11-20 20:53:57.242 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000388: Unsuccessful: INSERT INTO gender
2015-11-20 20:53:57.242 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : 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 '' at line 1
2015-11-20 20:53:57.242 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000388: Unsuccessful: (gender_id, gender_name)
2015-11-20 20:53:57.257 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : 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 'gender_id, gender_name)' at line 1
2015-11-20 20:53:57.257 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000388: Unsuccessful: VALUES
2015-11-20 20:53:57.257 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : 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 'VALUES' at line 1
2015-11-20 20:53:57.257 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000388: Unsuccessful: (1, 'Male'),
2015-11-20 20:53:57.257 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : 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 '1, 'Male'),' at line 1
2015-11-20 20:53:57.257 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000388: Unsuccessful: (2, 'Female')
2015-11-20 20:53:57.257 ERROR 7092 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport  : 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 '2, 'Female')' at line 1
这是我的实体:

@Entity
public class Gender {
    @Id
    @Column(name = "gender_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "gender_name")
    private String name; 
}
这是我在
import.sql中的查询:

INSERT INTO gender 
    (gender_id, gender_name) 
VALUES 
    (1, 'Male'), 
    (2, 'Female');

根据错误模式,您的行尾似乎包含无法处理的字符(隐藏字符,如LF或类似的字符)

我这么说是因为你所有的错误都与这一行的结尾有关。尝试将import.sql放在一行中,如下所示:

INSERT INTO gender (gender_id, gender_name) VALUES (1, 'Male'), (2, 'Female');

注意关键字之间只有空格,并删除所有无法打印的字符。您可以使用最喜爱的文本编辑器并使用“显示所有字符”选项。

错误显示您的查询中存在一些问题。请尝试在运行时打印查询并共享。请问,在运行时打印查询是什么意思?我已经提供了我的查询,我可以手动插入它。如果您以字符串形式提供查询,那么请删除男性和女性的单引号,它仍然是一样的。是的,我在一个名为
import.sql
.Duplicate of-all的文件中提供了它。尽管它是关于H2的,但问题的原因是相同的。我需要把一切都安排在一行中。作为@ Tobias Liefke所提到的链接,Hibernate默认使用<>代码> SunLeleNeSqLMuleDractRux//> >,将每个行视为单独的查询。要使在
import.sql
中读取多行查询成为可能,请将其更改为
MultipleLinesSqlCommandExtractor
。如果您使用的是
spring Boot
,请将
spring.jpa.properties.hibernate.hbm2ddl.import\u files\u sql\u extractor=org.hibernate.tool.hbm2ddl.multiplelinessqlcommandtractor
添加到
application.properties