gpt4 book ai didi

java - 删除级联不起作用,已经尝试了我找到的所有解决方案

转载 作者:行者123 更新时间:2023-11-30 05:52:24 30 4
gpt4 key购买 nike

Spring Rest API 应用程序。因此,当我删除一个用户时(我还想删除该用户的订单)。用户 ID 是订单的外键(一对多关系)。

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id")
public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

@OneToMany(
mappedBy = "order_products",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<OrderHasProduct> orders = new ArrayList<>();

用户类别

@OneToMany(
mappedBy = "orders",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<OrderHasProduct> orders = new ArrayList<>();

public boolean deleteUser(int id){
User usr = usrRepository.findById(id);
if (usr == null) {
throw new ResourceNotFoundException(User.class.getSimpleName());
}

usrRepository.delete(id);
User deletedUser = usrRepository.findById(id);
if (deletedUser != null)
return false;

return true;
}

最佳答案

您能否也显示 OrderHasProduct 类?这是一个工作示例:

@Entity
public class Post {

@Id
@GeneratedValue
private Long id;

private String title;

@OneToMany(
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<PostComment> comments = new ArrayList<>();

//Constructors, getters and setters
}

@Entity
public class PostComment {

@Id
@GeneratedValue
private Long id;

private String review;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

//Constructors, getters and setters

}
}

关于java - 删除级联不起作用,已经尝试了我找到的所有解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53675495/

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