hibernate的中文问题的解决方案

hibernate的中文问题的解决方案

系统配置:win2k3 server,jsdk 1.5.0 rc,mysql 4.0.20a,hibernate 2.1.0 ,elcipse 3.0.1
问题描述:在使用hibernate作为数据持久层的方案时,照样会遇到中文问题,具体情况时插入到数据库中的汉字显示为??,显示数据时汉字为??
探索: 经过试验发现与汉字编码有关的地方有以下两处,一个是数据库连接url ,一个是编程时获取Configuration 类的实例的语句的方式。
方案1:
    * 编程时获取Configuration 类的实例的语句为
    * new Configuration().addClass(xx.class);
    必须在hibernate.properties文件里数据库连接url后加上?characterEncoding=gbk&useUnicode=true
方案2:
    * 编程时获取Configuration 类的实例的语句为
    * new Configuration().configure().
    在hibernate.cfg.xml文件的<session-factory/>块中设置
        <property name="connection.useUnicode">true</property>
        <property name="connection.characterEncoding">UTF-8</property>
    或编程时写:
      Properties extraProperties = new Properties();
      extraProperties.put("hibernate.connection.useUnicode", "true");
      extraProperties.put("hibernate.connection.characterEncoding", "UTF-8");
      myConfiguration.addProperties(extraProperties);

原来怀疑与xml页面的编码有关试了一下,好像不起作用。

你可能感兴趣的:(hibernate的中文问题的解决方案)