【问题标题】:How to post ManyToOne entity using JSON format如何使用 JSON 格式发布 ManyToOne 实体
【发布时间】:2019-12-05 18:13:50
【问题描述】:

我有两个实体issueuser 我想将用户分配给一个问题,但是当尝试使用此JSON 执行此操作时,issues 表中的user_idNULL .

@Data
@Entity
@Table(name = "issues")
public class Issue {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String number;
    @Column
    private String title;
    @Column
    private String description;
    @Column
    @Enumerated(EnumType.STRING)
    private State state;
    @JsonIgnore
    @CreationTimestamp
    @Column
    private Timestamp createDate;
    @JsonIgnore
    @UpdateTimestamp
    @Column
    private Timestamp modifyDate;
    @ManyToOne(targetEntity = User.class)
    @JoinColumn
    private User user;

    public Issue() {

    }
@Data
@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String fullName;
    @Column
    private String username;
    @Column
    private String email;
    @Column
    private String password;
    @Column
    @Enumerated(EnumType.STRING)
    private Role role;
    @JsonIgnore
    @CreationTimestamp
    @Column
    private Timestamp createDate;
    @JsonIgnore
    @UpdateTimestamp
    @Column
    private Timestamp modifyDate;

    public User() {

    }

    public User(String fullName, String username, String email, String password, Role role) {
        this.fullName = fullName;
        this.username = username;
        this.email = email;
        this.password = password;
        this.role = role;
    }

首先我创建了一个没有任何问题的用户,但不知道如何解决这个问题,这里是我通过邮递员使用的 JSON。

{
    "number": "3",
    "title": "Create an working service",
    "description": "The problem is that we do not have an working service.",
    "state": "NEW",
    "user_id": "1"
}

这部分负责保存问题。

    public void save(Issue issue) {
        if (issue == null)
            return;
        Issue actual = issueRepository.findByNumber(issue.getNumber());
        if (actual != null) {
            actual.setNumber(issue.getNumber());
            actual.setTitle(issue.getTitle());
            actual.setDescription(issue.getDescription());
            actual.setState(issue.getState());
            actual.setUser(issue.getUser());
            issueRepository.save(actual);
        } else {
            issueRepository.save(issue);
        }
    }

在控制器中,我只有 @Valid @RequestBody 问题,服务保存了问题。

【问题讨论】:

  • 你要把它寄给什么?接收此 JSON 的代码如何解析它?一旦解析,它会做什么?所有这些代码都很重要。

标签: json spring-boot jpa


【解决方案1】:

您以 JSON 格式发送的不是Issue

首先,因为它不代表数据库持久性问题(这就是 Issue 类的用途),而是公共 API 期望其客户端提供的 JSON 结构。

其次,因为 Issue 没有任何名为 user_id 的字段,但您的 JSON 有。因此,请使用与Issue 不同的 类,它实际上与API 期望的JSON 结构匹配,因此具有user_id 属性。

然后使用user_id 使用UserRepository 通过其ID 查找User,并将User 设置为您正在创建的Issue

我也会将 user_id 重命名为 userId 以尊重 Java 约定。

【讨论】:

  • 如果不是问题,你能给我一个简单的例子吗?
  • 如果我的解释中有什么不清楚的地方,请告知并要求澄清。如果一切都可以理解,那么至少花几分钟时间自己尝试一下。这就是学习的方式。
  • 但是你有什么解决办法吗?除了你上面提供的“简单的非标准”解决方案?
【解决方案2】:

您的 Item 类中未提供 user_id 键。 根据实体映射, 请将您的 JSON 更改为此。

{
"number": "3",
 "title": "Create an working service", "description": "The problem is that we do not have an working service.", 
"state": "NEW",
"user":{
"fullName":"USER",
"email":"user@gmail.com",
"username":"user"
}
}

【讨论】:

  • 但这会插入一个新用户!他想将新问题链接到现有用户。
  • @Mehdi 他想链接到现有用户,然后他需要在他的用户 JSON 中添加 id 属性,即主键。
【解决方案3】:

你需要像这样发布json。

{
    "number": "3",
    "title": "Create an working service",
    "description": "The problem is that we do not have an working service.",
    "state": "NEW",
    "user":{
         "id":1
     }
}

这里的问题有用户的加入列,所以对于用户实体,你必须传递用户 ID。 你的存档应该是这样的。

 public void save(Issue issue) {
        if (issue == null)
            return;
        Issue actual = issueRepository.findByNumber(issue.getNumber());
        if (actual != null) {
            actual.setNumber(issue.getNumber());
            actual.setTitle(issue.getTitle());
            actual.setDescription(issue.getDescription());
            actual.setState(issue.getState());
            actual.setUser(userRepositoy.findById(issue.getUser().getId()));
            issueRepository.save(actual);
        } else {
            issueRepository.save(issue);
        }
    }

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 1970-01-01
    • 2019-10-01
    • 2018-10-26
    • 1970-01-01
    • 2018-03-11
    • 2017-01-31
    • 1970-01-01
    相关资源
    最近更新 更多