When running the Spring Boot JPA example: https://spring.io/guides/gs/accessing-data-jpa/ against a MySQL database and trying to store the "pile of poo" emoji 💩I get the exception java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xA9\xF0\x9F...'
I configured my database to use utf8mb4 encoding.
I know this is NOT a problem with MySQL as I am able to create a Customer and store it in the database using the MySQL client. I can even run the sample application and have it find a Customer with the "pile of poo" emoji.
2015-06-05 18:10:12.382 INFO 5119 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@446b0224: startup date [Fri Jun 05 18:10:12 PDT 2015]; root of context hierarchy
2015-06-05 18:10:13.567 INFO 5119 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2015-06-05 18:10:13.588 INFO 5119 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2015-06-05 18:10:13.648 INFO 5119 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {4.3.8.Final}
2015-06-05 18:10:13.649 INFO 5119 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2015-06-05 18:10:13.651 INFO 5119 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2015-06-05 18:10:13.804 INFO 5119 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
2015-06-05 18:10:14.086 INFO 5119 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2015-06-05 18:10:14.171 INFO 5119 --- [ main] o.h.h.i.ast.ASTQueryTranslatorFactory : HHH000397: Using ASTQueryTranslatorFactory
2015-06-05 18:10:14.365 INFO 5119 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000228: Running hbm2ddl schema update
2015-06-05 18:10:14.365 INFO 5119 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000102: Fetching database metadata
2015-06-05 18:10:14.366 INFO 5119 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000396: Updating schema
2015-06-05 18:10:14.390 INFO 5119 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000261: Table found: poo_test.Customer
2015-06-05 18:10:14.390 INFO 5119 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000037: Columns: [id, lastname, firstname]
2015-06-05 18:10:14.390 INFO 5119 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000108: Foreign keys: []
2015-06-05 18:10:14.391 INFO 5119 --- [ main] o.hibernate.tool.hbm2ddl.TableMetadata : HHH000126: Indexes: [primary]
2015-06-05 18:10:14.391 INFO 5119 --- [ main] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000232: Schema update complete
2015-06-05 18:10:14.777 INFO 5119 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
Customer found with findOne(1L):
--------------------------------
Customer[id=1, firstName='💩💩💩💩', lastName='💩💩💩💩']
2015-06-05 18:10:14.836 INFO 5119 --- [ main] hello.Application : Started Application in 2.819 seconds (JVM running for 3.077)
2015-06-05 18:10:14.837 INFO 5119 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@446b0224: startup date [Fri Jun 05 18:10:12 PDT 2015]; root of context hierarchy
2015-06-05 18:10:14.838 INFO 5119 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2015-06-05 18:10:14.839 INFO 5119 --- [ Thread-1] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
This is my modified Application.java from the sample:
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
CustomerRepository repository;
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Override
public void run(String... strings) throws Exception {
// save a couple of customers
repository.save(new Customer("💩💩💩💩", "💩💩💩💩"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer by ID
Customer customer = repository.findOne(1L);
System.out.println("Customer found with findOne(1L):");
System.out.println("--------------------------------");
System.out.println(customer);
System.out.println();
}
}
This is the application.yml file I'm using to configure my JDBC connection. I'm using a number of techniques I've found while researching this issue but nothing seems to work.
spring:
datasource:
url: "jdbc:mysql://localhost:3306/poo_test?useUnicode=yes&characterEncoding=utf8&characterResultSets=utf8"
username: root
password:
connectionInitSqls: "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;"
driverClassName: com.mysql.jdbc.Driver
testOnBorrow: true
validationQuery: SELECT 1
jpa:
show-sql: false
database-platform: org.hibernate.dialect.MySQL5Dialect
hibernate:
ddl-auto: update
naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
connection:
CharSet: utf8mb4
characterEncoding: utf8
useUnicode: true
This appears to be a common problem but I've yet to find a solution. Any help would be much appreciated.
After trying various approaches I managed to get things working using the following application.yml
spring:
datasource:
url: "jdbc:mysql://localhost:3306/poo_test?useUnicode=yes&characterEncoding=utf8"
username: root
password:
initSQL: "SET NAMES 'utf8mb4'"
jpa:
hibernate:
ddl-auto: update
The trick seems to be the initSQL
property
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With