我想从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
行!为什么呢?
答案 0 :(得分:-1)
你可以试试这个
@OneToOne(cascade = CascadeType.DELETE_ORPHAN)
private MyEntity my;
如果它不起作用,你可以试试这个: