我有一个 LoginCredential
类:
public class LoginCredential {
@Id @GeneratedValue Long userID;
String eMail;
String passwordHash;
@OneToOne(mappedBy = "loginCredential", fetch = FetchType.LAZY)
User user;
LoginCredential(){}
public LoginCredential(String eMail, String passwordHash, User user)
{
this.eMail = eMail;
this.passwordHash = passwordHash;
this.user = user;
}
}
然后我有User
类:
public class User {
@Id @GeneratedValue Long userID;
@OneToOne(fetch = FetchType.LAZY,targetEntity = LoginCredential.class)
@JoinColumn(name = "userID",referencedColumnName = "userID")
@JsonIgnore
private LoginCredential loginCredential;
public User(){}
public User(String eMail)
{
this.eMail=eMail;
}
public User(String eMail, LoginCredential loginCredential) {
this.eMail = eMail;
this.loginCredential = loginCredential;
}
}
在我的系统中,首先创建一个 LoginCredential
实例。
但对我来说,问题是在创建该实例后,user
字段仍为 null
。
如何解决这个问题?
由于它们是一对一映射的,还有其他方法吗?
我已经尝试过:{"email":"test@gmail.com","passwordHash":"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3","user":{"userID":801,"email":"test@ gmail.com"}}
我在前端的 react.js
应用程序中使用的方法:
createUserAndCredential = () => {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:8080/login/');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({
email: this.state.email,
passwordHash: this.state.passwordHash
}));
let that = this;
xhr.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
console.log(this.responseText);
/*
console.log(this.responseText);
console.log(that.state.newLoginCredential);
*/
that.state.newLoginCredential = this.responseText.toString();
var xhr1 = new XMLHttpRequest();
xhr1.open('POST', 'http://localhost:8080/user/');
xhr1.setRequestHeader("Content-Type", "application/json");
console.log(that.state.newLoginCredential);
let user = JSON.stringify({
email: that.state.email,
loginCredential: JSON.parse(that.state.newLoginCredential)
});
//console.log(loginCredential);
xhr1.send(user);
} else if (this.readyState !== 4 && this.status !== 200) {
console.log(this.status);
}
};
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
这给了我这个警告:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of
com.mua.cse616.Model.LoginCredential
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"email":"t7@gmail.com","passwordHash":"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofcom.mua.cse616.Model.LoginCredential
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"email":"t7@gmail.com","passwordHash":"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"}') at [Source: (PushbackInputStream); line: 1, column: 43] (through reference chain: com.mua.cse616.Model.User["loginCredential"])]
最佳答案
由于映射位于 User
一侧(mappedBy
驻留在此处),因此您必须保存它才能看到填充到 LoginCredential
中的更改>。但是您可以定义父级中的更改以级联到子级:
@OneToOne(mappedBy = "loginCredential", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
或者,如果您不想级联所有内容(例如删除):
@OneToOne(mappedBy = "loginCredential", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
关于javascript - 即使在发布后,Spring Boot 字段也始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58067607/