Search code examples
javamysqlhibernatejdbcemoji

saving emoji to mysql using hibernate 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

Solution

  • 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