0

I have a localizable/unicode JSON column on a MySQL database which is accessed via JPA. I get garbled up unicode strings on my API response

{
   "bannerTitle": "Copyright  © 2019",
}

The JSON binds to following entity property

@Column(nullable = false, columnDefinition = "json")
private String settings;

My data source URL has the Unicode tags as follows

jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8&useUnicode=yes

I tried using a converter in the following lines

public class StringToByteConverter implements AttributeConverter<String, byte[]> 

which fixed my fetch queries, but my save queries crashed with

Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.

What is the right way to fix while using the JPA persistence libraries?

OS: Ubuntu 18.04
MySQL: 5.7.27
Java: openjdk version "1.8.0_222"
Spring boot : 1.5.6.RELEASE

rjv
  • 6,058
  • 5
  • 27
  • 49

1 Answers1

0

Try using,

spring.datasource.tomcat.initSQL=SET NAMES 'utf8mb4'

One of the answer here reproduces the same error you have and solve the same by using set names utf8mb4. Way to execute the same in JPA is given here

Shivangi Gupta
  • 866
  • 8
  • 21