0

I know there are a lot of similar questions already, but none of it fixed my problem. I would like to simply save emojis to the DB. The request hits the server correctly, with the correct formatting, but the DB saves only question marks instead of the emoji. If I insert the emoji with a mysql query directly, it works. Wildfly is connected to the DB correctly. I made the following steps:

  • In MySQL I executed this query:

ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

The collations of the table are all utf8mb4_unicode_ci

  • I use JNDI to connect to the DB. The datasource connection url in Wildfly:

jdbc:mysql://mydomain/mydb?useUnicode=yes

Also tried: jdbc:mysql://mydomain/mydb?useUnicode=true&useEncoding=true&character_set_server=utf8mb4

And: jdbc:mysql://mydomain/mydb?useUnicode=true&useEncoding=true&characterEncoding=UTF-8&character_set_server=utf8mb4

  • In Spring the configuration part is:
<property name="jpaProperties">
    <props>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.connection.CharSet">utf8mb4</prop>
        <prop key="hibernate.connection.characterEncoding">utf8mb4</prop>
        <prop key="hibernate.connection.useUnicode">true</prop>
    </props>
</property>
  • I use the 8.0.11 version of the mysql-connector. Wildfly version: 11.0. MySQL version: 5.7.22

I have no idea what else should I do to make it work. It is 2018 and this is still not working by default. Crazy. Any suggestion would be appreciated.

Márk Farkas
  • 1,426
  • 1
  • 12
  • 25

1 Answers1

0

The solution was to modify the /etc/my.cnf MySQL config file almost as explained here:

https://stackoverflow.com/a/3513812/4109477

But with these two lines:

[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

It is important to use utf8mb4 instead of utf8.

The question might be a duplication, but maybe worth to leave it here to help others who think they have tried everything like I did, and have similar conditions.

Márk Farkas
  • 1,426
  • 1
  • 12
  • 25