javascript - 即使在发布后,Spring Boot 字段也始终为空

标签 javascript java spring spring-data-jpa hibernate-mapping

我有一个 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 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"}') 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/

相关文章:

javascript - 从 Ember.js 中的嵌套对象计算属性

javascript - 为什么当我的一个 JS 文件以相同的方式提供时,Chrome 可能会返回不正确的 MIME 类型错误?

java - Cloud Firestore 单元测试 Java

java - 使用 spring petclinic 示例和 hsqldb 的 jpa 查询语法

java - 如何使用 spring mvc 将图像上传到 webapp/resources/images 目录?

.net - 用于在页面内创建 div 样式窗口的 JavaScript 库

javascript - JavaScript 中未使用函数参数的标准约定

java - Android ActionBar 发出不同的 API <> 21

java - 在服务类中使用 AsyncTask?

java - 使用aspectj进行springboot日志记录获取IllegalArgumentException:错误在::0在切入点中正式未绑定(bind)