我正在尝试对父实体执行合并操作,它应该保留子实体,以及我使用休眠级联Save_Update。
这是映射
表名数据:-
@OneToMany
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Column(name="DATA_ID")
private List<Details> details;
表名详细信息:-
@ManyToOne
@JoinColumn(name="DATA_ID")
private Data data;
对数据的合并操作
Session.merge(Data)
请让我知道我要去哪里
答案 0 :(得分:0)
此代码可将BilledProduct对象持久存储在Bill对象中:
@OneToMany (mappedBy="bill", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@HiddenField
@Getter
@Setter
@EqualsAndHashCode.Exclude
private Set<BilledProduct> items;
Customer customer = customerService.findByCustomerNumber("000001");
Bill bill = new Bill(customer, sequenceService.findNextBillNumberForCustomer(customer));
HashSet items = new HashSet();
Product product = productService.findByProductNumber("000001");
BilledProduct item = new BilledProduct(bill, 2, product, product.getSellPrice());
items.add(item);
product = productService.findByProductNumber("000002");
item = new BilledProduct(bill, 4, product, product.getSellPrice());
items.add(item);
bill.setItems(items);
billService.save(bill);
我可以更新Bill实体,没问题。尝试CascadeType.ALL和orphanRemoval = true,也许与它有关。