1

How do i delete all the entries using hibernate deleteAll() ?

I have a class with multiple @oneToMany relationships (having like +5000 child entities) and when i try to do deleteAll i get the title error

oracle.jdbc.OracleDatabaseException: ORA-02292: integrity constraint (xxx) violated - child record found

I've tried adding

cascade = {CascadeType.ALL}

and

orphanRemoval=true

to @OneToMany relationship class, but no help.

It's a bidirectional relationship with following classes

@OneToMany(targetEntity = XXX.class, fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval=true, mappedBy = "zzz")
    @Fetch(FetchMode.SELECT)
    @JsonManagedReference
    private List<XXX> xxx;

@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(targetEntity = YYY.class, fetch = FetchType.LAZY, orphanRemoval=true, cascade = {CascadeType.ALL}, mappedBy = "zzz")
@Fetch(FetchMode.SELECT)
@JsonManagedReference
private List<YYY> yyy;

with child elements like

@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinColumn(name = "XXX", nullable=false)
@JsonBackReference
private XXX zzz;

i also tried HQL DELETE query but that dosent get me anywhere either.

How on earth do i delete all these entities consistently? So far i've manually droped the tables since this problem started (all entities were deleted fine just few days ago) but thats starting to really annoy me, but i cant figure how to do this.

Thanks!

Clomez
  • 1,410
  • 3
  • 21
  • 41

1 Answers1

1

You have set CascadeType.ALL on your parent and the best way to delete should be call one single delete on parent entity

If you try to delete a child, it can be hibernate will propagate delete on a parent that has still children not still deleted.

Last resort with this king of problem is:

  1. Enable logs on Spring Boot Application
  2. Run sql query generated in SQL server
  3. Find where the error happens evaluating the current database condition
  4. Change JPA if necessary
ValerioMC
  • 2,926
  • 13
  • 24
  • Im trying to delete the parent, but thats what gives the error. I tried changing CascadeType to all possible values but no help. – Clomez Oct 12 '18 at 06:49
  • have you tried to remove `CascadeType.ALL` in `@ManyToOne` relationship and and if still not working try to `fetch = FetchType.EAGER` in `@OneToMany` relationship. – ValerioMC Oct 12 '18 at 07:20
  • well, after many days of confusion i think i can accept the part for enabling logs.. In the end i had to manually drop tables but the problem seems to be fixed – Clomez Oct 16 '18 at 07:35