【发布时间】:2015-11-20 13:08:10
【问题描述】:
我想在每次应用程序运行时自动删除表并创建一个新表,并自动插入预定义的数据。我已经准备好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');
【问题讨论】:
-
该错误表明您的查询存在问题。尝试在运行时打印您的查询并分享。
-
请问,在运行时打印查询是什么意思?我已经提供了我的查询,我可以手动插入它。
-
如果您以字符串形式提供查询,则从男性和女性中删除单引号
-
还是一样。是的,我在一个名为
import.sql的文件中提供它。 -
重复H2 SQL Grammar Exception - 虽然是H2,但问题的原因是一样的