1

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>
Siriann
  • 405
  • 1
  • 6
  • 16

2 Answers2

0
<property name="hibernate.connection.useUnicode" value="true" />
<property name="hibernate.connection.characterEncoding" value="UTF-8" />

with hibernate 4.3.1 this works:

<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">utf-8</property>
Tanvi B
  • 1,577
  • 11
  • 14
  • I use application.properties, not xml-s. However, I added these lines like this: hibernate.connection.useUnicode=true hibernate.connection.characterEncoding=UTF-8 connection.useUnicode=true connection.characterEncoding=utf-8 But nothing happened. Also, they have "unknown prperty" warning. – Siriann May 22 '16 at 09:46
  • Can you try with connection url as below : jdbc:mysql://localhost:3306/mydb?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8&characterEncoding=utf8 – Tanvi B May 22 '16 at 10:03
  • It didn't help. But from the `java.io.CharConversionException` I guess it's not the jpa's or hibernate's fault, something isn't ok with java itself... – Siriann May 22 '16 at 10:15
  • It is not jpa or hibernate you can configure it at tomcat or maven level – Tanvi B May 22 '16 at 10:19
  • Along with connection url can you add below in web.xml for above error – Tanvi B May 22 '16 at 10:22
  • I use Maven. But I have `project.build.sourceEncoding` set to UTF-8. – Siriann May 22 '16 at 10:23
  • I don't have a web.xml in my project, I use Spring Boot. – Siriann May 22 '16 at 10:25
  • I think you need to alter your table ALTER TABLE @table CONVERT TO CHARACTER SET utf8 – Tanvi B May 22 '16 at 10:36
  • you can follow one good link http://javarticles.com/2016/01/spring-boot-jdbc-example.html – Tanvi B May 22 '16 at 10:37
0

Please add below in web xml

<filter>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncodingFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>
Tanvi B
  • 1,577
  • 11
  • 14
  • I don't use web.xml in my project since I use Spring Boot. However, I added this to Tomcat's web.xml, and also I took `org.apache.catalina.filters.SetCharacterEncodingFilter` section out of comment, but nothing happened. – Siriann May 22 '16 at 10:36
  • Here is Spring Boot doc on how to configure filters in the embedded Tomcat server -> http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-embedded-container – Sanjay May 22 '16 at 14:03