gpt4 book ai didi

java - 为什么更新记录的子项会删除与其子项的连接?

转载 作者:行者123 更新时间:2023-11-30 11:20:27 25 4
gpt4 key购买 nike

我需要更新以下类的项目,虽然我可以更新所有项目,但该类的所有字段都将更改为 null,并且它与其成员的连接将被删除。

@Entity
public class Category implements Serializable {
@Id
@GeneratedValue
private long id;

@OneToMany( cascade = CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Items> items;

private float price;

@ManyToOne
private Staff user;

@ManyToOne
private Staff owner;

@Temporal(javax.persistence.TemporalType.DATE)
private Date createDate;
....

@Entity
public class Items {

@Id
@GeneratedValue
private Long id;

private int serial;

@OneToOne
private Product product;
.....

hibernate

            Category cat = (Category) session.get(Category.class, id);
for (int i = 0; i < cat.getItems().size(); i++) {
cat.getItems().get(i).getProduct().setQuantity(10);

Part part = new Part();
part.setName("Temp");
cat.getItems().get(i).getProduct().getPart.add(part);

session.update(cat.getItems().get(i).getProduct());
session.save(part);

}
tx.commit();

示例

更新前

Category 
1 2000 Alex Jack 05-05-2014

category_categoryitem

1 137

categoryitem

137 900 20

更新后

category
1 0 Null Null Null

category_categoryitem


categoryitem

137 900 20

最佳答案

您是否尝试过使用@JoinTable 链接ItemCategory

分类.java

...
@OneoMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(
name="category_items",
joinColumns={@JoinColumn(name="category", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="item", referencedColumnName="id")})
private Set<Items> items = new HashSet<Items>();
...

关于java - 为什么更新记录的子项会删除与其子项的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22702836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com