我有这2类:
作家
@Entity
public class Writer extends PanacheEntity {
public String name;
public Writer() {
}
}
书
@Entity
public class Book extends PanacheEntity {
public String name;
@OneToOne
public Writer writer;
public Book() {}
}
资源类为:
@Path("/books")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class BookResource {
@GET
public List<Book> list() {
return Writer.listAll();
}
@GET
@Path("/{id}")
public Book get(Long id) {
return Book.findById(id);
}
@POST
@Transactional
public Response create(Book book) {
book.persist();
return Response.created(URI.create("/book/" + book.id)).build();
}
}
到达@POST方法的数据是一个json,类似于:
{
"name":"bookName",
"writer_id":1
}
我如何使用writer_id保存它以正确引用writer?这样数据库就不能正确地创建它:
1条答案
按热度按时间zpf6vheq1#
可以将正确的json传递给@POST方法: