The following relationship creates a foreign key mapping
@Entity
public class Department {
@Id
private String name;
//some more fields
}
@Entity
public class Employee {
@Id
private long id;
private String name;
private String designation;
@ManyToOne
@JoinColumn(name = "fk_department_id", foreignKey = @ForeignKey(name="fk_department"))
private Department department;
}
generates:
...CONSTRAINT
fk_departmentFOREIGN KEY (fk_department_id) REFERENCESdepartment(name)
Question: how can I trigger this constraint creation in hibernate without having to create the Department entity?
Eg just adding the foreign key @Id field without an explicit entity reference. But still trigger the fk on initial creation. The following is of course invalid:
@ManyToOne
@JoinColumn(name = "fk_department_id", foreignKey = @ForeignKey(name="fk_department"))
private String department;
You get the intention. Is that possible?
(sidenote: I'm not interested in creating that foreign key link by startup ddl/sql statements).