Step 1: Case code.
Step 2: Case diagram analysis.
------------------------------------------------------------------------Step 1: Case code----------------------------------------- --------------------------------------------------------------------------
When we explain hibernate many-to-one mapping, the first thing to think about is the classic many-to-one relationship, employees and departments, and let's see how they map.
Code Listing 1: Employee class
The package object is one-to-many; public class Employment {private Integer id;private String name;private Department department;public intege R GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Department getdepartment () {return Department;} public void Setdepartment (Department Department) {this.department = Department;} @Overridepublic String toString () {return ' [Employee number: ' +this.id+ ', Employee Name: ' +this.name+ '] ';}
Code Listing 2: Departmental class
The package object is one-to-many; import Java.util.hashset;import Java.util.set;public class Department {private Integer id;private String Name;private set<employment> empset = new hashset<employment> ();p ublic Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public set<employment> Getempset () {return empset;} public void Setempset (set<employment> empset) {this.empset = Empset;} @Overridepublic String toString () {return "[Department Number:" +this.id+ ", Department Name:" +this.name+ "]";}}
Code Listing 3: Employee Mapping File
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Code Listing 4: Departmental mapping file
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Code Listing 5: Customer execution
Package object One-to-many; import Org.hibernate.session;import Org.hibernate.transaction;import Utils.hibernateutil;public class Client {public static void main (string[] args) {client.save ();} public static void Save () {session session = NULL; Transaction Transaction = null;try {session = Hibernateutil.getsession (); Transaction = Session.begintransaction ();// Contact Association Department department= (Department) Session.get (Department.class, 1); Employment employment= (Employment) Session.get (Employment.class,department.getid ()); Employment.setdepartment ( NULL); SYSTEM.OUT.PRINTLN (department); System.err.println (Department.getempset ()); Transaction.commit ();} catch (Exception e) {if (transaction! = null) {Transaction.rollback ();}} finally {if (session! = NULL) {Session.close ();} }}}
-------------------------------------------------------------------------Step 2: Case diagram Analysis-------------------------------------- -------------------------------------------------------------------------
First, insert the Department table and then insert the employee table.
If you insert the Employee table first, insert the Department table?
Hard for the most of the evening, I hope to help everyone, I am very happy.