简体   繁体   中英

@OneToOne with CascadeType.ALL not getting deleted?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM