我试图将加密的密码插入UserProfile表。
我的sql文件用于创建表格,如:
-- ---------- Table for validation queries from the connection pool. ----------
DROP TABLE PingTable;
CREATE TABLE PingTable (foo CHAR(1));
-- ------------------------------ UserProfile ----------------------------------
DROP TABLE UserProfile;
CREATE TABLE UserProfile (
usrId BIGINT NOT NULL AUTO_INCREMENT,
loginName VARCHAR(30) COLLATE latin1_bin NOT NULL,
enPassword VARCHAR(150) NOT NULL,
firstName VARCHAR(30) NOT NULL,
lastName VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
CONSTRAINT UserProfile_PK PRIMARY KEY (usrId),
CONSTRAINT LoginNameUniqueKey UNIQUE (loginName))
ENGINE = InnoDB;
CREATE INDEX UserProfileIndexByLoginName ON UserProfile (loginName);
我注册用户的实现:
private String doEncryptedPassword(String clearPassword) {
HashFunction hf = Hashing.sha512();
HashCode hc = hf.newHasher(clearPassword.length()).putString(clearPassword, StandardCharsets.UTF_8).hash();
return hc.toString();
}
public UserProfile registerUser(String loginName, String clearPassword,
UserProfileDetails userProfileDetails)
throws DuplicateInstanceException {
try {
userProfileDao.findByLoginName(loginName);
throw new DuplicateInstanceException(loginName,
UserProfile.class.getName());
} catch (InstanceNotFoundException e) {
String encryptedPassword = doEncryptedPassword(clearPassword);
UserProfile userProfile = new UserProfile(loginName,
encryptedPassword, userProfileDetails.getFirstName(),
userProfileDetails.getLastName(), userProfileDetails
.getEmail());
userProfileDao.save(userProfile);
return userProfile;
}
}
UserProfile.java:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
public class UserProfile {
private Long userProfileId;
private String loginName;
private String encryptedPassword;
private String firstName;
private String lastName;
private String email;
public UserProfile() {
}
public UserProfile(String loginName, String encryptedPassword,
String firstName, String lastName, String email) {
/**
* NOTE: "userProfileId" *must* be left as "null" since its value is
* automatically generated.
*/
this.loginName = loginName;
this.encryptedPassword = encryptedPassword;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
@Column(name = "usrId")
@SequenceGenerator( // It only takes effect for
name = "UserProfileIdGenerator", // databases providing identifier
sequenceName = "UserProfileSeq")
// generators.
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "UserProfileIdGenerator")
public Long getUserProfileId() {
return userProfileId;
}
public void setUserProfileId(Long userProfileId) {
this.userProfileId = userProfileId;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
@Column(name = "enPassword")
public String getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "UserProfile [userProfileId=" + userProfileId + ", loginName="
+ loginName + ", encryptedPassword=" + encryptedPassword
+ ", firstName=" + firstName + ", lastName=" + lastName
+ ", email=" + email + "]";
}
}
邮件错误:
数据截断:对于列' enPassword'在第1行; SQL [不适用];嵌套异常是org.hibernate.exception.DataException:数据截断:对于列' enPassword'在第1行
如何解决这个问题?
(方法 doEncryptedPassword 中字符串返回的长度为129个字符)
答案 0 :(得分:1)
当列enPassword
传递的值大于150个字符时,将发生此异常。尝试增加enPassword
列大小
同时检查方法doEncryptedPassword
答案 1 :(得分:0)
我发现了问题。
我有两个数据库Data和DataTest。在'数据'一切正常。
In' DataTest' (执行测试的数据库)有这个' UserProfile'之前有多个外键(之所以不删除)。
旧表的 enPassword列定义为
enPassword VARCHAR(30) NOT NULL
出现错误的原因(129个字符的字符串无法存储到30个大小的列中。)
要解决使用外键删除表的问题,请执行此操作
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS UserProfile;
SET FOREIGN_KEY_CHECKS=1;