如您所见,我有两张桌子(优惠,项目) 我有插入项目的问题,每次添加提供它不插入项目列表。优惠包含物品清单。 这是我的代码:
OFFER:
@Entity
public class Offer{
@Id
@GeneratedValue
int id;
private String name;
private String description;
@OneToMany(mappedBy="offer")
private List<Item> items;
//getters and setters
ITEMS:
@Entity
public class Item{
@Id
@GeneratedValue
int idItem;
private String name;
private double price;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="idOffer")
private Offer offer;
// getters and setters
此处是填写表格的代码:
Offer offer=GenerateData.generateOffer();
List<Item> items=new ArrayList<>();
for(int i=0;i<3;i++){
Item item=GenerateData.generateItem();
items.add(item);
item.setOffer(offer);
}
offer.setItems(items);
pDB.addOffer(offer);//DAO for adding Offers into DB
已成功添加商品,但商品未成功添加。这是映射问题吗?
答案 0 :(得分:3)
您是否尝试在OneToMany端添加级联选项?
答案 1 :(得分:0)
优惠
@OneToMany(cascade = { CascadeType.ALL }, orphanRemoval = true, mappedBy = "offer")
private List<Item> items;
档案:
@ManyToOne
@JoinColumn(name="idOffer")
private Offer offer;