1

I have some strange issue with jpa delete propagation : I have an object A with a one to many relation with an object C . I have an other object B which have a one to one relation with object C (A-C and B-C) When i try to delete an 'A' object then hibernate delete it without deleting its C objects. But before that it tries to select C objects using the second relation shop

Here is the logs :

Hibernate: select ... from A A0_ where A0_.external_id=?
Hibernate: select ... from C C0_ inner join B B1_ on C0_.B_id=B1_.id  where C0_.A_id=?
Hibernate: select ... from C C0_ inner join B B1_ on C0_.B_id=B1_.id  where C0_.B_id=?
Hibernate: delete from A where id=?

Here is my code :

in A class :

@OneToOne(mappedBy = "a", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private C c;

in B class :

@OneToMany(mappedBy = "b", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private List<C> cs;

in C class :

@OneToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "a_id", nullable = false)
private A a;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "b_id", referencedColumnName = "id", nullable = false)
private B b;

i have redefined equals and hashCode method using a business key in the three entities

I'm using spring data jpa with hibernate

thanks in advance, Amrou

2 Answers2

1

You have to remove the c contained in A from the list of cs contained in B, because Hibernate has to unschedule the delete operation if the removed entity is persisted (either directly or by cascading of the PERSIST operation).

This behavior is required by the JPA specification; see this question for more details.

Also, you may find this answer useful; it nicely describes the process of removal unscheduling.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
0

I have the same problem as described in the first link that you provided.
As you said, i must delete the c entities before deleting the b entity. I was expecting that hibernate will do it automatically, but it doesn't because of the oneToOne relation to A owned by C .
Thank you very much.