我正在开始一个项目,以更详细地了解J2E和EJB。
上下文:在实习结束时,该学生要写一份报告,并在他的教授面前作一个关于实习的演讲。
我有一个数据库,称为“ grade_management”。它必须包含一个“学生”,“演示”,“报告”,“教授”和一个“标记”(有多个评估标准,例如表达,PowerPoint的质量...)表。但是现在它是空的,因为我想通过J2E实现它。
在我的EJB中,我有一个“ Presentation”类。这是哪个国家:
@Entity
public class Presentation implements Serializable {
@Id
@GeneratedValue (strategy=GenerationType.AUTO)
private int presentation_id;
private Date date;
private mark_id;
private int professor_id;
public Soutenance() {}
public Soutenance(Date date) {
this.date = date;
}
}
但是Presentation表包含2个外键:Professor_id和mark_id。我的问题是:我如何指出它们都是外键?
很抱歉,如果我不清楚,请随时提出问题。
Cordially
您不应该通过其他实体的ID来引用其他实体,而应该直接引用该实体。
类似的东西:
@OneToOne
@JoinColumn(name = "mark_id", referencedColumnName = "id")
private Mark mark; // supposed here that mark_id if link to entity `Mark`
@OneToOne
@JoinColumn(name = "professor_id", referencedColumnName = "id") // suppose "id" is the column name of the PK inside the table Professor.
private Professor professor; // supposed here that professor_id if link to entity `Professor`
此代码假设您使用单向关系。对于双向,您必须在另一端定义它(标记/教授类型)
@OneToOne(mappedBy = "professor")
private Presentation presentation;