I first came into contact with hibernate yesterday and only understood the one-to-one relationship ing. Today, I learned about the ing of one-to-many relationships with new gains.
1. One-to-many relationship ing
To create a table with one-to-many relationship, the primary key of one party is added to the table of multiple parties as the foreign key. Here is an example of employees and departments. In the past, when a pojo class was created without hibernate, an attribute should be added to the employee class EMP, that is, the Department number deptid. hibernate is different. You need to add a set in one class to store the objects of multiple parties. In addition, you need to add a "one" object to the class of "multiple" parties. That is to say, a set needs to be added to the dept class to store the EMP object. Because a Department corresponds to multiple employees, it is represented by a set. Each employee can belong to only one department. Therefore, a DEPE object must be added to the employee EMP to indicate the department to which the employee belongs. The code for department and employee classes is as follows:
1 public class Dept implements Serializable { 2 private int deptId; 3 private String deptName; 4 private Set<Emp> emps = new HashSet<Emp>(); 5 public int getDeptId() { 6 return deptId; 7 } 8 public void setDeptId(int deptId) { 9 this.deptId = deptId;10 }11 public String getDeptName() {12 return deptName;13 }14 public void setDeptName(String deptName) {15 this.deptName = deptName;16 }17 public Set<Emp> getEmps() {18 return emps;19 }20 public void setEmps(Set<Emp> emps) {21 this.emps = emps;22 }23 }
1 public class Emp implements Serializable{ 2 private int empNo; 3 private String empName; 4 private Date empBirthday; 5 private Dept dept; 6 public int getEmpNo() { 7 return empNo; 8 } 9 public void setEmpNo(int empNo) {10 this.empNo = empNo;11 }12 public String getEmpName() {13 return empName;14 }15 public void setEmpName(String empName) {16 this.empName = empName;17 }18 public Date getEmpBirthday() {19 return empBirthday;20 }21 public void setEmpBirthday(Date empBirthday) {22 this.empBirthday = empBirthday;23 }24 public Dept getDept() {25 return dept;26 }27 public void setDept(Dept dept) {28 this.dept = dept;29 }30 31 }
After writing the pojo class, you need to configure the ing between these two classes and tables. The Code is as follows:
1. Dept. HBM. xml
1
Here, a set is configured, and the name = "EMPs" in it indicates the attribute EMPs in the dept class. It is a set that stores the EMP object. Cascade = "Save-Update, delete" indicates that data can be deleted in cascade mode and inserted in cascade mode. Cascade has four values: all, save-Update, delete, and none. The default value is none, indicating that cascade operations cannot be performed. <One-to-operate class = "com. pojo. EMP "/> indicates that dept has a one-to-multiple relationship with EMP. They establish a relationship with deptid, that is, deptid is the foreign key of EMP.
2. EMP. HBM. xml
1
<Strong-to-one> </strong-to-one> is added to indicate that the relationship between EMP and DEPT is many-to-one, name = "Dept" indicates that there is an attribute in the EMP class that is dept to the object Dept, column = "deptid" indicates that the relationship between them is established using deptid.
The following code inserts cascade data:
1 package COM. test; 2 3 Import Java. util. date; 4 5 import Org. hibernate. session; 6 Import Org. hibernate. transaction; 7 8 Import COM. pojo. dept; 9 Import COM. pojo. EMP; 10 Import COM. util. dbutil; 11 12 public class cascade insert data {13 14/** 15 * @ Param args16 */17 public static void main (string [] ARGs) {18 // get session19 session = dbutil. getsession (); 20 // create a new dept21 dept = new dept (); 22 Dept. setdeptname ("dinner"); 23 24 // create emp25 EMP e1 = new EMP (); 26 e1.setempname ("Li Bai"); 27 e1.setempbirthday (new date ()); 28 29 EMP e2 = new EMP (); 30 e2.setempname ("Wang Wei"); 31 e2.setempbirthday (new date (); 32 33 Dept. getemps (). add (E1); 34 Dept. getemps (). add (E2); 35 36 transaction TR = session. begintransaction (); 37 try {38 session. save (Dept); 39 tr. commit (); 40} catch (exception e) {41 TR. rollback (); 42} finally {43 session. close (); 44} 45} 46 47}
Create a dept object, create two EMP objects, add these two EMP objects to the set of dept objects, and then save Dept. For cascading insert, you only need to operate the parent table to operate the child table. The premise is that the previous cascade = "Save-Update" must be written.