ユーザー エンティティとジョブ エンティティの間に次の双方向の 1:n 関係があります。
ユーザークラス:
...
@OneToMany(mappedBy = "user",cascade={CascadeType.PERSIST})
public Collection<Job> getJobs() {
return jobs;
}
public void addJob(Job j) {
jobs.add(j);
j.setUser(this);
}
...
ジョブ クラス:
// Job class
...
@ManyToOne
public User getUser() {
return user;
}
ユーザーオブジェクトを保存する(そして、休止状態にジョブエンティティを自動的に保存させる)ことができます...次に、データベースからジョブエンティティを削除したいときに、例外が発生します....
User user = new User();
user.addJob(new Job());
entityManager.getTransaction().begin();
entityManager.persist(user);
entityManager.getTransaction().commit();
Job j = entityManager.find(Job.class, 1L);
entityManager.getTransaction().begin();
entityManager.remove(j);
entityManager.getTransaction().commit();
例外:
Exception in thread "main" javax.persistence.RollbackException: Error while committing the transaction
Caused by: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [dst1.model.Job#<null>]
なぜこれが起こるのですか?ところで。ジョブオブジェクトで永続化を明示的に呼び出すと、問題は発生しません...しかし、ジョブを個別に永続化したくありませんが、休止状態でそれらを永続化させます(カスケードを使用すると、実際に機能します....)
どうも