Hibernate的UTF-8 [英] UTF-8 from Hibernate

查看:57
本文介绍了Hibernate的UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从MySql数据库的查询中检索信息时出现问题:

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

Je bâtirai 

UTF-8字符集和Hibernate或MySql存在错误.

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

请问该如何解决?

这是我的Hibernate设置:

This my Hibernate setup:

<?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.retrieveVersetMonth());

我的DAO:

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;
    }
}

我的数据库:

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

感谢您的回答

推荐答案

我找到了答案使用以下参数配置Hibernate之后:

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>

我们还必须在web.xml中使用以下参数将Spring配置为UTF-8:

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>

它有效!谢谢大家

这篇关于Hibernate的UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆