Hibernate入門:關聯規則之多對一和一對多

來源:互聯網
上載者:User
文章目錄
  • 一對多的規則:
  • 多對一規則:

多對一規則:比如Employee和Department,多個Employee對應一個Department,稱為多對一;

一對多規則:比如Department和Employee,一個Department對應多個Employee,稱為一對多;

一對多的規則:

public class Department {private int id;private String name;private Set<Employee> emps;//此為一對多 getter setter...}

public class Employee {private int id;private String name;getter setter...}

在Department.hbm.xml中設定:

<set name="emps">        <key column="depart_id"></key><!-- 因為集合屬性不可能儲存在這張表中,需要另開一張表,所以需要指定儲存此集合的表用於串連department表的id屬性的外鍵 -->        <!-- depart_id為Employee表中需要和Department表的id相關聯的屬性 -->        <one-to-many class="Employee"/><!-- 指定一對多對應的表 --></set>

一對多規則設定以後,在Department中是沒有任何變化的,變化是在Employee中多出了一個depart_id屬性;

多對一規則:

public class Department {private int id;private String name;getter setter...}

package org.xiazdong;public class Employee {private int id;private Department depart;//此為多對一,通過Department對象引用Departmentprivate String name;getter setter...}

Employee.hbm.xml中設定:

<many-to-one name="depart" column="depart_id" /><!-- 設定在Employee中有一個depart_id和Department的主鍵相關聯-->

執行個體:

Employee.java

package org.xiazdong;public class Employee {private int id;private Department depart;//此為多對一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;}public Department getDepart() {return depart;}public void setDepart(Department depart) {this.depart = depart;}}

Department.java

package org.xiazdong;import java.util.Set;public class Department {private int id;private String name;private Set<Employee> emps;//此為一對多 public Set<Employee> getEmps() {return emps;}public void setEmps(Set<Employee> emps) {this.emps = emps;}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;}}

Employee.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"><!-- Generated 2012-5-17 10:59:47 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping package="org.xiazdong"><class name="Employee"><id name="id"><generator class="native" /></id><property name="name" /><many-to-one name="depart" column="depart_id" /></class></hibernate-mapping>

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"><!-- Generated 2012-5-17 10:59:47 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping package="org.xiazdong">    <class name="Department">        <id name="id">            <generator class="native"/>        </id>        <property name="name"/>                <set name="emps">        <key column="depart_id"></key><!-- 因為集合屬性不可能儲存在這張表中,需要另開一張表,所以需要指定儲存此集合的表用於串連department表的id屬性的外鍵 -->        <!-- depart_id為Employee表的主鍵,和Department表的id相關聯 -->        <one-to-many class="Employee"/><!-- 指定一對多對應的表 -->        </set>    </class></hibernate-mapping>

執行後表為:

Department

Employee

測試類別:

package test;import org.hibernate.Session;import org.hibernate.Transaction;import org.hibernate.util.HibernateUtil;import org.xiazdong.Department;import org.xiazdong.Employee;public class Many2OneTest {public static void main(String[] args) {add();query();}static void query(){Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();Department depart = (Department) session.get(Department.class, 6);Employee e = (Employee) session.get(Employee.class, 8);System.out.println(depart.getEmps());System.out.println(e.getDepart().getName());tx.commit();session.close();}static void add(){Department depart = new Department();depart.setName("depart");Employee e1 = new Employee();e1.setName("e1");e1.setDepart(depart);Employee e2 = new Employee();e2.setName("e2");e2.setDepart(depart);Session session = HibernateUtil.getSession();Transaction tx = session.beginTransaction();session.save(depart);session.save(e1);session.save(e2);tx.commit();session.close();}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.