Chinaunix首页 | 论坛 | 博客
  • 博客访问: 194586
  • 博文数量: 76
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 490
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-12 16:58
文章分类

全部博文(76)

文章存档

2011年(3)

2010年(52)

2009年(21)

我的朋友

分类: Java

2011-06-01 19:11:31


本文參考自:http://blog.tremend.ro/2007/08/14/how-to-set-the-default-charset-to-utf-8-for-create-table-when-using-hibernate-with-java-persistence-annotations/,感謝提供者spostelnicu.

Question: hibernate與mysql開發中,新增和修改時出現中文亂碼 
Answer:  
步驟一:將hibernate.cfg.xml中的connection.url值加入characterEncoding=utf8,具體如下:

 

  1.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     
  2.     "">    
  3.     
  4.     
  5. <hibernate-configuration>    
  6.   <session-factory>    
  7.     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_first?characterEncoding=utf8property>    
  8.     ...     
  9.       
  10.   <session-factory>    
  11. <hibernate-configuration>   

步驟二:在建表時,一定要將MySQL中的資料表格的欄位字符集設置成utf8的 ,否則會拋出類似如下錯誤: 
錯誤訊息:com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'name' at row 1; 

如果是在外部建表,建表語句寫法如下:

  1.          create table `my_table` (  
  2.     `Id` int(11) NOT NULL auto_increment,  
  3.     `my_column` mediumtext NOT NULL default ”,  
  4.     ......  
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
  6.           

另外, hibernate的ORM機制允許我們選建實體,再建表,可以通過hibernate內部的hbm2ddl將實體類直接導出成關系表,可以寫一個 
hbm2ddl操作類,如下: 
java文件:ExportDB.java

  1. package com.jason.hibernate;  
  2.   
  3.         import org.hibernate.cfg.Configuration;  
  4.         import org.hibernate.tool.hbm2ddl.SchemaExport;  
  5.   
  6.         public class ExportDB {  
  7.   
  8.             /** 
  9.              * 執行本程式將會建立資料庫表格 
  10.              * @param args 
  11.              */  
  12.             public static void main(String[] args)  {  
  13.                 // TODO Auto-generated method stub  
  14.   
  15.                 /** 
  16.                  * 讀取hibernate配置文件 
  17.                  * 加上.configure()會去讀取hibernate.hbm.xml文件,如果沒有設定. 
  18.                  * 則會默認讀取hibernate.properties 
  19.                  */  
  20.                  Configuration cfg = new Configuration().configure();  
  21.                            
  22.                  //讀取配置檔,讀取User.hbm.xml成ddl  
  23.                  SchemaExport export = new SchemaExport(cfg);          
  24.                 //生成實體表  
  25.                  export.create(truetrue);  
  26.             }  
  27.         }  

此時,需要額外對hibernate.cfg.xml進行設定, 先寫一個客戶方言類CustomerMySQLDialect.java,
 重載MySQLDialect的getTableTypeString()方法,實現對生成的表格進行字符集設定

 java文件: CustomerMySQLDialect.java

  1. package com.jason.hibernate;     
  2.     
  3.     import org.hibernate.dialect.MySQLDialect;     
  4.     
  5.     public class CustomerMySQLDialect extends MySQLDialect {     
  6.     
  7.         /**   
  8.          * 重載getTableTypeString()方法   
  9.          */    
  10.         public String getTableTypeString()     
  11.         {     
  12.             return " ENGINE=InnoDB DEFAULT CHARSET=utf8";            
  13.         }     
  14.     }    

之後,將hibernate的方言映身到該文件: 
文件: hibernate.cfg.xml

  1.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     
  2.         "">    
  3.     
  4.     
  5.     <hibernate-configuration>    
  6.       <session-factory>    
  7.         ...     
  8.         <property name="hibernate.dialect">com.jason.hibernate.CustomerMySQLDialectproperty>    
  9.         ...     
  10.           
  11.       <session-factory>    
  12.     <hibernate-configuration>    

這樣就一切OK了,其它的方法沒試過,不過這個我運行過是ok的. 
另外說明: 在網上有一些提供以下的方法,不過經過我的驗證,這種方法是不行的,因為Hibernate3中好像沒有hibernate.connection.charSet屬性: 
文件: hibernate.cfg.xml

  1.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"     
  2.         "">    
  3.     
  4.     
  5. <hibernate-configuration>    
  6.   <session-factory>    
  7.     ...     
  8.     <property name="hibernate.connection.charSet">utf8(或utf-8)property>    
  9.     ...     
  10.       
  11.   <session-factory>    
  12. <hibernate-configuration>    

如有不对或不足之处,望予以指正...不胜感激...

阅读(609) | 评论(0) | 转发(0) |
0

上一篇:再说Play!framework

下一篇:没有了

给主人留下些什么吧!~~