Hibernate (b) Many-to-one relationship mapping

Source: Internet
Author: User

Learning Hibernate is a more convenient way to manipulate databases, there are many-to-one relationships in relational models in the database, such as the relationships between employees and departments, and how does this relationship map in hibernate? Let me use a small demo to explain in detail.

The build map is divided into the following steps:

1. Design the domain object Department, Employee, the code is as follows:

Package Cn.itcast.hibernate.domain;public class Department {private int id;private String name;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Package Cn.itcast.hibernate.domain;public class Employee {private int id;private String name;private Department depart; public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Department Getdepart () {return depart;} public void Setdepart (Department depart) {This.depart = Depart;}}

2. Import Hibernate.jar and its dependent packages, the jar package has been provided in the above blog, no longer repeat here.

3. Write the Department.hbm.xml and Employee.hbm.xml mapping files, the code is implemented as follows:

<?xml version= "1.0"?>  <! DOCTYPE hibernate-mapping public       "-//hibernate/hibernate mapping DTD 3.0//en"      "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">  

<?xml version= "1.0"?>  <! DOCTYPE hibernate-mapping public       "-//hibernate/hibernate mapping DTD 3.0//en"      "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">  
The "Many-to-one" part of Employee.hbm.xml is the focus, through which the association between department object and employee is established.

4. Write the Hibernate.cfg.xml configuration file, the code is implemented as follows:

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibernate.sour Ceforge.net/hibernate-configuration-3.0.dtd "> 5. Write the Hibernateutils class, which is mainly used to complete hibernate initialization and provide a way to get the session, the Code is implemented as follows:
Package Com.entity;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public Final class Hibernateutil {private static sessionfactory sessionfactory; Private Hibernateutil () {}static {//parse config file, complete hibernate initialization configuration cfg=new configuration (); Cfg.configure ();// All information can be found in Sessionfactory sessionfactory=cfg.buildsessionfactory ();} public static Sessionfactory Getsesssionfactory () {return sessionfactory;} public static Session GetSession () {return sessionfactory.opensession ();}}
6. Write the main method, by manipulating the Department class and the employee class, to write data to the database, the code is implemented as follows:
Package Com.entity;import Org.hibernate.session;import Org.hibernate.transaction;import Cn.itcast.hibernate.domain.department;import Cn.itcast.hibernate.domain.employee;public class Many2One {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubadd ();} Static Department Add () {Session s = null; Transaction tx = Null;try {Department depart = new Department ();d epart.setname ("Depart name"); Employee EMP = new Employee (); Emp.setdepart (depart); Emp.setname ("emp name"); s = hibernateutil.getsession (); tx = S.begintransaction (); S.save (depart); S.save (EMP); Tx.commit (); return depart;} Finally {if (s! = null) S.close ();}}}
Let's take a look at the changes in the data table as follows:


Through the above methods, we can easily accomplish many-to-one relationship from the relational model to the object model of the mapping, perfect embodiment of the ORM idea.

I hope that my explanation can help you learn hibernate.

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.