1

I want to delete any attached child objects when removing a parent from DB. But it does not work:

@Entity
public class ParentEntity {
    @Id
    private Long id;    

    //must remain unidirectional
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    private MyEntity my;

}

@Entity
public class MyEntity {
    private String text;
}

public interface ParentEntityRepository extends CrudRepository<ParentEntity, Long> {}

When I execute:

@Autowired
private ParentEntityRepository  dao;

@Transactional
public void removeId(long id) {
      dao.delete(id);
}

Result: MyEntity row still exists in DB! Why?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

-1

You can try this

@OneToOne(cascade = CascadeType.DELETE_ORPHAN)
private MyEntity my;

If it doesn't works you can try this:

Hibernate deleting orphans when updating collection

Community
  • 1
  • 1
Kratul
  • 158
  • 2
  • 13