【发布时间】:2020-01-23 19:53:24
【问题描述】:
我有一个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>
这给了我这个警告:
已解决 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法构造
com.mua.cse616.Model.LoginCredential的实例(尽管至少存在一个 Creator):没有 String-argument 构造函数/工厂方法可从 String 反序列化值('{"email":"t7@gmail.com","passwordHash":"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"}');嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造com.mua.cse616.Model.LoginCredential的实例(尽管至少存在一个 Creator):没有字符串参数构造函数/工厂方法可以从字符串值反序列化('{"email" :"t7@gmail.com","passwordHash":"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3"}') 在 [来源:(PushbackInputStream);行:1,列:43](通过引用链:com.mua.cse616.Model.User["loginCredential"])]
【问题讨论】:
标签: javascript java spring spring-data-jpa hibernate-mapping