gpt4 book ai didi

java - Hibernate - native SQL 参数的奇怪顺序

转载 作者:搜寻专家 更新时间:2023-11-01 02:54:23 24 4
gpt4 key购买 nike

我正在尝试使用 native 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 - native SQL 参数的奇怪顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4619436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com