gpt4 book ai didi

java - 从@OneToMany-association : CascadeType. ALL + orphanRemoval = true 中删除 child 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:13:27 24 4
gpt4 key购买 nike

我很难从 OneToMany 关联中删除 child 。我的实体:

@Entity
@Table(name = "PERSON")
public class PersonEntity extends BaseVersionEntity<Long> implements Comparable<PersonEntity>
{
...
// bi-directional many-to-one association to Project
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person", orphanRemoval = true)
private final Set<ProjectEntity> projects = new HashSet<ProjectEntity>();
...

@Entity
@Table(name = "PROJECT")
public class ProjectEntity extends BaseVersionEntity<ProjectPK>
{
@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;
...

我删除 child 的失败尝试:

personEntity.getProjects().clear();

这可行,但我认为这不是正确的方法:

for (Iterator<ProjectEntity> iterator = personEntity.getProjects().iterator(); iterator.hasNext();)
{
ProjectEntity projectEntity = iterator.next();
projectDao.deleteEntity(projectEntity);
iterator.remove();
}

我在这里做错了什么?

谢谢
强尼

最佳答案

关联是双向的,双向关联的拥有方是没有 mappedBy 属性的一方。这意味着在这种情况下,拥有方是项目方。

Hibernate 只考虑拥有方来知道关联是否存在。这意味着要打破一个人和一个项目之间的关联,你必须在项目中将这个人设置为null

关于java - 从@OneToMany-association : CascadeType. ALL + orphanRemoval = true 中删除 child 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10426979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com