从@ OneToMany-association删除孩子:CascadeType.ALL + orphanRemoval = true不起作用 [英] Removing childs from @OneToMany-association: CascadeType.ALL + orphanRemoval = true not working

查看:675
本文介绍了从@ OneToMany-association删除孩子:CascadeType.ALL + orphanRemoval = true不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难从OneToMany协会中删除孩子。我的实体:

  @Entity 
@Table(name =PERSON)
public class PersonEntity extends BaseVersionEntity<长>实现可比< PersonEntity>
{
...
//双向多对一关联到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
私人ProjectPK ID;
...
//与UdbPerson的双向多对一关联
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =PERSON_ID, nullable = false,insertable = false,updatable = false)
私人PersonEntity person;
...

@Embeddable
public class ProjectPK实现Serializable
{
//默认序列版本ID,对于可序列化类是必需的。
private static final long serialVersionUID = 1L;

@NotNull
@Column(name =PERSON_ID)
private long personId;
...

我尝试删除孩子失败:

  personEntity.getProjects()。clear(); 

这很有效,但我认为这不是正确的做法:

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

我在这里做错了什么?



感谢

Jonny

解决方案

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

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

I'm having a hard time removing childs from a OneToMany-association. My entities:

@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;
  ...

My unsuccessful attempt to delete the childs:

personEntity.getProjects().clear();

This works, but I don't think thats the right approach:

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

What am I doing wrong here?

Thanks
Jonny

解决方案

The association is bidirectional, and the owning side of a bidirectional association is the one where there is no mappedBy attribute. This means that in this case, the owning side is the project side.

Hibernate only considers the owning side to know if the association exists or not. This means that to break the association between a person and a project, you must set the person to null in the project.

这篇关于从@ OneToMany-association删除孩子:CascadeType.ALL + orphanRemoval = true不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆