I have a few entities linked as follows:
@Entity
@Table(name = "distribution_activity")
public class DistributionActivity extends AbstractActivity {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "activity", orphanRemoval = true)
protected Set<DistributionTask> tasks = new TreeSet<>();
...
}
and
@Entity
@Table(name = "distribution_task")
public class DistributionTask extends AbstractTask {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "activity_id")
protected DistributionActivity activity;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "store_id")
protected Store store;
...
}
and
@Entity
@Table(name = "store")
public class Store extends AbstractAuditableEntity {
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, mappedBy = "store", orphanRemoval = true)
protected Set<DistributionTask> distributionTasks = new TreeSet<>();
...
}
The repositories are as follows:
@Repository
public interface DistributionActivityRepository extends PagingAndSortingRepository<DistributionActivity, Long> {
}
and
@Repository
public interface StoreRepository extends PagingAndSortingRepository<Store, Long> {
}
I'm using MySQL and the tables are generated WITHOUT any cascade options on the foreign keys. When I delete a DistributionActivity everything works fine and Hibernate actually issues delete statements for each of the linked tasks.
hibernate.SQL:109 - delete from distribution_task where id=? and version=?
hibernate.SQL:109 - delete from distribution_activity where id=? and version=?
When I delete a Store however, no delete statements are generated for the linked tasks and a MySQLIntegrityConstraintViolationException exception is thrown referring to a foreign key violation.
hibernate.SQL:109 - delete from store where id=? and version=?
Any clues?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…