I have my application.properties
set up like this :
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.url =
Non of the answer worked for me … except the url encoding part.
The solution in my case is twofold:
1- Add encoding in the URL of Database config bean:
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/customersdb?useSSL=false&useUnicode=yes&characterEncoding=UTF-8&characterSetResults=UTF-8" />
2- Add this configuration to your dispatcher config file:
<filter>
<filter-name>encoding-filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Otherwise, other configurations don't take effect.
See "question marks" in http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored .
Also,
⚈ spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
⚈ spring.jpa.properties.hibernate.connection.CharSet=utf-8
⚈ spring.jpa.properties.hibernate.connection.useUnicode=true
Keep your hibernate configuration Like this
jdbc:mysql://localhost:3306/dbname?useUnicode=yes&characterEncoding=UTF-8
And Change your DB Collation Like this
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
More information : Link
In your /etc/mysql/my.cnf
file change the following.
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
if you want save non-ascii character correctly, you should make sure:
following is how to achieve it:
after refer another Chinese post, my final working configuration:
src/main/java/com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java
package com.crifan.smartelectricserver;
import org.hibernate.dialect.MySQL55Dialect;;
// import org.hibernate.dialect.MySQL5InnoDBDialect; // Deprecated
// public class MySQL5InnoDBDialectUtf8mb4 extends MySQL5InnoDBDialect {
public class MySQL55DialectUtf8mb4 extends MySQL55Dialect {
@Override
public String getTableTypeString() {
return "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci";
}
}
which set table charset to utf8mb4
and collation to utf8mb4_unicode_ci
and
src/main/resources/application.properties
spring.jpa.database-platform=com.crifan.smartelectricserver.MySQL55DialectUtf8mb4
in which:
com.crifan.smartelectricserver.MySQL55DialectUtf8mb4
: corresponding above file com/crifan/smartelectricserver/MySQL55DialectUtf8mb4.java
my.cnf
find your my.cnf
in your machine, edit to:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
src/main/resources/application.properties
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/smart_electric?useUnicode=yes&characterEncoding=UTF-8
in which:
useUnicode=yes&characterEncoding=UTF-8
: means connect MySQL with charset utf-8Solved it by applying to the column definition as shown below:
public class Goal{
@Column(name = "SOURCE_OF_FUNDS", length = 6000, columnDefinition = "VARCHAR(6000) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL")
private String sourceOfFunds;
}