我有以下實體兩個實體@ManyToOne應該加入同一個表
學生
@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
已經存儲在數據庫中,並且都處於附加狀態。我想堅持我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
誰能解釋這個問題,可能的方式來解決它?
UPDATE
見下面我自己的解決方案。
我很抱歉,但是這不工作爲我的情況。不過,我設法使用@SecondaryTable解決了這個問題。 – yatskevich 2010-06-12 23:01:18