-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Persisting Maps Containing UTF-8 Characters
PostPosted: Wed Oct 01, 2003 11:45 am 
Newbie

Joined: Wed Oct 01, 2003 10:52 am
Posts: 6
Location: Oslo, Norway
I am running into problems trying to persist a map containing various UTf-8 (non-ascii) characters. I have a Map containing String/Integer pairs. When I try to store the map, I get the following SQL exception:

Code:
(util.JDBCExceptionReporter          38  ) SQL Error: 1062, SQLState: S1009
(util.JDBCExceptionReporter          46  ) Invalid argument value,  message from server: "Duplicate entry '111-?????' for key 1"
(util.JDBCExceptionReporter          38  ) SQL Error: 1062, SQLState: S1009
(util.JDBCExceptionReporter          46  ) Invalid argument value,  message from server: "Duplicate entry '111-?????' for key 1"
(util.JDBCExceptionReporter          37  ) Could not synchronize database state with session
java.sql.BatchUpdateException: Invalid argument value,  message from server: "Duplicate entry '111-?????' for key 1"


Given that I am trying to persist a map, I can't see how it's possible to have this error... I can't possibly have duplicate keys in the map. Note that the "?????" values are actually some Japanese UTF-8 characters, not displayable byt he console. I am a bit lost as to where the and why Hibernate tries to insert duplicate values into the database. It seems to actually be trying to insert the literal string ????? instead of the actual data.

At first, I thought it was a simple problem of MySQL not supporting UTF-8 data in v4.0, so I then created a custom type which Base64 encodes the data prior to storing it:

Code:
public void nullSafeSet(PreparedStatement st, Object value, int index)
        throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.VARCHAR);
        } else {
            String base64String = encode((String)value);
            System.out.print(base64String + " " + (String) value);
            for(int i = 0; i < base64String.getBytes().length; i++) {
                System.out.print(base64String.getBytes()[i] + " ");
            }
   
            st.setString(index, base64String);
        }
    }


The duplicate entry errors still occured when using this custom type... leading me to belive that Hibernate is doing something a bit 'funny' with the map data prior to storing it?

I have found a work-around to this problem, which is to Base64 encode the data before putting it into the map. This eliminates the duplicate entry errors... which seems to point a finger at hibernate doing somethign to the data? But it also forces any code using the map to know that the data is in Bas64 format... which is not something I want.

Any suggestions or hints?

Thanks,
-mike

For reference, here is the mapping file for teh problematic map:
Code:
<map
    name="strOccMap"
    table="strOccMap"
    lazy="false"
    sort="unsorted"
    inverse="false"
    cascade="none"
    order-by="str"
   >

        <key column="id" />

        <index column="str"
                  type="string" />

        <element column="occurances"
                  type="int"
                  not-null="false"
                  unique="false" />

</map>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2003 11:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate does not do anything "funny".

Are you -sure- the exception only occurs with internationalized strings? If so, it is your JDBC driver or database that is responsible.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2003 12:16 pm 
Newbie

Joined: Wed Oct 01, 2003 10:52 am
Posts: 6
Location: Oslo, Norway
Hi Gavin, I'm sure the exception only occurs when using i18n strings. I originally blamed teh database and/or the driver (MySQL 3.23), but playing with a custom type seems to indicate things go wrong outside of the db/driver area.

Using my simple UserType which just Base64 encodes the string in the nullSafeSet() method, I get the following Exception:

Code:
(util.JDBCExceptionReporter          38  ) SQL Error: 1062, SQLState: S1009
(util.JDBCExceptionReporter          46  ) Invalid argument value,  message from server: "Duplicate entry '112-Pz8/Pz8=' for key 1"
(util.JDBCExceptionReporter          38  ) SQL Error: 1062, SQLState: S1009
(util.JDBCExceptionReporter          46  ) Invalid argument value,  message from server: "Duplicate entry '112-Pz8/Pz8=' for key 1"
(util.JDBCExceptionReporter          37  ) Could not synchronize database state with session


Since the same Base64 encoded keys are coming out of the custom type(verfied with some print statements), this points to something going astray before the database or driver has a chance to mess things up? I'd blame my custom type for any problems, but this issue happens with or without it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2003 12:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I guarantee you that Hibernate does not meddle with strings in any way.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.