JPA utf-8 字符未保留 [英] JPA utf-8 characters not persisted
问题描述
我有一个使用 JPA 的简单 Web 应用程序.
I have a simple web application where I use JPA.
我有一个名为 BlogEntry 的实体.
I have an entity called BlogEntry.
当我提交一个新的 BlogEntry 时,当我调试我的应用程序时,我看到 utf8 字符就好了.
When I submit a new BlogEntry, when I debug my application I see the utf8 characters just fine.
例如
em.persist(entity);
在这一行中,如果我调试例如:
In this line, if I debug for example:
entity.getTitle()
我可以在 IDE 中成功看到 utf-8 字符.(例如 ğ 或 ç )
I can successfuly see utf-8 characters in the IDE. ( like ğğ, or çç )
此外,我的数据库有 UTF8 排序规则,我可以使用 sql 插入 utf-8 字符,例如INSERT INTO..."
Also, my database has UTF8 collation and I can insert utf-8 characters just fine with sql using like "INSERT INTO..."
但是,使用 JPA,字符会保留为 ????
However, with JPA, the characters are persisted as ????
为什么会这样?
问候.
这是我的persistence.xml:
Here is my 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">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tugay.blog.core.model.Blogentry</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/blogdatabase"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="aabbccdd"/>
</properties>
</persistence-unit>
</persistence>
推荐答案
使用persistence.xml文件属性中的字符编码
use the character encoding in the property of persistence.xml file
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/blogdatabase?useUnicode=yes&characterEncoding=UTF-8"/>
这篇关于JPA utf-8 字符未保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!