Let's say that I have:
class Car {
...
@OneToMany(mappedBy = "wheel", fetch = FetchType.LAZY)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
Set<Wheel> wheels;
}
class Wheel {
...
@ManyToOne(fetch = FetchType.LAZY)
Car car
}
I have this code (spring data, wheelRepository extends CrudRepository) to delete a Wheel:
wheelRepository.delete(wheel);
The code does not delete the wheel and does not throw any exception. Looking at the sql log, there is no "delete" statement.
I finally found a the problem: I have a car with a wheel set containing the wheel.
Doing this will solve the issue:
car.getWheels().remove(wheel);
wheelRepository.delete(wheel);
But is it possible to avoid the first line: when I delete a wheel, I want the wheel to be removed from its car automatically?
Another question: is it possible by default to throw an exception instead of having a "silently non-delete"?
Thanks!