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?