Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert emoji does not work with spring-boot and MariaDB

I would like to insert emoji like πŸ˜ƒ in mariaDB database but I always get a sql error.

Here is the stacktrace:

12-01-2018 16:01:44.466 [Executor - Migration - 1] WARN  o.h.e.jdbc.spi.SqlExceptionHelper.logExceptions:129 - SQL Error: 1366, SQLState: 22007
12-01-2018 16:01:44.466 [Executor - Migration - 1] ERROR o.h.e.jdbc.spi.SqlExceptionHelper.logExceptions:131 - (conn:498) Incorrect string value: '\xF0\x9F\x92\xB3\xF0\x9F...' for column 'notes' at row 1
Query is: insert into customer (backend_archiving_date, backend_creation_date, backend_update_date, genius_client_id, address, birthday, city, company_name, country, email, fidelity_account_id, first_name, last_name, notes, phone, siret, zip_code, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?), parameters [<null>,'2018-01-12 15:01:44.454','2018-01-12 15:01:44.454',2159,'20 rue raspail
',<null>,'LEVALLOIS PERRET','','FRANCE','','00c55854-99df-4db1-88b9-34f7e5608477','claude','amsellem','πŸ’³πŸ’³πŸ’³πŸ’³','','','92300','2152ed7d-80a1-4305-9fcb-4e21f5947e32']

I my Maria db, the table has

ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

and the field "notes" has also "utf8mb4 - Unicode" charset.

When I insert via a sql query

update customer set notes='πŸ’³πŸ’³πŸ’³πŸ’³' where id = 'f5920301-5ee0-4d58-a786-d4701d9e9d73';

it works, but when I want to insert emoji with my program (spring-boot, spring-data-jpa, hibernate) I always get the error (see stacktrace).

EDIT

FYI Here is the creation script

CREATE TABLE `customer` (
`id` char(36) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`phone` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`birthday` date DEFAULT NULL,
`notes` varchar(2048) DEFAULT NULL,
`address` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`country` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`zip_code` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`company_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`siret` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`fidelity_account_id` char(36) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
`backend_creation_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`backend_update_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`backend_archiving_date` timestamp NULL DEFAULT NULL,
`genius_client_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`),
KEY `fk_customer_fidelity_account` (`fidelity_account_id`),
CONSTRAINT `fk_customer_fidelity_account` FOREIGN KEY (`fidelity_account_id`) REFERENCES `fidelity_account` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

EDIT 2

Here is the connection params (spring-boot application.yml)

#Β Datasource configuration
spring:
  datasource:
    type: "com.zaxxer.hikari.HikariDataSource"
    url: "jdbc:mariadb://localhost:3306/genius_back useUnicode=yes&characterEncoding=UTF8"
    hikari:
      driver-class-name: "org.mariadb.jdbc.Driver"
      username: "mariadb"
      password: "mariadb"
      minimum-idle: 5
      maximum-pool-size: 20
      validation-query: "SET NAMES utf8mb4"
  jackson:
     serialization:
      write_dates_as_timestamps: false
  http:
     client-user-agent: "Genius"
     multipart:
      max-file-size: 100Mb
      max-request-size: 150Mb
like image 704
psv Avatar asked Dec 02 '22 10:12

psv


1 Answers

OKAY, I found the problem.

The solution was to add

spring:
  datasource:
     connectionInitSql: "SET NAMES 'utf8mb4'" 

in the application.yml.

connectionInitSql is used by HikariCP when it open the connection.

like image 200
psv Avatar answered Dec 04 '22 23:12

psv