Diagram, detailed hibernate many-to-one mapping

Source: Internet
Author: User
Tags generator

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.