作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
该项目中有实体帐户和服务(摘要)。服务有 child 类、押金。账户类别代码:
@Entity
public class Account {
private static Logger log = LogManager.getLogger(Account.class);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column
private double amount;
@Column
private AccountType type;
@Column(name = "date_start")
private Date dateStart;
@Column(name = "date_end")
private Date dateEnd;
@Column(name = "in_rate")
private short inRate;
@ManyToOne
@JoinColumn(name = "client_id")
private Client client;
...
服务类代码:
@MappedSuperclass
abstract public class Services {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long id;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "from_acc_id")
protected Account fromAcc;
...
存款另外还有一个金额字段,但这并不是那么重要:
@Entity
public class Deposit extends Services {
@Column
private double amount;
当尝试删除包含来自 Deposit 的链接的 Account 实例时,会出现错误:
2020-03-13 13:29:51 ERROR SqlExceptionHelper: 131 - ERROR: UPDATE or DELETE in the "account" table violates the foreign key constraint "fk8qcea1frw0og19kft1ltq9kf9" of the "deposit" table
Details: The key (id) = (1) still has links in the "deposit" table.
如何配置级联删除,以便在删除账户记录时,自动删除存款记录?
最佳答案
一般来说,注释 @OnDelete 有帮助。 Account 类没有改变。服务代码:
@MappedSuperclass
abstract public class Services {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long id;
@ManyToOne(cascade = CascadeType.REFRESH)
@JoinColumn(name = "from_account_id")
@OnDelete(action = OnDeleteAction.CASCADE)
protected Account fromAcc;
...
关于java - Hibernate级联删除依赖实体(ManyToOne OneToMany),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60670554/
我是一名优秀的程序员,十分优秀!