Search code examples
javajpaone-to-manyopenjpa

Delete a referenced/OneToMany relation instead of "Nulling" the column


We have two entities:

public class User {
  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinColumn(name = "user_id", insertable = true, updatable = true)
  @ElementList(name = "tabledata")
  public List<MyUserTableData> tableData = new ArrayList<MyUserTableData>();
}

public class MyUserTableData {
  public Long user_id;
}

The action that I do is that I remove an entry from u.tableData and then call the EntityManager to merge(u).

OpenJPA will remove the entry from the User object by setting the corresponding record in the MyUserTableData with a user_id = "null".

But what I want is that if the entry is deleted from the User, it should also delete the record from the MyUserTableData and not just NULL the column user_id.

How can I force OpenJPA to delete the OneToMany related entry instead of putting a null in the column?

I will not accept answers that do asume that my database schema is bad :) The table MyUserTableData is basically a foreign key table that connects the user to another entity but holds some more information then just a foreign key, it adds some meta data to the foreign key that neither belong to the user nor to the other entity.

Thanks!

Sebastian


Solution

  • I was able to resolve my issue:

    http://openjpa.apache.org/builds/1.0.4/apache-openjpa-1.0.4/docs/manual/manual.html#dependent

    @ElementDependent => does exactly what I want.