java - Creating a foreign key mapping in hibernate -
I have two tables of department and staff, and one department can have many employees.
If I have to model the database ID as a foreign key and use the hibernate to model the employee table, then I have two options.
a) I either I can make a list in the department category
b) Using @ManyToOne and @JoinColumn on the column name mentioned in the Employee class = 'Department ID'.
Which approach is recommended? Or are these two approaches completely used for different problems?
There are two organizations presently there and departments. Secondly, we have to understand that the meaning of ManyToOne
is here, as you said, many employees can be in the same department.
Be the kind of mapping, please note that this is just a snippet:
@Entity @Table (name = "EMPLOYEE") public class employee {@Id @ GeneratedValue @column (name = "employee_id") Private longtime employee ID; @column (name = "first name") private string firstname; @column (name = "last name") private string last name; @ManyToOne @JoinColumn (name = "department_id") Department of Personal Department; // gateer and setter methods
}
@inti @Table (name = "department") public class department {@Id @ generatedValue @Column ( Name = "section_id") private long section ID; @column (name = "department_name") private string field name; @OneToMany (mapped = "department") private set & lt; Employee & gt; Employees; // gateer and setter methods
}
Since there is a bi-directional
relationship, here on one side the owner of the relationship will be here Annotation @ join column
indicates that this entity is the owner of the relationship. That is, there is a column with a foreign key for the table referenced in the corresponding table, while the attribute mapped
indicates that the unit on this side is the inverse of the relationship, and the owner has "other" Organization
mapppedby
can be found.
Comments
Post a Comment