0

I'm using SpringBoot and JPA + Hibernate.

I have these entities:

@Entity
@Table(name = "post")
@AllArgsConstructor @NoArgsConstructor
@Getter @Setter
public class Item extends BaseEntity {

    @Id
    @Column(name = "id")
    @Type(type = "uuid-char")
    private UUID uuid = UUID.randomUUID();

    @OneToMany(mappedBy = "post", orphanRemoval=true, fetch = FetchType.EAGER)
    @MapKey(name="userId")
    private Map<UUID, Share> shares;

    .... 

    @CreationTimestamp
    @Column(name = "creation_date")
    @Setter(AccessLevel.NONE)
    private Instant creationDate;

    @UpdateTimestamp
    @Column(name = "last_mod_date")
    @Setter(AccessLevel.NONE)
    private Instant lastModificationDate;

}


@Entity
@Table(name = "comment"
@AllArgsConstructor @NoArgsConstructor
@Getter @Setter
public class Comment extends BaseEntity {

    @Id
    @Column(name = "id")
    @Type(type = "uuid-char")
    @Setter(AccessLevel.NONE)
    private UUID uuid = UUID.randomUUID();

    @ManyToOne(fetch = FetchType.LAZY)
    @NotNull()
    @JoinColumn(name = "post_id")
    private Post post;

    .....

    @CreationTimestamp
    @Column(name = "creation_date")
    @Setter(AccessLevel.NONE)
    private Instant creationDate;

    @UpdateTimestamp
    @Column(name = "last_mod_date")
    @Setter(AccessLevel.NONE)
    private Instant lastModificationDate;
}

@Test
@Transactional
public void testDeleteItem() {
    itemRepo.delete(item);
    itemRepo.flush();
}

When I try to delete a parent entity (Post), all related entities Comments remains on the database. Why doesn't works the cascade deletion?

Safari
  • 11,437
  • 24
  • 91
  • 191
  • Ref [how-does-jpa-orphanremoval-true-differ-from-the-on-delete-cascade-dml-clause](https://stackoverflow.com/questions/4329577/how-does-jpa-orphanremoval-true-differ-from-the-on-delete-cascade-dml-clause) – Dirk Deyne Apr 08 '21 at 18:01

1 Answers1

0

OrphanRemoval doesn't work. Use cascade = CascadeType.ALL instead