I'm using spring boot and hibernate, trying to save an entity with a @ManyToOne relation by posting only the id of the referenced entity
@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;
...
}
And the controller code is similar to:
@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);
}
}
And the posted JSON is similar to
{
"bar" : 1
}
However I'm getting an error in jackson while deserializing
"Could not read document: Unresolved forward references for: Object id [1]"