2011-12-14 83 views
0

我無法獲得將數據庫中的utf8字符寫入休眠狀態。如果我使用phpmyadmin或類似工具手動編寫它們,它會顯示在網頁中,並且一切正常。但是,如果我mannualy填寫表單並提交它,然後使用休眠將它保存到數據庫它給出įųÄųÄ。在MySQL一切都是UTF-8:Java EE + Hibernate + Glassfish 3.1 + MySQL 5.5.16 - > utf不工作

mysql> SHOW VARIABLES LIKE 'collation%'; 
+----------------------+-------------------+ 
| Variable_name  | Value    | 
+----------------------+-------------------+ 
| collation_connection | utf8_general_ci | 
| collation_database | utf8_general_ci | 
| collation_server  | utf8_general_ci | 
+----------------------+-------------------+ 

mysql> SHOW VARIABLES LIKE 'character_set%'; 
+--------------------------+----------------------------+ 
| Variable_name   | Value      | 
+--------------------------+----------------------------+ 
| character_set_client  | utf8      | 
| character_set_connection | utf8      | 
| character_set_database | utf8      | 
| character_set_filesystem | binary      | 
| character_set_results | utf8      | 
| character_set_server  | utf8      | 
| character_set_system  | utf8      | 
| character_sets_dir  | /usr/share/mysql/charsets/ | 
+--------------------------+----------------------------+ 

所有表/在VARCHAR表中的列是utf-8 general

我休眠-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     "> 

    <!-- Enable annotation style of managing transactions --> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions --> 
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->  
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html --> 
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html --> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- init-method="createDatabaseSchema" --> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="packagesToScan" value="lt.database.model" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.connection.useUnicode">true</prop> 
       <prop key="hibernate.connection.characterEncoding">UTF-8</prop> 
       <prop key="hibernate.connection.charSet">UTF-8</prop>     
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

    <!-- Declare a datasource that has pooling capabilities--> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
    destroy-method="close" 
    p:driverClass="com.mysql.jdbc.Driver" 
    p:jdbcUrl="jdbc:mysql://localhost/aukcionas?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8" 
    p:user="root" 
    p:password="" 
    p:acquireIncrement="5" 
    p:idleConnectionTestPeriod="60" 
    p:maxPoolSize="100" 
    p:maxStatements="50" 
    p:minPoolSize="10" /> 

    <!-- Declare a transaction manager--> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
      p:sessionFactory-ref="sessionFactory" /> 

</beans> 

那麼爲什麼還是不能得到這個東西的工作?我使用:

  • GlassFish的3.1
  • 的MySQL 5.5.16
  • 休眠3
  • 春3.0.2
  • 春季安全3.0.7
+0

您確定將正確的值傳遞給hibernate - 是否在調用hibernate API檢查之前記錄了值? – gkamal 2011-12-14 02:11:23

回答

0

由我自己定。將此添加到web.xml文件中:

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