7

I am having difficulties with UTF-8 characters. This is a simple JSF project. I use JSF 2.2 and Glassfish 4.0

I have a method where I go:

em.persist(user);

When I debug

user.getName()

in this point, I can see the utf-8 characters in my IDE. Also I keep the string in a session - bean and I can see them on the browser fine as well.

Only when they are persisted to DB, they are persisted as: ?????

I can also edit the DB myself and save utf-8 characters. What I mean is, my SQL configuration is good for UTF-8.

The problem is somewhere in JPA.

This is what I have tried: ( all together: )

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="Persistence" transaction-type="JTA">
        <jta-data-source>fus</jta-data-source>
        <class>com.tugay.fup.core.model.User</class>
        <properties>
            <property name="javax.persistence.jdbc.url"
                  value="jdbc:mysql://localhost:3306/fus?useUnicode=yes&amp;characterEncoding=UTF-8"/>
        </properties>
    </persistence-unit>
</persistence>

This is glassfish-web.xml:

<glassfish-web-app>
    <parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>

And I am using EntityManager managed by container (transaction type = JTA)

So in JDBC connection pools in Glassfish I have:

jdbc:mysql://localhost:3306/fus?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8

for: property: URL...

However none of these help.

Still characters not persisted correctly.

dima
  • 75
  • 1
  • 10
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

2 Answers2

12

When using Glassfish, you can set these properties as additional to your JDBC Connection Pools. Locate and view your database connection in Web Administration (Resources->JDBC Connection Pools->your.connection). In additional properties tab add (if there are not present yet) mentioned properties and restart your server:

//name, value
characterEncoding, UTF-8
characterSetResults, UTF-8
useUnicode, true

The result will be the same if parameters are added to URL, but this is more maintainable solution in my opinion.

klimpond
  • 756
  • 8
  • 15
7

This solved it:

jdbc:mysql://localhost:3306/fus?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8

so this was wrong:

jdbc:mysql://localhost:3306/fus?useUnicode=true&connectionCollation=utf8_general_ci&characterSetResults=utf8

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319