使用 hibernate3 从 mysql 检索 utf-8 字符串时出现问题

发布于 2024-12-03 01:34:02 字数 1982 浏览 1 评论 0原文

当我尝试从 mysql 数据库加载对象(一行)时,字符串属性未正确加载,因此当我打印它们时,没有显示任何内容。

这是我的休眠配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>    
    <property name="connection.driver_class">
       com.mysql.jdbc.Driver
    </property>
    <property name="connection.url">
      jdbc:mysql://localhost:3306/demo_hib_1
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password"> </property>
    <property name="pool_size">5</property>
    <property name="show_sql">true</property>
    <property name="dialect">
      org.hibernate.dialect.MySQLDialect
    </property>
    <!-- Mapping files -->

     <mapping resource="com/navid/Person.hbm.xml"/>

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

我尝试向连接 url 添加编码:

jdbc:mysql://localhost:3306/demo_hib_1&characterEncoding=UTF-8

并得到休眠异常:

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
    at com.navid.Main.main(Main.java:31)
Caused by: org.dom4j.DocumentException: Error on line 10 of document  : The reference to entity "characterEncoding" must end with the ';' delimiter. Nested exception: The reference to entity "characterEncoding" must end with the ';' delimiter.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
    ... 3 more

When I try to load an object( a row ) from mysql database, the string properties are not loaded properly , and as a result when I print them, nothing is displayed.

here is my hibernate config file :

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>    
    <property name="connection.driver_class">
       com.mysql.jdbc.Driver
    </property>
    <property name="connection.url">
      jdbc:mysql://localhost:3306/demo_hib_1
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password"> </property>
    <property name="pool_size">5</property>
    <property name="show_sql">true</property>
    <property name="dialect">
      org.hibernate.dialect.MySQLDialect
    </property>
    <!-- Mapping files -->

     <mapping resource="com/navid/Person.hbm.xml"/>

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

I tryed adding encoding to connection url :

jdbc:mysql://localhost:3306/demo_hib_1&characterEncoding=UTF-8

and got hibernate exception :

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
    at com.navid.Main.main(Main.java:31)
Caused by: org.dom4j.DocumentException: Error on line 10 of document  : The reference to entity "characterEncoding" must end with the ';' delimiter. Nested exception: The reference to entity "characterEncoding" must end with the ';' delimiter.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
    ... 3 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2024-12-10 01:34:02

hibernate 配置文件是一个 XML 文件,因此 raw &不允许使用符号

我想到了两个选项(但未经测试!),第一个是使用 XML & 转义序列:

<property name="connection.url">
  jdbc:mysql://localhost:3306/demo_hib_1?useUnicode=true&characterEncoding=UTF-8
</property>

或者使用名称+值语法,这不会需要 &转义:

<property name="connection.url" value="jdbc:mysql://localhost:3306/demo_hib_1?useUnicode=true&characterEncoding=UTF-8" />

请注意,我也添加了第二个选项,我认为您两个都需要

The hibernate config file is an XML file, so raw & symbols aren't allowed

Two options that spring to mind (but untested!), the first would be to use the XML & escape sequence:

<property name="connection.url">
  jdbc:mysql://localhost:3306/demo_hib_1?useUnicode=true&characterEncoding=UTF-8
</property>

Or using name+value syntax, which wouldn't need the & escaping:

<property name="connection.url" value="jdbc:mysql://localhost:3306/demo_hib_1?useUnicode=true&characterEncoding=UTF-8" />

Note that I've added a 2nd option too, I think you need both

奢欲 2024-12-10 01:34:02

您是否尝试添加

<property name="connection.characterEncoding">UTF-8</property>

到会话工厂配置。

来自 Hibernate 文档:

可以通过在连接属性名称前添加“hibernate.connection”来给出任意连接属性。例如,您可以使用 hibernate.connection.charSet 指定 charSet 连接属性。

Did you try adding

<property name="connection.characterEncoding">UTF-8</property>

to session factory configuration.

From Hibernate documentation:

Arbitrary connection properties can be given by prepending "hibernate.connection" to the connection property name. For example, you can specify a charSet connection property using hibernate.connection.charSet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文