简体   繁体   English

没有删除带有CascadeType.ALL的@OneToOne?

[英]@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! 结果: MyEntity行仍存在于数据库中! 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 更新集合时休眠删除孤儿

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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