我有两个桌子
我试图按顺序在两个表中保存数据,但是数据被存储在第一个表中,并向我抛出如下错误:
{
"timestamp": "2020-06-15T11:17:06.540+0000",
"status": 500,
"error": "Internal Server Error",
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
"path": "/user/register"
}
实体类如下:
UserAccount
@Entityd
@Table(name = "tbl_user_info")
public class UserAccount {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
@Column(name = "mobile_number")
private String mobileNumber;
@Column(name = "password")
private String password;
@Column(name = "token")
private String token;
@Column(name = "admin")
private Integer admin;
@Column(name = "country_code")
private String countryCode;
@Column(name = "serial_number")
private String serialNumber;
protected UserAccount() {
}
public UserAccount(Integer id, String name, String email, String mobileNumber, String password, String token, Integer admin, String countryCode, String serialNumber) {
this.id = id;
this.name = name;
this.email = email;
this.mobileNumber = mobileNumber;
this.password = password;
this.token = token;
this.admin = admin;
this.countryCode = countryCode;
this.serialNumber = serialNumber;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public Integer getAdmin() {
return admin;
}
public void setAdmin(Integer admin) {
this.admin = admin;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserAccount that = (UserAccount) o;
return Objects.equals(id, that.id) &&
Objects.equals(name, that.name) &&
Objects.equals(email, that.email) &&
Objects.equals(mobileNumber, that.mobileNumber) &&
Objects.equals(password, that.password) &&
Objects.equals(token, that.token) &&
Objects.equals(admin, that.admin) &&
Objects.equals(countryCode, that.countryCode) &&
Objects.equals(serialNumber, that.serialNumber);
}
@Override
public int hashCode() {
return Objects.hash(id, name, email, mobileNumber, password, token, admin, countryCode);
}
@Override
public String toString() {
return "UserAccount{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", mobileNumber='" + mobileNumber + '\'' +
", password='" + password + '\'' +
", token='" + token + '\'' +
", admin=" + admin +
", countryCode='" + countryCode + '\'' +
", serialNumber='" + serialNumber + '\'' +
'}';
}
}
UserAuthInfo
@Entity
@Table(name = "tbl_user_auth")
public class UserAuthInfo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "client_id")
private String clientId;
@Column(name = "user_id")
private Integer userId;
@Column(name = "code")
private String code;
@Column(name = "access_token")
private String accessToken;
@Column(name = "refresh_token")
private String refreshToken;
@Column(name = "expires_in")
private Integer expiresIn;
@Column(name = "modified_datetime")
private String datetime;
public UserAuthInfo() {
}
public UserAuthInfo(Integer id, String clientId, Integer userId, String code, String accessToken, String refreshToken, Integer expiresIn, String datetime) {
this.id = id;
this.clientId = clientId;
this.userId = userId;
this.code = code;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.expiresIn = expiresIn;
this.datetime = datetime;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public Integer getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(Integer expiresIn) {
this.expiresIn = expiresIn;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserAuthInfo that = (UserAuthInfo) o;
return Objects.equals(id, that.id) &&
Objects.equals(clientId, that.clientId) &&
Objects.equals(userId, that.userId) &&
Objects.equals(code, that.code) &&
Objects.equals(accessToken, that.accessToken) &&
Objects.equals(refreshToken, that.refreshToken) &&
Objects.equals(expiresIn, that.expiresIn) &&
Objects.equals(datetime, that.datetime);
}
@Override
public int hashCode() {
return Objects.hash(id, clientId, userId, code, accessToken, refreshToken, expiresIn, datetime);
}
@Override
public String toString() {
return "UserAuthInfo{" +
"id=" + id +
", clientId='" + clientId + '\'' +
", userId=" + userId +
", code='" + code + '\'' +
", accessToken='" + accessToken + '\'' +
", refreshToken='" + refreshToken + '\'' +
", expiresIn=" + expiresIn +
", datetime=" + datetime +
'}';
}
}
控制器:
@PostMapping("register") // Needed parameter : All except id and token
public AuthResponse registerUser(@RequestBody UserAccount userAccount) throws NoSuchAlgorithmException {
if (userAccount == null) throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Request Body can not be null");
jsonResponse = new JSONResponse();
if (!userAccountService.isEmailPresent(userAccount.getEmail())){
userAccount.setPassword(DigestUtils.md5Hex(userAccount.getPassword()));
return userAccountService.registerUser(userAccount);
}
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"Email ID Already Exists");
}
服务:
public AuthResponse registerUser(UserAccount userAccount) throws NoSuchAlgorithmException {
UserAccount ua = userAccountRepository.save(userAccount);
String token = tokenService.createToken((ua.getId()));
//Saving the user id and newly created token in tbl_token_info
ua.setToken(token);
UserAccount ua1 = userAccountRepository.save(ua);
UserAuthInfo userAuthInfo = new UserAuthInfo();
userAuthInfo.setClientId(UtilityMethods.clientId);
userAuthInfo.setUserId(ua.getId());
String code = UtilityMethods.getCode(userAccount.getEmail(), userAccount.getPassword());
userAuthInfo.setCode(code);
String accessToken = UtilityMethods.getAccessToken(code);
String refreshToken = UtilityMethods.getRefreshToken(code);
userAuthInfo.setAccessToken(accessToken);
userAuthInfo.setRefreshToken(refreshToken);
userAuthInfo.setExpiresIn(3600);
userAuthInfo = userAuthService.saveUserAuth(userAuthInfo);
AuthResponse authResponse = new AuthResponse();
authResponse.setAccessToken(userAuthInfo.getAccessToken());
authResponse.setRefreshToken(userAuthInfo.getRefreshToken());
authResponse.setExpiresIn(60);
authResponse.setCreatedDateTime(UtilityMethods.getCurrentDateTime());
return authResponse;
}
请指导出什么问题了。
答案 0 :(得分:0)
您必须使用表之间的关系:
@OneToMany用于表中的1-N组成关系。 @嵌入1-1关系
@ManyToOne用于表中的N-1关联关系。
不要使用双向关系。总是单向的。
如果不使用它,则数据库的映射可能是错误的。