英文:
saving emoji to mysql using hibernate Incorrect string value: '\xF0\x9F\x98\x88\xF0\x9F...' for column 'name' at row 1
问题
我的连接字符串具有以下属性
useUnicode=true&characterEncoding=utf8&character_set_server=utf8mb4&charset=utf8mb4
我使用了以下代码
jpaProperties.put("hibernate.connection.useUnicode", true);
jpaProperties.put("hibernate.connection.characterEncoding", "utf8");
jpaProperties.put("hibernate.connection.CharSet", "utf8mb4");
数据库也支持 utf8mb4,因为当我手动添加记录时,它可以正确保存
但在尝试保存表情符号时仍然出现错误
Incorrect string value: '\xF0\x9F\x98\x88\xF0\x9F... ' for column 'name' at row 1
英文:
my connection string has the following properties
useUnicode=true&characterEncoding=utf8&character_set_server=utf8mb4&charset=utf8mb4
and I used
jpaProperties.put("hibernate.connection.useUnicode", true);
jpaProperties.put("hibernate.connection.characterEncoding", "utf8");
jpaProperties.put("hibernate.connection.CharSet", "utf8mb4");
the DB also supports utf8mb4 since when I add a record manually it saves it correctly
still getting errors when trying to save an emoji
Incorrect string value: '\xF0\x9F\x98\x88\xF0\x9F...' for column 'name' at row 1
答案1
得分: 0
我非常有信心地认为您清楚地表达了您打算在整个技术堆栈中使用UTF-8,除了您的数据。
您真正的问题在于,您的数据(原始字符串)一开始就不是有效的UTF-8。您可以使用以下代码片段轻松验证:
public static boolean isValidUTF8(byte[] input) {
CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
try {
utf8Decoder.decode(ByteBuffer.wrap(input));
return true;
} catch (CharacterCodingException e) {
return false;
}
}
您应该改为始终使用 utf8mb4
(包括列定义,应为 ... CHARSET=utf8mb4 COLLATE utf8mb4_general_ci
或者 ... CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
)。
您需要根据文档对MySQL Connector版本(和配置)格外注意。
英文:
I'm quite confident that you clearly expressed your intention to use UTF-8 against your whole technology stack... except your data.
Your real issue here is that your data (the original string) is not valid UTF-8 to begin with. You can easily verify this with the following snippet:
public static boolean isValidUTF8(byte[] input) {
CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
try {
utf8Decoder.decode(ByteBuffer.wrap(input));
return true;
} catch (CharacterCodingException e) {
return false;
}
}
You should instead use utf8mb4
all the way (including your column definition, which should be ... CHARSET=utf8mb4 COLLATE utf8mb4_general_ci
or ... CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
).
You need to pay extra attention on MySQL Connector version (and configuration) according to the documentation.
答案2
得分: 0
我通过将 mysql-connector-java 升级到 5.1.49 并在连接字符串中添加以下内容来解决了这个问题:
{连接字符串}?characterEncoding=UTF-8&useUnicode=true
参考链接:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html
英文:
I Solved it by upgrading the mysql-connector-java to 5.1.49 and adding the following to the connection string
{connection string}?characterEncoding=UTF-8&useUnicode=true
reference:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-charsets.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论