Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORA-02292 when using Hibernate

I am trying to implement a delete function but all I get is this ORA-02292 ERROR:

Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02292: Integritäts-Constraint (VDMA.FK892DE8B473F40868) verletzt - untergeordneter Datensatz gefunden

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)

I have an Entity (MainEntity) that has an n:m relationship with its child entity.

For example: A User can have multiple cars, each car can be driven by different users.

When a User is deleted, I want the associations between the User and the Car to be deleted as well. That´s why I thought I could do the following :

User Entity

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "user_car", joinColumns = {@JoinColumn(name = DATABASE_COLUMN_ID, nullable = false, updatable = false) }, inverseJoinColumns = {@JoinColumn(name = DATABASE_COLUMN_TYPE_ID,
      nullable = true, updatable = false) })
private Set<UserCar> userCars;

Car Entity

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "userCars")
  private Set<User> users;

The result is :

The tables get created, the data gets persisted - everything works fine. EXCEPT deleting entries: As soon as I try to delete a User and the User has a car (and therefore a user : car relationship) I do get the errors shown above.

If the user doesn´t have any cars I can delete him without any issues. So the problem must be the constraint in the USER_CAR table.

like image 956
Breiti Avatar asked Nov 17 '25 00:11

Breiti


1 Answers

Clear userCars as well:

user.getUserCars().clear()

This will break the association between user and associated cars (it will delete the corresponding records from the junction table).

Also, you don't want CascadeType.ALL on many-to-many associations, because it implicitly contains CascadeType.REMOVE. Using it means that removal will be cascaded to cars as well, although there are other users who are associated with those cars.

like image 54
Dragan Bozanovic Avatar answered Nov 19 '25 13:11

Dragan Bozanovic