我正在尝试使用Hibernate映射实体产品,并自我引用其他产品。
为创建项目而发送的JSON是这样的:
{"name":"chair", "description":"red chair",
"parent": {"name":"table","description":"red table"}
}
当我收到这个json时,我需要在子产品DB上保留,并使用父属性的productId设置PARENT_PRODUCT_ID。
请帮忙吗?
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)
@JoinColumn(name="PRODUCT_ID")
private List<Image> images;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="PRODUCT_ID")
private List<Product> children;
@ManyToOne
@JoinColumn(name = "PARENT_PRODUCT_ID")
private Product parent;
Image.java:
@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;
答案 0 :(得分:0)
在oneToMany关系中,我认为它应该是:
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent")
private List<Product> children;