从@ OneToMany-association中删除子项:CascadeType.ALL + orphanRemoval = true not working
我很难从OneToMany协会中删除孩子。 我的实体:
@Entity @Table(name = "PERSON") public class PersonEntity extends BaseVersionEntity implements Comparable { ... // bi-directional many-to-one association to Project @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person", orphanRemoval = true) private final Set projects = new HashSet(); ... @Entity @Table(name = "PROJECT") public class ProjectEntity extends BaseVersionEntity { @EmbeddedId private ProjectPK id; ... // bi-directional many-to-one association to UdbPerson @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PERSON_ID", nullable = false, insertable = false, updatable = false) private PersonEntity person; ... @Embeddable public class ProjectPK implements Serializable { // default serial version id, required for serializable classes. private static final long serialVersionUID = 1L; @NotNull @Column(name = "PERSON_ID") private Long personId; ...
我未能成功删除孩子:
personEntity.getProjects().clear();
这有效,但我不认为这是正确的方法:
for (Iterator iterator = personEntity.getProjects().iterator(); iterator.hasNext();) { ProjectEntity projectEntity = iterator.next(); projectDao.deleteEntity(projectEntity); iterator.remove(); }
我在这做错了什么?
谢谢
强尼
关联是双向的,双向关联的拥有方是没有mappedBy属性的关联。 这意味着在这种情况下,拥有方是项目方。
Hibernate只考虑拥有方来知道关联是否存在。 这意味着要破坏人员和项目之间的关联,您必须在项目中将人员设置为null
。
- 使用Spring HibernateDaoSupport进行延迟加载?
- Hibernate Session刷新行为
- Spring REST,JSON“无法处理托管/后向引用’defaultReference’”415不支持的媒体类型
- JPA2 Criteria API更优选的一些现实世界示例是什么?
- 如何将Hibernate类映射转换为Spring应用程序上下文?
- Hibernate PersistentSet remove()操作不起作用
- 如何取消正在运行的SQL查询?
- oracle上的Hibernate序列,@ GeneratedValue(strategy = GenerationType.AUTO)
- 我应该如何使用Spring / Hibernate项目设置业务对象