在Hibernate中无法成功导入import.sql

Dis*_*Hay 6 java spring hibernate jpa

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

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
Run Code Online (Sandbox Code Playgroud)

这是我的实体:

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

    @Column(name = "gender_name")
    private String name; 
}
Run Code Online (Sandbox Code Playgroud)

这是我的查询import.sql

INSERT INTO gender 
    (gender_id, gender_name) 
VALUES 
    (1, 'Male'), 
    (2, 'Female');
Run Code Online (Sandbox Code Playgroud)

JFP*_*ard 5

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

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

INSERT INTO gender (gender_id, gender_name) VALUES (1, 'Male'), (2, 'Female');
Run Code Online (Sandbox Code Playgroud)

通过注意关键字之间仅留空格,并删除所有不可打印的字符。您可以使用自己喜欢的文本编辑器,并使用选项“显示所有字符”。

  • 确实如此。我需要将所有内容合而为一。正如@Tobias Liefke提到的链接一样,Hibernate默认使用`SingleLineSqlCommandExtractor`,它将每行视为单独的查询。为了能够读取`import.sql`中的多行查询,将其更改为`MultipleLinesSqlCommandExtractor`。如果您使用的是Spring Boot,则将spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor = org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor添加到application.properties中。 (8认同)