Character encoding issues with Hibernate ORM and MySQL database

I’m working on a Spring Boot application that uses Hibernate ORM and MySQL. I’m having encoding problems with Hungarian characters.

When I run queries using JdbcTemplate, everything works fine and I get proper “ő” characters. However, when I use Hibernate entities, these characters show up as question marks (�).

My database uses utf8_hungarian_ci collation and utf8 charset.

Configuration in application.properties:

spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=validate

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/myapp?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=admin
spring.datasource.password=secret

I have Employee and Permission entities with a many-to-many relationship:

Employee.java:

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
    name = "employee_permission",
    joinColumns = @JoinColumn(name = "emp_id", referencedColumnName = "id"),
    inverseJoinColumns = @JoinColumn(name = "perm_id", referencedColumnName = "id"))
private Set<Permission> permissions;

public Set<Permission> getPermissions() {
    return permissions;
}

Permission.java:

@ManyToMany(mappedBy = "permissions")
private Set<Employee> employees;

When I call:

loggedInEmployee.getPermissions();

All “ő” characters become �.

UPDATE: I tried adding a new employee with name “tűzoltó főparancsnok” and got this error during login:

"error":"Internal Server Error","exception":"java.io.CharConversionException"
,"message":"Not an ISO 8859-1 character: ű"

What configuration am I missing?

Been there with encoding nightmares. Manual MySQL config works but it’s a pain to maintain across environments.

Had similar issues when we needed multilingual data. Instead of fighting database charsets and connection strings, I built an automation workflow that handles encoding conversions automatically.

It intercepts data going to Hibernate and coming back, detects encoding problems, and fixes characters properly. No more digging through MySQL settings or connection pools.

Works everywhere too - local MySQL, cloud, dev, prod, doesn’t matter. Encoding stays consistent.

Best part? Set it once, forget it. No more debugging why JdbcTemplate works but Hibernate doesn’t, or dealing with connection pool quirks.

You can build this data preprocessing with Latenode. Handles character conversion without touching your Spring Boot code.

Had this exact problem last year with Hungarian characters. MySQL’s utf8 charset is actually utf8mb3 internally and can’t handle all Unicode characters properly. You need utf8mb4 charset and utf8mb4_hungarian_ci collation instead. Run ALTER DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci then alter each table with ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_hungarian_ci. Update your connection string too: jdbc:mysql://localhost:3306/myapp?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&connectionCollation=utf8mb4_hungarian_ci. JdbcTemplate probably works because it handles connections differently than Hibernate’s session management.

Check your Hibernate version - older ones had connection pooling bugs that ignored charset settings. That “ű” error is definitely a server-side encoding mismatch. Run show variables like 'char%'; in MySQL to see your server defaults. I bet they’re not utf8mb4.

This happens because JdbcTemplate and Hibernate handle connection setup differently. Hibernate often ignores your JDBC URL charset settings and falls back to whatever the server defaults are.

First, fix your MySQL server config. In /etc/mysql/mysql.conf.d/mysqld.cnf, make sure you’ve got:
character-set-server=utf8mb4
collation-server=utf8mb4_hungarian_ci

Then add spring.jpa.properties.hibernate.connection.characterEncoding=UTF-8 to force Hibernate to use UTF-8.

The many-to-many relationship makes this worse because lazy loading creates new database connections that grab the server defaults instead of your app’s encoding settings.

Restart MySQL after changing the config - this fixed the exact same Hungarian character issue I had in production.

The ISO 8859-1 exception means your Hibernate session factory isn’t using the connection URL encoding. I hit this same issue with Czech characters - Hibernate sometimes ignores JDBC driver encoding settings. Try adding these to your properties: spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect and spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true. More importantly, turn on spring.jpa.show-sql=true temporarily to see what SQL Hibernate’s actually generating. You’ll probably notice it’s not using the same connection parameters as your JdbcTemplate queries. That many-to-many fetch might be creating new connections that grab the default server encoding instead of your UTF-8 setting. Add &sessionVariables=character_set_connection=utf8mb4,character_set_results=utf8mb4,character_set_client=utf8mb4 to your connection URL as backup.

That ISO 8859-1 error means hibernate’s ignoring your URL encoding params. Add spring.jpa.properties.hibernate.connection.CharSet=utf8 and spring.datasource.hikari.connection-init-sql=SET NAMES utf8mb4 COLLATE utf8mb4_hungarian_ci to force session encoding. Also check your MySQL server variables - make sure utf8mb4 is the default, not just your database collation.

The CharConversionException related to ISO 8859-1 suggests that your database connection is defaulting to Latin-1 encoding incorrectly. It seems you’re experiencing issues with Hungarian characters due to MySQL’s utf8 charset, which does not fully accommodate all characters. I recommend switching your database charset to utf8mb4, as it supports a broader range of characters. Additionally, please modify your connection URL to include: jdbc:mysql://localhost:3306/myapp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Europe/Budapest&connectionCollation=utf8mb4_hungarian_ci. Don’t forget to add these properties in your application.properties: spring.jpa.properties.hibernate.connection.characterEncoding=utf-8 and spring.jpa.properties.hibernate.connection.useUnicode=true. The reason JdbcTemplate works could be due to differing configurations with the connection pool. Lastly, ensure your MySQL server’s default charset is set to utf8mb4.