【发布时间】:2016-12-27 02:09:32
【问题描述】:
我正在使用 spring boot 和 hibernate,试图通过仅发布引用实体的 id 来保存具有 @ManyToOne 关系的实体
@Entity
@Table(name = "foo_table")
public class Foo implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private id;
@NotNull
@JsonIdentityReference(alwaysAsId=true)
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "bar_id", nullable = false)
private Bar bar;
...
}
@Entity
@Table(name = "bar_table")
public class Bar implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "bar")
private Set<Foo> foos;
...
}
和控制器代码类似:
@RestController
public class FooController {
@Autowired
private FooRepo fooRepo;
@RequestMapping(value = "/foo", method= RequestMethod.POST)
public Foo foo(@RequestBody @Valid Foo foo)
throws Exception {
return fooRepo.save(foo);
}
}
并且发布的 JSON 类似于
{
"bar" : 1
}
但是我在反序列化时在 jackson 中遇到错误
"Could not read document: Unresolved forward references for: Object id [1]"
【问题讨论】:
标签: java hibernate spring-boot jackson