我试图使用原生MySQL的MD5加密函数,所以我在我的映射文件中定义了自定义插入.hibernate-mapping package=tutorialclass name=com.xorty.mailclient.client.domain.User table=userid name=login type=...
我试图使用原生MySQL的MD5加密函数,所以我在我的映射文件中定义了自定义插入.
<hibernate-mapping package="tutorial">
<class name="com.xorty.mailclient.client.domain.User" table="user">
<id name="login" type="string" column="login"></id>
<property name="password">
<column name="password" />
</property>
<sql-insert>INSERT INTO user (login,password) VALUES ( ?, MD5(?) )</sql-insert>
</class>
</hibernate-mapping>
然后我创建用户(非常简单的POJO只有2个字符串 – 登录名和密码)并尝试坚持它.
session.beginTransaction();
// we have no such user in here yet
User junitUser = (User) session.load(User.class, "junit_user");
assert (null == junitUser);
// insert new user
junitUser = new User();
junitUser.setLogin("junit_user");
junitUser.setPassword("junitpass");
session.save(junitUser);
session.getTransaction().commit();
究竟发生了什么?
用户已创建,但具有反向参数顺序.他登录“junitpass”,“junit_user”是MD5加密并存储为密码.
我错了什么?谢谢
编辑:添加POJO类
package com.xorty.mailclient.client.domain;
import java.io.Serializable;
/**
* POJO class representing user.
* @author MisoV
* @version 0.1
*/
public class User implements Serializable {
/**
* Generated UID
*/
private static final long serialVersionUID = -969127095912324468L;
private String login;
private String password;
/**
* @return login
*/
public String getLogin() {
return login;
}
/**
* @return password
*/
public String getPassword() {
return password;
}
/**
* @param login the login to set
*/
public void setLogin(String login) {
this.login = login;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @see java.lang.Object#toString()
* @return login
*/
@Override
public String toString() {
return login;
}
/**
* Creates new User.
* @param login User's login.
* @param password User's password.
*/
public User(String login, String password) {
setLogin(login);
setPassword(password);
}
/**
* Default constructor
*/
public User() {
}
/**
* @return hashCode
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((null == login) ? 0 : login.hashCode());
result = prime * result
+ ((null == password) ? 0 : password.hashCode());
return result;
}
/**
* @param obj Compared object
* @return True, if objects are same. Else false.
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof User)) {
return false;
}
User other = (User) obj;
if (login == null) {
if (other.login != null) {
return false;
}
} else if (!login.equals(other.login)) {
return false;
}
if (password == null) {
if (other.password != null) {
return false;
}
} else if (!password.equals(other.password)) {
return false;
}
return true;
}
}
解决方法:
从docs:
The parameter order is important and is defined by the order Hibernate handles properties. You can see the expected order by enabling debug logging for the
org.hibernate.persister.entity
level. With this level enabled Hibernate will print out the static SQL that is used to create, update, delete etc. entities. (To see the expected sequence, remember to not include your custom SQL through annotations or mapping files as that will override the Hibernate generated static sql)
听起来好像没有办法预测这个顺序.
本文标题为:java – Hibernate – 本机SQL参数的奇怪顺序
基础教程推荐
- Java中实现两个线程交替运行的方法 2023-08-10
- java操作json对象出现StackOverflow错误的问题及解决 2023-01-09
- java的时间类汇总(齐全) 2023-05-14
- 浅谈一下Spring中的createBean 2023-03-15
- 图解Java ReentrantLock公平锁和非公平锁的实现 2023-06-10
- JSP页面中超链接传递中文参数出现乱码问题解决方法 2023-08-03
- 从JAVA应用程序使用SSL连接到MongoDb 2023-11-06
- Java使用@EnableEurekaServer实现自动装配详解 2023-06-23
- Java实现简单的日历界面 2023-01-02
- 深入了解Java行为型设计模式之策略模式 2023-05-24