【问题标题】:spring boot field always null even after post-ing即使在发布之后,弹簧引导字段也始终为空
【发布时间】: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


    【解决方案1】:

    由于映射位于 User 一侧(mappedBy 驻留在那里),您必须保存它才能看到填充到 LoginCredential 的更改。但是您可以将父级中的更改定义为级联到子级:

    @OneToOne(mappedBy = "loginCredential", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    

    或者,如果您不想级联所有内容(例如删除):

    @OneToOne(mappedBy = "loginCredential", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    

    【讨论】:

    • user 字段仍然是 null。这里:{"userID":993,"passwordHash":"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3","user":null, "email":"t7@gmail.com"}]
    • 我已经编辑了这个问题并添加了我用来创建这个实例的代码,你能看看吗。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2019-10-12
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多