I have following problem: when I try to delete an entity that has following relation:
@OneToMany(mappedBy="pricingScheme", cascade=CascadeType.ALL, orphanRemoval=true)
private Collection<ChargeableElement> chargeableElements;
with a CrudRepository
through a provided delete method it removes the entity along with its all chargeable elements which is fine. The problem appears when I try to use my custom delete:
@Modifying
@Query("DELETE FROM PricingScheme p WHERE p.listkeyId = :listkeyId")
void deleteByListkeyId(@Param("listkeyId") Integer listkeyId);
it says:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Cannot delete or update a parent row: a foreign key constraint fails
(`listkey`.`chargeableelements`, CONSTRAINT `FK_pox231t1sfhadv3vy7ahsc1wt`
FOREIGN KEY (`pricingScheme_id`) REFERENCES `pricingschemes` (`id`))
Why I am not allowed to do this? Does @Query
methods do not support cascade property? I know I can findByListkeyId(…)
first and then remove persistent entity with standard delete method, but it is inelegant. Is it possible to use a custom @Query
method the way I tried to?