简体   繁体   English

删除引用的/ OneToMany关系,而不是“清空”列

[英]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). 我要做的动作是我从u.tableData中删除一个条目,然后调用EntityManager进行merge(u)。

OpenJPA will remove the entry from the User object by setting the corresponding record in the MyUserTableData with a user_id = "null". OpenJPA将通过在MyUserTableData中使用user_id =“ null”设置相应的记录来从User对象中删除该条目。

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. 但是我想要的是,如果从User中删除了该条目,它还应该从MyUserTableData中删除记录,而不仅仅是将user_id列设为NULL。

How can I force OpenJPA to delete the OneToMany related entry instead of putting a null in the column? 如何强制OpenJPA删除与OneToMany相关的条目,而不是在列中放置null?

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. 我不会接受假设我的数据库架构不好的答案:)表MyUserTableData基本上是一个外键表,该表将用户连接到另一个实体,但保存着更多的信息,而不仅仅是外键,它向其中添加了一些元数据。既不属于用户也不属于另一个实体的外键。

Thanks! 谢谢!

Sebastian 塞巴斯蒂安

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 http://openjpa.apache.org/builds/1.0.4/apache-openjpa-1.0.4/docs/manual/manual.html#dependent

@ElementDependent => does exactly what I want. @ElementDependent =>正是我想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM