1. Requirements
A department has a number of employees; "One-to-many"
Multiple employees, belonging to one department "many to one"
2. Entity Bean Design
Dept:
Public class Dept { privateint depid; Private String depname; Private New Hashset<employee>(); Set ... Get ... }
Employee:
Public class Employee { privateint empId; Private String empname; Private Double salary; Private Dept Dept; Set ... Get ...}
3. Configure the mapping file
Dept.hbm.xml
<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.juaner.hibernate.department"> <classname= "Dept"Table= "T_dept"> <!--PRIMARY Key - <IDname= "Depid"> <Generatorclass= "Native"/> </ID> < Propertyname= "Depname"column= "Deptname"type= "string"/> <!--configuration of a one-to-many association map - <Setname= "Emps"Table= "T_employee" > <!--foreign key Fields - <Keycolumn= "dept_id"></Key> <!--One -to-many relationships - <One-to-manyclass= "Employee"/> </Set> </class></hibernate-mapping>
Employee.hbm.xml
<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.juaner.hibernate.department"> <classname= "Employee"Table= "T_employee"> <!--PRIMARY Key - <IDname= "EmpId"column= "EmpId"> <Generatorclass= "Native"/> </ID> < Propertyname= "EmpName"column= "EmpName"type= "string"/> < Propertyname= "Salary"column= "Salary"/> <!--Many-to-one mapping - <Many-to-onename= "Dept"column= "dept_id"class= "Dept"></Many-to-one> </class></hibernate-mapping>
4. Save your data
- Use the "one" side to set the association
@Test Public voidTest1 () {Session session=sf.opensession (); Session.begintransaction (); //Department ObjectDept Dept =NewDept (); Dept.setdepname ("Development Department"); //Employee ObjectEmployee Employee =NewEmployee (); Employee.setempname ("Zhang San"); Employee.setsalary (1000); Employee Employee2=NewEmployee (); Employee2.setempname ("John Doe"); Employee2.setsalary (2000); Dept.getemps (). Add (employee); Dept.getemps (). Add (Employee2); Session.save (dept); Session.save (employee); Session.save (EMPLOYEE2); Session.gettransaction (). commit (); Session.close (); }
5 SQL statements are executed, with the last two update statements used to set the association:
Hibernate:insert into t_dept (deptname) VALUES (?) Hibernate:insert into T_employee (EmpName, salary, dept_id) VALUES (?,?,? ) Hibernate:insert into T_employee (EmpName, salary, dept_id) VALUES (?,?,?) Hibernate:update T_employee set dept_id=? where empid=? hibernate:update t_employee set dept_id= where empid=?
- Use the "many" side to set the association
@Test Public voidTest2 () {Session session=sf.opensession (); Session.begintransaction (); //Department ObjectDept Dept =NewDept (); Dept.setdepname (Personnel); //Employee ObjectEmployee Employee =NewEmployee (); Employee.setempname ("Zhang San"); Employee.setsalary (1000); Employee Employee2=NewEmployee (); Employee2.setempname ("John Doe"); Employee2.setsalary (2000); Employee.setdept (dept); Employee2.setdept (dept); //save one party first, then save more of the party, the relationship will be automatically maintained, reduce unnecessary statementsSession.save (dept); Session.save (employee); Session.save (EMPLOYEE2); Session.gettransaction (). commit (); Session.close (); }
Only three SQL statements are executed at this time:
Hibernate:insert into t_dept (deptname) VALUES (?) Hibernate:insert into T_employee (EmpName, salary, dept_id) VALUES (?,?,? ) Hibernate:insert into T_employee (EmpName, salary, dept_id) VALUES (?,?,?)
5.inverse Properties
The inverse property is used to set whether inversion is controlled. In a one-to-many mapping, you can only configure on the side of "a" and the default value is False, that is, the "one" party has control. The inverse value has the following effects on the data:
- Saving Data has an impact
If you set control inversion, that is inverse=true, then the association is maintained through the departmental side. While saving the department, the data is saved, but the relationship is not maintained. The foreign key field is null
- Get data without impact
- Disassociate relationships have an impact
Inverse=false, you can disassociate
Inverse=true, the current party has no control, cannot disassociate (no UPDATE statement, no error)
- Deleting data has an impact on association relationships
Inverse=false, with control, can be deleted. Empty the foreign key reference before deleting the data
Inverse=true, no control: If the deleted record has been referenced by foreign key, will error, violate the main foreign KEY reference constraint! If the deleted records are not referenced, you can delete them directly.
6.cascade Properties
The Cascade property is used to set cascading methods, which have 4 values:
None cascade operation, default value
Save-update Cascade Save or update
Delete Cascade deletion
All cascade save, UPDATE, delete
One-to-many and multi-pair mappings in Hibernate