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>