【问题标题】:How to set UTF-8 properly in Hibernate?如何在 Hibernate 中正确设置 UTF-8?
【发布时间】:2025-12-20 02:20:10
【问题描述】:

我将 Hibernate 用于 JEE 解决方案。我需要数据库连接为 UTF-8。这是我尝试过的:

数据库端

mysql> select c.character_set_name from information_schema.tables as t, information_schema.collation_character_set_applicability as c where c.collation_name = t.table_collation and t.table_schema = "ir2016" and t.table_name = "personne";
+--------------------+
| character_set_name |
+--------------------+
| utf8               |
+--------------------+
1 row in set (0.01 sec)

我还插入了来自 MySQL Workbench 的示例数据。结果很好地以 UTF-8 编码。所以问题肯定出在JEE服务器端。

JEE 方面

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--Databaseconnectionsettings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/ir2016?useUnicode=true&amp;characterEncoding=utf-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">xxxx</property>    
        <property name="connection.useUnicode">true</property>
        <property name="connection.characterEncoding">utf8</property>
        <property name="connection.CharSet">utf8</property>

        <!--JDBC connectionpool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!--SQL dialect-->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--EnableHibernate'sautomaticsession contextmanagement -->
        <property name="current_session_context_class">thread</property>

        <!--Disablethe second-levelcache -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!--Echo all executedSQL to stdout-->
        <property name="show_sql">true</property>

        <mapping resource="Quiz.hbm.xml"/>
        <mapping resource="Proposition.hbm.xml"/>
        <mapping resource="Question.hbm.xml"/>
        <mapping resource="Personne.hbm.xml"/>
        <mapping resource="Choisir.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

server.xml

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8"/>

JSP 页面

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>

有人可以帮忙吗?

【问题讨论】:

  • 那么,有什么问题吗?你不能用 utf-8 写到你的数据库吗?
  • 是的,问题是我仍然无法在 utf8 中写入 db
  • 第一个输出可能无关紧要;而是提供SHOW CREATE TABLE personne;
  • “不能写”是什么意思??服务器崩溃?什么都没发生?垃圾是放到桌子上的吗?例外?还有什么?

标签: java mysql hibernate utf-8


【解决方案1】:

这是一个 JEE 解决方案,因此您需要检查您的 web.xml 是否配置为接受 UTF-8,我忘了它。我需要一个过滤器来做到这一点

web.xml

<filter> 
  <filter-name>CharacterEncodingFilter</filter-name> 
  <filter-class>com.yourcompany.yourapp.util.CharacterEncodingFilter</filter-class> 
  <init-param> 
    <param-name>requestEncoding</param-name> 
    <param-value>UTF-8</param-value> 
  </init-param> 
</filter> 

<filter-mapping> 
  <filter-name>CharacterEncodingFilter</filter-name> 
  <url-pattern>/*</url-pattern> 
</filter-mapping> 

CharacterEncodingFilter.java

package com.yourcompany.yourapp.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Struts 1.3.10 encoding problem
 * 
 * @see http://www.coderanch.com/t/557874/Struts/Struts-encoding
 */
public class CharacterEncodingFilter implements Filter { 
    private FilterConfig fc; 

    @Override
    public void destroy() {
        // TODO Auto-generated method
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req; 
        HttpServletResponse response = (HttpServletResponse) resp; 

        request.setCharacterEncoding("UTF8"); 
        response.setCharacterEncoding("UTF8"); 

        chain.doFilter(request, response); 

        request.setCharacterEncoding("UTF8"); 
        response.setCharacterEncoding("UTF8"); 
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
    }
}

来源:Struts 1.3.10 encoding problem

【讨论】: