我想發送以下JSON到REST API並堅持數據庫,但只有Product
創建,Image
它不是。保存OneToMany從JSON到數據庫的對象
{ 「名稱」: 「筆」, 「描述」: 「紅色筆」, 「圖像」:[{ 「類型」: 「JPEG」}] }
@控制器
@POST
@Path("/product/add")
@Consumes("application/json")
public Response addProduct(Product product) {
service.createProduct(product);
}
@Service
@Autowired
private ProductDAO productDAO;
@Autowired
private ImageDAO imageDAO;
public void createProduct(Product product) {
productDAO.save(product);
}
@product
@Entity
@Table
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer productId;
@Column(name = "NAME")
private String name;
@Column(name = "DESCRIPTION")
private String description;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="product")
private Set<Image> images;
@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="parent")
private Set<Product> children;
@ManyToOne
@JoinColumn(name = "PARENT_PRODUCT_ID")
private Product parent;
@Image
@Entity
@Table
public class Image implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer imageId;
@Column(name = "TYPE")
private String type;
@ManyToOne
@JoinColumn(name = "PRODUCT_ID", nullable = false)
private Product product;
在@POST
方法中,當打印接收的Product
對象,這是返回:
產品的[ProductID = NULL,名字=筆,描述=紅筆,圖像= [圖像[id = null,type = jpeg,product = null]],children = null,parent = null]
正確的方法是先堅持Product
,然後堅持Image
或Hibernate可以自動堅持Image
當我堅持Product
?