Java 列';加密密码';不能为空?
我正在使用Java 列';加密密码';不能为空?,java,sql,spring-mvc,spring-data-jpa,Java,Sql,Spring Mvc,Spring Data Jpa,我正在使用Java和springdatajpa开发一个web服务。我设置了不同的端点,效果很好。现在我在post方法中实现了逻辑,并返回一个我无法解决的错误 我使用postman,当我尝试使用id、用户名、姓氏和密码发出post请求时,我收到一个错误500,服务器返回我这个错误: java.sql.SQLIntegrityConstraintViolationException:列“加密的\u密码”不能为null 您声明属性encryptedPassword不能为空。异常意味着,当您试图将该属性
Java
和springdatajpa
开发一个web服务。我设置了不同的端点,效果很好。现在我在post方法中实现了逻辑,并返回一个我无法解决的错误
我使用postman
,当我尝试使用id、用户名、姓氏和密码发出post请求时,我收到一个错误500,服务器返回我这个错误:
java.sql.SQLIntegrityConstraintViolationException:列“加密的\u密码”不能为null
您声明属性
encryptedPassword
不能为空。异常意味着,当您试图将该属性保存到数据库时,该属性为null
。您可能忘记设置此属性。或者您设置了它(称为setter),但新值为null
。要解决此问题,请在保存到DB之前将此属性设置为非null
值。您声明属性encryptedPassword
不能为null。异常意味着,当您试图将该属性保存到数据库时,该属性为null
。您可能忘记设置此属性。或者您设置了它(称为setter),但新值为null
。要解决此问题,请在保存到DB之前将此属性设置为非null
值。您有@列(null=false)
用于私有字符串加密密码代码>并且您只传递id、用户名、姓氏和密码。这会破坏条件nullable=false
,并导致java.sql.SQLIntegrityConstraintViolationException:Column'encrypted_password'不能为null
解决方案
设置@列(nullable=true)
或
将加密密码的值传递给私有字符串加密密码的@列(nullable=false)
代码>并且您只传递id、用户名、姓氏和密码。这会破坏条件nullable=false
,并导致java.sql.SQLIntegrityConstraintViolationException:Column'encrypted_password'不能为null
解决方案
设置@列(nullable=true)
或
传递encryptedPassword
的值引发异常,因为您将列encryptedPassword
设置为非null
(@column(…,null=false)
)。根本原因很可能是因为未正确设置is。如果看不到端点、构建要创建的实体的逻辑和POST请求,则很难进一步诊断问题。引发异常是因为您将列encryptedPassword
设置为非null
(@column(…,nullable=false)
)。根本原因很可能是因为未正确设置is。如果看不到端点、构建要创建的实体的逻辑和POST请求,就很难进一步诊断问题。
package com.nicolacannata.Appws.entity;
import javax.persistence.*;
import java.io.Serializable;
@Entity(name="users")
public class UserEntity implements Serializable{
private static final long serialVersionUID = 8076405899207283205L;
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String userId;
@Column(nullable = false, length = 50)
private String firstName;
@Column(nullable = false, length = 50)
private String lastName;
@Column(nullable = false, length = 50)
private String email;
@Column(nullable = false)
private String encryptedPassword;
private String emailVerificationToken;
@Column(nullable = false)
private boolean emailVerificationStatus = false;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
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;
}
public String getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
public String getEmailVerificationToker() {
return emailVerificationToken;
}
public void setEmailVerificationToker(String emailVerificationToker) {
this.emailVerificationToken = emailVerificationToker;
}
public boolean isEmailVerificationStatus() {
return emailVerificationStatus;
}
public void setEmailVerificationStatus(boolean emailVerificationStatus) {
this.emailVerificationStatus = emailVerificationStatus;
}
}