Spring Mvc Hibernate编码/多行导入sql
问题内容:
我正在开发Spring MVC,
在项目启动时,我已设置database
为sql
使用hibernate配置导入默认值hibernate.hbm2ddl.import_files
。内部数据使用import.sql
编码UTF-8
。
控制台输出
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000388:
Unsuccessful: INSERT INTO menu (id, DATE_CREATED, DATE_DELETED,
DATE_UPDATED, TITLE_ENG, TITLE_GEO, TITLE_RUS, ENABLED, PARENT_ID,
URL, SITE_ID, USER_ID) VALUES
ERROR:
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
ERROR:
org.hibernate.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: (1,
'2015-09-10 12:00:00', NULL, NULL, 'About Us', N'ჩვენს
შეს�ხებ', 'About Us', b'1', NULL, '/article/view/1', 3,
1).
问题答案:
- 确保将JDBC驱动程序设置为使用UTF-8:
jdbc:mysql://dbname?useUnicode=true&characterEncoding=utf-8
- 在中
persistence.xml
,设置hibernate连接字符集:
<property name="hibernate.connection.charSet" value="UTF-8"/>
如果resources/import.sql
文件是多行,请添加:
<property
name="hibernate.hbm2ddl.import_files_sql_extractor"
value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor"
/>
或对于Spring Boot,在application.properties
:
spring.jpa.properties.hibernate.connection.charSet=UTF-8
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor