title: Hibernate doesn't encode UTF-8 when persisting objects to MySQL database
body: <p>I'm creating a Spring MVC web application. Objects persisted by Hibernate to MySQL database are encoded wrongly. The data is coming from a .jsp page form, and is passed to a controller method using POST method. </p>
  
  <p>I already tried applying solutions proposed
  <a href="https://stackoverflow.com/questions/13063372/hibernate-mysql-how-to-set-the-encoding-utf-8-for-database-and-tables">here</a>
  and <a href="https://stackoverflow.com/questions/11758094/hibernate-encodes-wrong-while-persisting-objects-utf-8">here</a>, but they don't seem to work. </p>
  
  <p>This is the header I'm using on my JSP pages:</p>
  
  <pre><code>&lt;%@page language="Java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %&gt;
  </code></pre>
  
  <p>This is the database configuration file I'm using in my application:</p>
  
  <pre><code>jdbc.driverClassName=com.mysql.jdbc.Driver
  jdbc.url=jdbc:mysql://127.0.0.1:3306/database?useUnicode=yes&amp;amp;characterEncoding=UTF-8
  jdbc.username=root
  jdbc.password=password
  
  hibernate.dialect=org.hibernate.dialect.MySQLDialect
  hibernate.hbm2ddl.auto=update
  hibernate.show_sql=false
  hibernate.format_sql=true
  </code></pre>
  
  <p>I also added a UTF-8 filter in my web.xml file:</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;SetCharacterEncodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;encoding&lt;/param-name&gt;
          &lt;param-value&gt;UTF8&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;SetCharacterEncodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>I also tried to change collation of my database by running a command in MySQL Workbench (tried utf8, utf8_unicode_ci, utf8_polish_ci). I tried to change collation of only one table (the one containing UTF-8 string), too. </p>
  
  <p>None of this works. UTF-8 characters are stored as "?". I know the error is occuring while persisting the object, because I print the object in my console before persisting, and UTF-8 is displayed correctly. So I assume the problem lies in the Hibernate configuration and/or MySQL database configuration. </p>
  
  <p>I will provide additional configuration files, should that be necessary. </p>
  
  <p>Here's the CREATE statement for the table involved:</p>
  
  <pre><code>CREATE TABLE `table` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `string` varchar(255) CHARACTER SET utf8 COLLATE utf8_polish_ci NOT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
  </code></pre>