开发者

UTF8 - Hibernate/MySQL weirdness

开发者 https://www.devze.com 2022-12-12 11:59 出处:网络
I have a db in production where all of my tables are using utf8 / utf8_general_ci encoding. This is basically wor开发者_运维知识库king fine except in one scenario.

I have a db in production where all of my tables are using utf8 / utf8_general_ci encoding. This is basically wor开发者_运维知识库king fine except in one scenario.

What happens is that ??? are being returned for some characters (Chinese, etc); however, they are also returned correctly for the same table but via a different criteria.

I've double checked the connection parameters from Hibernate to MySQL and they have the good charset set.

I cannot understand how this can be happening. The criteria that returns the bad characters is just a simple findById:

Criteria criteria = getHibernateSession().createCriteria(CalendarEvent.class);
criteria.add(Restrictions.eq("id", id));
return (CalendarEvent) criteria.uniqueResult();

This is only happening in production on Solaris - I cannot reproduce it locally.


In your connection-string have you tried

jdbc:mysql://localhost/dbname?characterEncoding=utf8

or add JVM parameter -Dfile.encoding=utf-8 when starting your application / server


Try setting the following properties in your hibernate configuration file:

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

精彩评论

暂无评论...
验证码 换一张
取 消