4

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
1
  • How do you "print" string properties? Have you checked in a debugger that the data coming back from MySQL is indeed rubbish or whether it is the printing that doesn't work? Commented Sep 5, 2011 at 12:18

2 Answers 2

10

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 &amp; escape sequence:

<property name="connection.url">
  jdbc:mysql://localhost:3306/demo_hib_1?useUnicode=true&amp;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

Sign up to request clarification or add additional context in comments.

7 Comments

I used &amp escaping and exception does not occure. but I still have the original problem : strings are not printed properly. when I print any string attribute I get a blank string whose length is twice as long as the original string stored in the database.
The "twice as long" may indicate a mix up between UTF-8 and UTF-16 at some point, though I can't immediately see why that would result in a blank (as opposed to incorrect) string.
Might be a case of "Garbage In, Garbage Out" - if you've previously stored data without correctly setting the encoding you'll have trouble getting it back again! Also double check that both your database and table are set for UTF-8, otherwise you can get some very odd behaviour
I checked both database and table encoding, both utf-8. also I removed the data and inserted it again to ensure "Garbage in Garbage out" is not happening. Still the problem is not solved !
actually, I had to use this escaping in name+value case also
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.