If the following content does not understand, you can find it:
Annotation-based relationship ing preparations: Http://blog.csdn.net/p_3er/article/details/9061911
XML-based many to one: http://blog.csdn.net/p_3er/article/details/9036759
XML-based one-to-many: http://blog.csdn.net/p_3er/article/details/9036921
This article combines many-to-one with one-to-many to form a bidirectional ing. If you only want one way, just remove the annotations on the other side.
Department:
@ Entity @ table (name = "department", catalog = "hibernate") public class Department implements Java. io. serializable {private integer ID; private string name; private set <employee> employees = new hashset <employee> (0); public department () {} public department (string name, set <employee> employees) {This. name = Name; this. employees = employees;} @ ID @ generatedvalue @ column (name = "ID", unique = true, nullable = False) Public integer GETID () {return this. ID;} public void setid (integer ID) {This. id = ID ;}@ column (name = "name", nullable = false, length = 45) Public String getname () {return this. name;} public void setname (string name) {This. name = Name ;}@ onetomany (cascade = cascadetype. all, fetch = fetchtype. lazy, mappedby = "Department")/* One to multiple. In the one-to-multiple relationship between Department and employee, when mappedby in department is specified, the relationship can only be actively maintained by employee. that is, the processing department of the employee cascade. previous ing files: <set name = "employees" inverse = "false" cascade = "all"> <key column = "department_id"> </key> <one-to-learn class = "cn. framelife. hibernate. entity. employee "/> </set> */Public set <employee> getemployees () {return this. employees;} public void setemployees (set <employee> employees) {This. employees = employees ;}}
Employee:
@ Entity @ table (name = "employee", catalog = "hibernate") public class employee implements Java. io. serializable {private integer ID; private department Department; private string name; public employee () {}@ ID @ generatedvalue @ column (name = "ID", unique = true, nullable = false) Public integer GETID () {return this. ID;} public void setid (integer ID) {This. id = ID;} @ manytoone (fetch = fetchtype. lazy) @ joincolumn ( Name = "department_id")/* multiple to one. <Role-to-one name = "department" column = "department_id"> </role-to-one> */public department getdepartment () {return this. department;} public void setdepartment (Department) {This. department = Department;} @ column (name = "name", nullable = false, length = 45) Public String getname () {return this. name;} public void setname (string name) {This. name = Name ;}}