2016-09-06 106 views
1

我有一個問題,當我從我的MySQL數據庫檢索查詢信息涉及這樣的:UTF-8從休眠

Je bâtirai 

有使用UTF-8字符集一個錯誤,休眠或與MySQL。

我該如何解決這個問題?

這是我的休眠設置:

<?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> 

當我檢索:

System.out.println(VerseMonthDAO.retreveVersetMonth()); 

吾道:

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 retreveVersetMonth() { 
     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 retreveSourceMonth() { 
     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; 
    } 
} 

我的數據庫:

+---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+ 
| 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                       | 
+---------+----------------+----------------+----------------+-------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+ 

感謝您的回答

+0

請提供'SHOW CREATE TABLE'。 –

+0

在第一次運行hibernate創建所有表創建 CMJ

+0

然後我更改hbm2ddl.auto以在第二次運行時更新 – CMJ

回答

1

我找到了答案 配置後休眠與這些參數:

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

我們還必須配置Spring爲UTF-8這些參數在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> 

,它作品!謝謝大家

0

確保您的MySQL是UTF-8編碼:

show variables like 'collation_%'; 

結果應該是:

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

然後,你需要確保你的數據庫是UTF-8編碼:

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

結果應該是:

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

字符集是相關的,排序規則是次要的。 –

+0

mysql> SELECT DEFAULT_CHARACTER_SET_NAME,DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA S WHERE schema_name ='church'; + ---------------------------- + ------------------- ----- + | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | + ---------------------------- + ------------------- ----- + | utf8 | utf8_general_ci | + ---------------------------- + ------------------- ----- + 設置1行(0.00秒) – CMJ

+0

mysql>顯示像'collat​​ion_%'這樣的變量; + ---------------------- + ----------------- + |變量名|值| + ---------------------- + ----------------- + | collat​​ion_connection | utf8_general_ci | | collat​​ion_database | utf8_general_ci | | collat​​ion_server | utf8_general_ci | + ---------------------- + ----------------- + 3行集合(0.00秒) – CMJ

0

變爲亂碼真的聽起來像字符集插入期間是latin1的,不是UTF8。

您的輸出意味着提取時的字符集是正確的utf8。

+0

怎麼說插入utf-8好嗎? – CMJ

+0

我看到'hibernate.connection.CharSet'(etc),但這些設置在創建表時是有效的嗎?當執行INSERT時? –

+0

在創建表之前我在hibernate.cfg.xml中寫過 – CMJ