Answer the question
In order to leave comments, you need to log in
How to implement cascading deletion of dependent entities (ManyToOne, OneToMany) in Hibernate?
There are entities Account and Services (abstract) in the project. Services has a child class called Deposit. Account class code:
@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;
...
2020-03-13 13:29:51 ERROR SqlExceptionHelper:131 - ОШИБКА: UPDATE или DELETE в таблице "account" нарушает ограничение внешнего ключа "fk8qcea1frw0og19kft1ltq9kf9" таблицы "deposit"
Подробности: На ключ (id)=(1) всё ещё есть ссылки в таблице "deposit".
Answer the question
In order to leave comments, you need to log in
In the end, the @OnDelete annotation helped. The Account class did not change. Services code:
@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;
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question