【hibernate】<第二节>hibernate的一对多映射(基本类型)

  所需工具与前文一致!

  第一部分内容:基本类型的一对多(one to many)

  以部门表与员工表为例:

  目录结构:

    【hibernate】<第二节>hibernate的一对多映射(基本类型)_第1张图片

  hibernate.cfg.xml内容

    

 1 
 2 configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6     
 7     
 8         
 9         com.mysql.jdbc.Driver
10         
11         
18         org.hibernate.dialect.MySQLDialect
19         
20         jdbc:mysql:///test
21         
22         root
23         
24         root
25         
26     
27         
28         true
29         
30         
31         
34         update
35         
36     
37         
38     
39 

    两个实体类:

      Department.java 

package cn.kiwifly.entity;

import java.util.HashSet;
import java.util.Set;

public class Department {

    private Integer id;
    private String name;
    private Set employees = new HashSet<>();

    public 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 getEmployees() {
        return employees;
    }

    public void setEmployees(Set employees) {
        this.employees = employees;
    }

    public String toString() {
        return "Department [id=" + id + ", name=" + name + ", employees="
                + employees + "]";
    }

}

    Employee.java

package cn.kiwifly.entity;

public class Employee {

    private Integer id;
    private String name;
    private Integer age;
    private Department department;

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public 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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age
                + ", department=" + department.getName() + "]";
    }

}

    两个实体类的配置文件:

      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">
<hibernate-mapping package="cn.kiwifly.entity">
    <class name="Department" table="t_department">
        <id name="id" column="id" type="integer">
            <generator class="native" />
        id>
        <property name="name" column="name" type="string"/>
        <set name="employees" table="t_employee" inverse="true">
            <key column="department_id" />
            <one-to-many class="Employee" />
        set>
    class>
hibernate-mapping>

           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">
<hibernate-mapping package="cn.kiwifly.entity">
    <class name="Employee" table="t_employee">
        <id name="id" column="id" type="integer">
            <generator class="native" />
        id>
        <property name="name" column="name" type="string" />
        <property name="age" column="age" type="integer" />
        <many-to-one name="department" column="department_id" class="Department" />
    class>
hibernate-mapping>

               hibernate 一对多映射格式

类型

特点

    映射格式

Set

无序,不重复

  

  

List

有序,可重复

  

  

  

Array

有序,可重复(同List)

(因不能动态增长,所以不常用)

  

  

  

    (与List基本一样)

Bag

有序,可重复

(hibernate特有对象)

  

  

      (与Set基本一样)

Map

键值对,无序,Key不可重复

  

  

  

     

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

一些细节问题:

    1、写类的toStirng()方法的时候要注意,如果只是有eclipse的快捷键复写,可能会出现栈溢出的异常,因为eclipse的复写只是写了类名,因为这里的相互引用会出现死循环

    2、以上重点在set的映射,其余的都是在其基础上映射的

    3、List与Array与Set相比多了一个list-index属性,就是这个属性来让list变的有序,这么就引出一个问题;数据库如何保证数据的有序性哪? 

      hibernate的作法:设置一个索引字段;用以保存顺序;

    4、set有两个重要的属性inverse与cascade这里没说,但很重要,有很多博客说的很详细,可以看看

 

你可能感兴趣的:(【hibernate】<第二节>hibernate的一对多映射(基本类型))