Piane_Ramso
Piane_Ramso

Reputation: 51

JPA (Hibernate) Cyrillic characters

I have Spring Roo app using JPA implemented through Hibernate. I can't use cyrillic symbols because they become '????' in the DB.

1) Direct insert of cyrillic symbols into DB is OK.

2) If there are any cyrillic symbols in the DB, they are displayed fine on a web page.

3) I have such part of config in web.xml

 <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

4) While debugging (before insert to DB) in IDE cyrillic symbols are displaying OK, but after insert and then select symbols are '????'.

5) I tried to set 'accept-charset=UTF-8' attr to 'form' tag, tried to add characterEnc‌​oding=UTF-8 to DB URL, tried to set the following hibernate properties:

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

What else can be wrong? Why cyrillic symbols still become '????' ?!

p.s. Ubuntu 13.04, MySQL 5.5, Spring 3.2, Spring Roo 1.2.3

upd: Funally got the solution: I generated ru_RU and ru_RU.UTF-8 system locale and set locale to ru_RU.UTF-8. Detailed instruction is here. And, I can't explain why, all works fine!.

p.p.s. characterEncoding and useUnicode properties from '5' aren't needed. characterEncoding=UTF-8 parameter needed to be added to connection string.

Upvotes: 2

Views: 1934

Answers (1)

eruiz
eruiz

Reputation: 1973

Edit MySQL settings /etc/mysql/my.cnf and force UTF-8 compliance for all connections:

[client]
default-character-set = utf8

[mysqld]
init-connect='SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci

[mysql]
default-character-set = utf8

It is recommended to recreate the database after reconfiguring it.

Upvotes: 2

Related Questions