@OneToOne与CascadeType.ALL没有被删除?

时间:2015-08-19 08:12:21

标签: java hibernate

我想从DB中删除父项时删除任何附加的子对象。但它不起作用:

@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> {}

执行时:

@Autowired
private ParentEntityRepository  dao;

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

结果: DB中仍存在MyEntity行!为什么呢?

1 个答案:

答案 0 :(得分:-1)

你可以试试这个

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

如果它不起作用,你可以试试这个:

Hibernate deleting orphans when updating collection