There are 3 classes:
@Entity public class А { ... @OneToMany(mappedBy = "а", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) private Set<В> bObj = new HashSet<B>(); ... } @Entity public class B { @Column(name = "amount", nullable = false) private Integer amount; @ManyToOne @JoinColumn(name = "a_id", nullable = false) private A a; @ManyToOne @JoinColumn(name = "c_id", nullable = false) private C c; } @Entity public class C { ... @OneToMany(mappedBy = "c", fetch = FetchType.LAZY) private Set<В> bObj = new HashSet<B>(); ... } I do not understand how to set up the @OneToMany connection, so that when a class A object is deleted, all the objects of class B associated with it will be deleted?
Something I'm confused ...
Throws errors:
[ERROR] (DAO-слой класса А.java:33) - Ошибка при удалении. id = 5 org.hibernate.exception.ConstraintViolationException: could not execute statement and
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`таблица В`, CONSTRAINT `FK_bbpbxkkfko0nwn5uleye8org4` FOREIGN KEY (`а_id`) REFERENCES `таблица С) Part of the code is removed, the name changed for simplicity.
ZY As an option to delete objects of class B according to the list that we receive from A, and then delete A ... but can there be a simpler solution with the correct setting?
Yeah, I see. Then another question is why hibernate does not create (and does not update) the ON DELETE CASCADE scheme if I did everything for it:
@ManyToOne @JoinColumn(name = "а_id", nullable = false) @OnDelete(action = OnDeleteAction.CASCADE)<--- private А а; I'll fix it with my hands, but still ...