Spring Mvc Hibernate Encoding/Multi-line import sql
Asked Answered
C

2

5

I am working on Spring MVC,

On project startup i have set database to import default sql with hibernate configuration hibernate.hbm2ddl.import_files. Data inside import.sql is encoded with UTF-8.

Console Output

 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).
Corporator answered 5/10, 2015 at 16:10 Comment(1)
I am not familiar with import_files, but from the output my guess is that sql statements in the file span two lines but import_files expect each sql statement to span only one line causing "insert ... values" and "the value list" to be treated as two separate statements. Check out this thread #674302.Louvre
D
23
  1. Make sure you are setting the JDBC driver to use UTF-8:

    jdbc:mysql://dbname?useUnicode=true&characterEncoding=utf-8

  2. In persistence.xml, set the hibernate connection charset:

    <property name="hibernate.connection.charSet" value="UTF-8"/>

If your resources/import.sql file is multi-line, add:

<property 
  name="hibernate.hbm2ddl.import_files_sql_extractor"
  value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor" 
/>

or for Spring Boot, in application.properties:

spring.jpa.properties.hibernate.connection.charSet=UTF-8
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
Dufy answered 5/10, 2015 at 18:42 Comment(1)
Best answer find out that the problem was really with multiple lines and with encoding also, nice work dude!Corporator
L
0

For Spring Boot, if you use Hibernate 6, extractor changed its name and place. New way is:

spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.schema.internal.script.MultiLineSqlScriptExtractor
Ludwigg answered 29/4, 2023 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.