リレーションの片側のみが永続コンテキストで管理されているのに、双方向リレーションの要素を削除できるのはなぜですか (例 I)? 機能しない一方向のリレーションシップがある場合 (例 II を参照)。なんで?
エンティティ:
@Entity
Class User {
...
@OneToMany(mappedBy = "user")
private List<Process> processes;
@OneToOne // Unidirectional
private C c;
...
@PreRemove
private void preRemove() {
for (Process p : processes) {
p.internalSetUser(null);
}
}
...
}
@Entity
Class Process {
...
@ManyToOne
private User user;
...
@PreRemove
protected void preRemove() {
if (this.user != null) {
user.internalRemoveProcess(this);
}
}
...
}
@Entity
Class C {
}
例 I:
// Create User u1 with Processes p1, p2
tx.start();
// Only u1 is manged in persistence context and no process
userFacade.delete(u1); // There following is called: >> em.remove(em.merge(u1)); // Works
tx.commit();
例 II:
// Create User u and Object C c, establish their relation.
tx.start();
cFacade.remove(c); //>>MySQLIntegrityConstraintViolationException,foreign key constraint fails
ty.commit();
最初の例では、これらの内部メソッドを使用して、それぞれの場合に関係の反対側を設定しますが、この反対側は永続コンテキストで管理されていないと思いますか?! ユーザーのプロセスを変更してユーザーを保存すると、cascade.MERGEを使用しない限り、または両方がトランザクションに読み込まれてPCで管理されていない限り、プロセスは更新されません。では、なぜ除去が機能するのでしょうか。