orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

Struggling with deletion of orphans on a @OneToMany(orphanRemoval = true) annotated field

Open emorency opened this issue 13 years ago • 4 comments

I try to update the list of Text by removing an element and adding a new one. The resulting ParentObject is updated correctly but when I look in OrientDb, the Text that is now orphan is sill there...

My classes are like this:

@Unique(properties = "name")
public class ParentObject {

  @Nonnull
  @NotBlank
  private String name;

  @OneToMany(orphanRemoval = true)
  private List<Text> titles = new ArrayList<Text>();

  @Id
  private String id;

  @Version
  private Integer version;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public Integer getVersion() {
    return version;
  }

  public void setVersion(Integer version) {
    this.version = version;
  }

  @Nonnull
  public String getName() {
    return name;
  }

  public void setName(@Nonnull String name) {
    this.name = name;
  }

  public List<Text> getTitles() {
    return titles;
  }

  public void setTitles(List<Text> titles) {
    this.titles = titles;
  }
}

public class Text{

 private String locale;

  private String text;

 @Id
  private String id;

  @Version
  private Integer version;

  public Text() {
  }

  public Text(String text, String locale) {
    this.text = text;
    this.locale = locale;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public Integer getVersion() {
    return version;
  }

  public void setVersion(Integer version) {
    this.version = version;
  }

 public String getLocale() {
    return locale;
  }

  public void setLocale(String locale) {
    this.locale = locale;
  }

  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }
}

Any ideas ?

emorency avatar Oct 24 '13 12:10 emorency

I run on orientDb 1.5.1.

emorency avatar Oct 24 '13 12:10 emorency

@MaDaPHaKa is this for you?

lvca avatar Oct 24 '13 12:10 lvca

Hi, we have testcases for that usecase that passes. Can you post me your object remove code?

2013/10/24 Luca Garulli [email protected]

@MaDaPHaKa https://github.com/MaDaPHaKa is this for you?

— Reply to this email directly or view it on GitHubhttps://github.com/orientechnologies/orientdb/issues/1762#issuecomment-26989733 .

Luca Molino

MaDaPHaKa avatar Oct 25 '13 09:10 MaDaPHaKa

I was seeing similar behavior using Orient 1.3.0, and found that orphan removal for a list seems to work "as expected" if you make your modifications to the contents of the parent object's existing list - don't assign a new list using the setter.

If you make your modifications to the parent's existing list, orphans are deleted. If you assign a new list to the parent object's list field (e.g., a list containing only the child objects you want to keep), child objects from the original list that should be considered orphaned will remain in the db.

So, in the case of this issue, try doing something like

myParentObj.getTitles().clear();
myParentObj.getTitles().addAll(listOfTitlesToKeep);

instead of

myParentObj.setTitles(listOfTitlesToKeep);

buzzkillington avatar Mar 28 '14 18:03 buzzkillington