I use JPA
, JDBC
and MySQL
for my Spring Boot project. I have some trouble with special characters.
If I write an SQL query via JdbcTemplate
, it's just fine. I can get back "á" characters.
But if it's via JPA
, those characters are �-s.
The schema's default collation is utf8_hungarian_ci, and the default characterset is utf8.
My application.properties
:
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: update
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/databasename?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username: root
spring.datasource.password: password
I have two tables, user
and userroles
, and they are connected with @ManyToMany
relations with a jointable.
User.java
:
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "user_userrole",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "userrole_id", referencedColumnName = "id"))
private List<UserRole> userRoles;
public List<UserRole> getUserRoles() {
return userRoles;
}
UserRole.java
:
@ManyToMany(mappedBy = "userRoles")
private List<User> users;
When I call this method:
currentUser.getUserRoles();
all "á" characters are replaced by �-s. What am I missing?
EDIT:
I added a new user: árvíztűrő tükörfúrógép to the database. (I could do it without any problem, and I save user with jpa too, by the way.) But when I tried to log in, I've got this exception:
error":"Internal Server Error","exception":"java.io.CharConversionException"
,"message":"Not an ISO 8859-1 character: ű"
pom.xml
:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>