Spring - 发布JSON对象时链接到父级丢失

时间:2015-12-27 22:14:19

标签: java json spring

在我的Angular + Spring应用程序中,我有一个实体Logging,它有一个父Project

@Entity
public class Logging implements Identifiable<Integer> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String message;
    private Date date;
    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    private Project project;

这是我用来保存日志记录的端点:

@RequestMapping(value="/logging", method = RequestMethod.POST, consumes = "application/json")
@ResponseBody
public Logging saveLogging(@RequestBody Logging logging) {
    return apiLoggingService.save(logging);
}

我现在发布的JSON对象如下所示:

{
    id: null
    date: "2015-12-27T22:04:20.556Z"
    message: "test"
    project: {
        id: 16616,
       // ...
    }
}

但我也尝试过这个:

{
    id: null
    date: "2015-12-27T22:04:20.556Z"
    message: "test"
    project: 16616,
}

当对象保存到数据库时,如何确保填写project的外键?

由于

1 个答案:

答案 0 :(得分:1)

我通过将此添加到我的实体来解决了我的问题:

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")

由于循环引用,我还删除了之前添加的JsonIgnore