私は次のエンティティを持っています
学生
@Entity
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//getter and setter for id
}
先生
@Entity
public class Teacher implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//getter and setter for id
}
タスク
@Entity
public class Task implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(optional = false)
@JoinTable(name = "student_task", inverseJoinColumns = { @JoinColumn(name = "student_id") })
private Student author;
@ManyToOne(optional = false)
@JoinTable(name = "student_task", inverseJoinColumns = { @JoinColumn(name = "teacher_id") })
private Teacher curator;
//getters and setters
}
author
とcurator
はすでにDBに保存されており、両方とも接続状態になっていることを考慮してください。私は自分のTask
:を持続させようとしています
Task task = new Task();
task.setAuthor(author);
task.setCurator(curator);
entityManager.persist(task);
Hibernateは次のSQLを実行します。
insert
into
student_task
(teacher_id, id)
values
(?, ?)
もちろん、これはnull value in column "student_id" violates not-null constraint
誰かがこの問題とそれを解決するための可能な方法を説明できますか?
アップデート
以下の私自身の解決策を参照してください。