数据截断:数据过长列
Data truncation: Data to long for column
我正在尝试将加密的密码插入用户配置文件 Table。
我的 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 + "]";
}
}
消息错误:
Data truncation: Data too long for column 'enPassword' at row 1; SQL [n/a]; nested exception is org.hibernate.exception.DataException: Data truncation: Data too long for column 'enPassword' at row 1
如何解决这个问题?
(方法doEncryptedPassword中字符串returns的长度为129个字符)
当为列 enPassword
传递的值大于 150 个字符时,将发生此异常。尝试增加 enPassword
列大小
同时检查方法 doEncryptedPassword
中字符串 returns 的长度是多少
我发现了问题。
我有两个数据库 Data 和 DataTest。在 'Data' 中一切正常。
在'DataTest'(执行测试的数据库)中有这个'UserProfile'之前有多个外键(为什么不删除的原因)。
旧的table有enPassword列定义像
enPassword VARCHAR(30) NOT NULL
这是错误的原因(129个字符的字符串无法存储到30个大小的列中)。
要解决删除带有外键的 table 的问题,请执行此操作
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS UserProfile;
SET FOREIGN_KEY_CHECKS=1;
我正在尝试将加密的密码插入用户配置文件 Table。
我的 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 + "]";
}
}
消息错误:
Data truncation: Data too long for column 'enPassword' at row 1; SQL [n/a]; nested exception is org.hibernate.exception.DataException: Data truncation: Data too long for column 'enPassword' at row 1
如何解决这个问题?
(方法doEncryptedPassword中字符串returns的长度为129个字符)
当为列 enPassword
传递的值大于 150 个字符时,将发生此异常。尝试增加 enPassword
列大小
同时检查方法 doEncryptedPassword
我发现了问题。
我有两个数据库 Data 和 DataTest。在 'Data' 中一切正常。
在'DataTest'(执行测试的数据库)中有这个'UserProfile'之前有多个外键(为什么不删除的原因)。
旧的table有enPassword列定义像
enPassword VARCHAR(30) NOT NULL
这是错误的原因(129个字符的字符串无法存储到30个大小的列中)。
要解决删除带有外键的 table 的问题,请执行此操作
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS UserProfile;
SET FOREIGN_KEY_CHECKS=1;