java JPA EntityManager 和字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27636242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA EntityManager and character encoding
提问by Neeeko
For the first time, I'm creating a simple website with JSP, Servlets, a Tomcat server, MySQL and Netbeans (if that helps).
我第一次用 JSP、Servlets、Tomcat 服务器、MySQL 和 Netbeans(如果有帮助的话)创建一个简单的网站。
Thanks to JPA EntityManager, I can insert data into my database like this :
感谢 JPA EntityManager,我可以像这样将数据插入到我的数据库中:
EntityManager entityManager = Persistence.createEntityManagerFactory("test").createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(usr);
entityManager.getTransaction().commit();
Where usr is an Entity Class Usergenerated thanks to Netbeans. Everything works perfectly except the fact that it doesn't support characters such as accents.
其中 usr 是由于 Netbeans 生成的实体类用户。除了不支持重音等字符外,一切都完美无缺。
Assuming I want to insert the word "cliché" :
假设我想插入“陈词滥调”这个词:
- If I try to print the value from my Userclass, everything is fine.
- When the value is sent, if I try to SELECT the value from my base, I've got strange characters instead of the "é"
- 如果我尝试从我的User类打印值,一切都很好。
- 当值被发送时,如果我尝试从我的基础中选择值,我会得到奇怪的字符而不是“é”
Database fields are utf8_unicode_ci. If I try to make a simple INSERT from MySQL prompt such as
数据库字段是utf8_unicode_ci。如果我尝试从 MySQL 提示进行简单的 INSERT,例如
INSERT INTO table (name) VALUES("ééé");
it's working perfectly.
它工作得很好。
As I read on some websites, I modified my persistence.xmlwith the following line :
当我在一些网站上阅读时,我使用以下行修改了我的persistence.xml:
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull"/>
but the result is the same.
但结果是一样的。
EDIT : Here are the properties of my User Class
编辑:这是我的用户类的属性
@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
@NamedQuery(name = "Users.findByIdUsers", query = "SELECT u FROM Users u WHERE u.idUsers = :idUsers"),
@NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
@NamedQuery(name = "Users.findByPasswd", query = "SELECT u FROM Users u WHERE u.passwd = :passwd"),
@NamedQuery(name = "Users.findByFirstName", query = "SELECT u FROM Users u WHERE u.firstName = :firstName"),
@NamedQuery(name = "Users.findByLastName", query = "SELECT u FROM Users u WHERE u.lastName = :lastName"),
@NamedQuery(name = "Users.findByEmail", query = "SELECT u FROM Users u WHERE u.email = :email"),
@NamedQuery(name = "Users.findByEmailAndPwd", query = "SELECT u FROM Users u WHERE u.email = :email AND u.passwd = :passwd")})
public class Users implements Serializable {
@Basic(optional = false)
@Column(name = "address")
private String address;
@Basic(optional = false)
@Column(name = "city")
private String city;
@Basic(optional = false)
@Column(name = "postal")
private String postal;
@Basic(optional = false)
@Column(name = "phone")
private String phone;
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idUsers")
private Integer idUsers;
@Basic(optional = false)
@Column(name = "username")
private String username;
@Basic(optional = false)
@Column(name = "passwd")
private String passwd;
@Basic(optional = false)
@Column(name = "firstName")
private String firstName;
@Basic(optional = false)
@Column(name = "lastName")
private String lastName;
@Basic(optional = false)
@Column(name = "email")
private String email;
As I read on the web, I also tried to modify my catalina.batby adding the following lines but it didn't change anything.
当我在网上阅读时,我还尝试通过添加以下几行来修改我的catalina.bat,但它没有改变任何内容。
set JAVA_OPTS=%JAVA_OPTS% -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8
设置 JAVA_OPTS=%JAVA_OPTS% -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8
Any help is much appreciated.
任何帮助深表感谢。
采纳答案by Neeeko
I was able to fix my problem thanks to VGR's answer here : HttpServletRequest UTF-8 Encoding
由于 VGR 的回答,我能够解决我的问题:HttpServletRequest UTF-8 编码
Thanks a lot for your help.
非常感谢你的帮助。
回答by Rafael Zeffa
try add this config in your persistence.xml
尝试在你的persistence.xml中添加这个配置
<property name="hibernate.connection.characterEncoding">utf8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.charSet">UTF-8</property>
回答by Babak Behzadi
You can add unicode encoding filter to web.xml:
您可以在 web.xml 中添加 unicode 编码过滤器:
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
if you use Spring you can use 'org.springframework.web.filter.CharacterEncodingFilter' class to encode all requests, else you should use other encoding class.
如果您使用 Spring,您可以使用 'org.springframework.web.filter.CharacterEncodingFilter' 类对所有请求进行编码,否则您应该使用其他编码类。