"SSH Three frames" Hibernate Foundation Sixth: mapping, analysis and adding and querying of many-to-one relational relationships

Source: Internet
Author: User

Here's an example: the relationship between the department and the employee.

A department can correspond to multiple employees, which is obviously a many-to-one relationship.


We need to create two entity classes: Employee, Department (Department)

Employee class: Employee.java (In this case we have added a department-type attribute to do the correlation)

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;}}


Department Department.java

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;}}


Then, we will build their mapping files in their directory:

Employee.hbm.xml (Here we have added a <many-to-one/> tag to specify the mapping)

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

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


Finally we need to build hibernate.cfg.xml, which is used to establish a mapping file that connects to the database and loads each entity class:

<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
By this point, all the configurations have been set up and you are ready to start testing.

We create a Many2one.java file:

Package Cn.itcast.hibernate;import Org.hibernate.session;import Org.hibernate.transaction;import Cn.itcast.hibernate.domain.department;import Cn.itcast.hibernate.domain.employee;public class Many2One {public static void Main (string[] arg) {add (); Employee emp = query (1);} 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);   Establish an association relationship of two objects emp.setname ("emp name"); s = hibernateutil.getsession (); tx = S.begintransaction (); S.save (EMP); The EMP object and depart object are saved in different order and there will be some difference s.save (depart); If an employee (EMP) is inserted after the department (depart), it generates three database statements//Because the EMP depart_id field is not inserted after both insertions, so there is a last statement to insert Tx.commit (); return depart ;} Finally{if (s!=null) {s.close ();}}} static Employee query (int empId) {Session s = null; Transaction tx = Null;try{s = Hibernateutil.getsession (); tx = S.begintransaction (); Employee EMP = (employee) s.get (Employee.class, empId); Query System.out.println (Emp.getname ()) by ID; Query Employee NameSystem.out.println (Emp.getdepart (). GetName ()); According to the employee inquires into which department tx.commit (); return EMP;} Finally{if (s!=null) {s.close ();}}}}

The basic operation of many-to-one is complete.

Therefore, hibernate can directly manipulate the relational database with object-oriented language.

"SSH Three frames" Hibernate Foundation Sixth: mapping, analysis and adding and querying of many-to-one relational relationships

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.