19080

UTF-8 from Hibernate

Question:

I have a problem when I retrieve information from a query from my MySql database it comes like this:

Je bâtirai

There is a mistake with UTF-8 charset and Hibernate or with MySql.

How can I solve this please?

This my Hibernate setup:

<pre class="lang-xml prettyprint-override"><?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/church</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="hibernate.connection.CharSet">utf8</property> <property name="hibernate.connection.characterEncoding">utf8</property> <property name="hibernate.connection.useUnicode">true</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- créer la BD --> <!-- property name="hbm2ddl.auto">create</property --> <!-- Met la BD existante à jour --> <!-- property name="hbm2ddl.auto">update</property --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping des classes persistantes --> <mapping class="com.church.metier.User" /> <mapping class="com.church.metier.Comment" /> <mapping class="com.church.metier.Text" /> <mapping class="com.church.metier.MessageText" /> <mapping class="com.church.metier.MessageVideo" /> <mapping class="com.church.metier.News" /> <mapping class="com.church.metier.VerseMonth" /> <mapping package="com.church.metier" /> </session-factory> </hibernate-configuration>

When I retrieve:

<pre class="lang-java prettyprint-override">System.out.println(VerseMonthDAO.retrieveVersetMonth());

My DAO:

<pre class="lang-java prettyprint-override">package com.church.DAO; import com.church.metier.VerseMonth; import java.util.Locale; import org.hibernate.Session; import org.springframework.cglib.core.Local; import org.springframework.context.i18n.LocaleContextHolder; import com.church.util.HibernateUtil; public class VerseMonthDAO { public static void savaOrUpdate(VerseMonth verse) { Session hibernateSession = HibernateUtil.getSession(); org.hibernate.Transaction transc = null; try { transc = hibernateSession.beginTransaction(); hibernateSession.saveOrUpdate(verse); transc.commit(); } catch(Exception e) { if (transc != null) { transc.rollback(); } e.printStackTrace(); } finally { hibernateSession.close(); } } public static String retrieveVersetMonth() { Locale locale = LocaleContextHolder.getLocale(); String language = locale.getLanguage(); if (language.equals("en")) { language = "textEn"; } else if (language.equals("in")) { language = "textIn"; } else { language = "textFr"; } Session hibernateSession = HibernateUtil.getSession(); org.hibernate.Transaction transc = null; String verse = null; Long count; try { transc = hibernateSession.beginTransaction(); count = ((Long) hibernateSession.createQuery("select count(*) from VerseMonth").uniqueResult()); verse = (String) hibernateSession.createQuery ("SELECT "+ language +" FROM VerseMonth verse WHERE verse.verseId = '" + count.intValue() + "'") .uniqueResult(); transc.commit(); } catch(Exception e) { if (transc!=null) { transc.rollback(); } e.printStackTrace(); } finally { hibernateSession.close(); } return verse; } public static String retrieveSourceMonth() { Locale locale = LocaleContextHolder.getLocale(); String language = locale.getLanguage(); if (language.equals("en")) { language = "sourceEn"; } else if (language.equals("in")) { language = "sourceIn"; } else { language = "sourceFr"; } Session hibernateSession = HibernateUtil.getSession(); org.hibernate.Transaction transc = null; String source = null; Long count; try { transc = hibernateSession.beginTransaction(); count = ((Long) hibernateSession.createQuery("select count(*) from VerseMonth").uniqueResult()); source = (String) hibernateSession.createQuery ("SELECT "+ language +" FROM VerseMonth verse WHERE verse.verseId = '" + count.intValue() + "'") .uniqueResult(); transc.commit(); } catch(Exception e) { if (transc!=null) { transc.rollback(); } e.printStackTrace(); } finally { hibernateSession.close(); } return source; } }

my databases:

+---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+ | verseId | SOURCEEN | SOURCEFR | SOURCEIN | TEXTEN | TEXTFR | TEXTIN | +---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+ | 1 | Je bâtirai | Je bâtirai | Je bâtirai | Je bâtirai | Je bâtirai | Je bâtirai | +---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+

thanks for your answers

Answer1:

I find the answer After configure Hibernate with these parameter:

<property name="hibernate.connection.CharSet">utf8</property> <property name="hibernate.connection.characterEncoding">utf8</property> <property name="hibernate.connection.useUnicode">true</property>

We must also configure Spring to UTF-8 with these parameter in the web.xml:

<filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping>

and it works ! Thanks everybody

Answer2:

Make sure your Mysql is UTF-8 encode :

show variables like 'collation_%';

The result should be:

+----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+

Then you need make sure your database is UTF-8 encode:

SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA S WHERE schema_name = 'your_database_name';

The result should be:

+-------------------------------+-------------------------+ | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | +-------------------------------+-------------------------+ | utf8 | utf8_general_ci | +-------------------------------+-------------------------+

Answer3:

Mojibake really sounds like the character set <em>during insertion</em> was latin1, not utf8.

Your output implies that the character set when fetching was correctly utf8.

Recommend

  • Java: DBunitils + Spring: Different Hibernate-Dialect
  • SQL Query to Update One Column Based on Data from Another
  • Root element minOccurs or maxOccurs
  • Why won't EF4 generate a method to support my Function Import?
  • How do I get an Option instead of an Option from a Diesel query which only returns 1 or 0 records?
  • Hibernate generation index and foreign key with same name for MySQL
  • bcrypt-nodejs compare method returns false every time
  • How to set up precision attribute used by @Column annotation?
  • Trying to get all column names in a table
  • Mongoose TypeError: Cannot use 'in' operator to search for '_id' in
  • XSD assert not recognised
  • Cassandra 2.1: Recursion by nesting UDT's
  • Iterate twice through a DataReader
  • Nested projects in multiproject visual studio templates
  • Thread synchronization with syncwarp
  • Retaining data after updating application
  • Examples of how to a STS in .Net 4.5 using WCF
  • HttpClient: disabling chunked encoding
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Jquery Knockout: ko.computed() vs classic function?
  • Clear fused location provider's location for testing
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • ActiveRecord query for a count of new users by day
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Can Jackson SerializationFeature be overridden per field or class?
  • javascript inside java/jsp code
  • WinForms: two way TextBox problem
  • Redux, normalised entities and lodash merge
  • ActionScript 2 vs ActionScript 3 performance
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • Comma separated Values
  • How to disable jQuery.jplayer autoplay?
  • Android Studio and gradle
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key