UTF - 8 with JPA and Glassfish 4.0

I'm having difficulty with UTF-8 characters. This is a simple JSF project. I am using JSF 2.2 and Glassfish 4.0

I have a method by which I go:

em.persist(user); 

When i debug

 user.getName() 

at this point, I see utf-8 characters in my IDE. I also save the string in the bean session, and I also see them in the browser.

Only when they are saved in the database, they are saved as: ?????

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

The problem is somewhere in the JPA.

This is what I 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 use a container managed EntityManager (transaction type = JTA)

So, in the 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 this helps.

Symbols are saved correctly.

+7
java mysql hibernate jpa glassfish
source share
2 answers

This resolved this:

JDBC: MySQL: // local: 3306 / FUS useUnicode = true & characterEncoding = UTF-8 & characterSetResults = UTF-8

so this was wrong:

JDBC: MySQL: // local: 3306 / FUS useUnicode = true & connectionCollation = utf8_general_ci & characterSetResults = utf8

+7
source share

When using Glassfish, you can set these properties in addition to your JDBC connection pools. Locate and view the database connection in web administration (Resources-> JDBC Connection Pools-> your.connection). On the additional properties tab, add (if not already) the mentioned properties and restart the server:

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

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

+10
source share

All Articles