hibernate一對多的實現

來源:互聯網
上載者:User

昨天我們講了多對一關聯性。今天我們就講講一對多的實現。

既然多個員工屬於一個部門。

那麼我們也可以說一個部門有多個員工。

 

首先我想說,不管是多對一還是一對多,他們的共同點就是兩張表有聯絡。實現的實質就是通過找共同屬性的欄位(可以不同名字,但是在關聯時候,類型是一致的)。

如果是員工表和部門表的話,那麼我們可以通過部門編號來實現。

 

下面我寫一個尋找一個部門有多少人執行個體來說明1對多的實現方式。

1.      部門對象映射類

package com.fish.testdao;

 

import java.util.Set;

 

public
class
Department{

private Integer
id;

private String
name;

private Set<Employee>
employee;//利用這個我們可以將一個部門的所有員工放到這個集合,到時候我們只要遍曆這個集合我們就知道一個部門都有誰了。

 

public Set<Employee> getEmployee() {

    return
employee;

}

public
void
setEmployee(Set<Employee> employee) {

    this.employee = employee;

}

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;

}

 

}

 

2.      員工對象映射類

 

package com.fish.testdao;

 

public
class
Employee {

private Integer
id;

private String
name;

private 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 Department getDepartment() {

    return
department;

}

public
void
setDepartment(Department department) {

    this.department = department;

}

 

}

3.      部門xml

<?xml
version="1.0"
encoding="UTF-8"?>

<!DOCTYPE
hibernate-mapping PUBLIC

    "-//Hibernate/HibernateMapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

    <class
name="com.fish.testdao.Department">

       <id
name="id"
type="integer">

           <generator
class="increment"></generator>

       </id>

       <property
name="name"></property>

       <set
name="employee">

           <key
column="department"></key>

           <one-to-many
class="com.fish.testdao.Employee"/>

       </set>

    </class>

</hibernate-mapping>    

*注<set name="employee">說明這個集合要封裝的是部門類中的set屬性叫employee和類的屬性是一致的

           <keycolumn="department"></key> //關聯的是員工表的這個屬性

           <one-to-manyclass="com.fish.testdao.Employee" />  //操作的是員工表

       </set>

 

4.       員工xml

<?xml
version="1.0"
encoding="UTF-8"?>

<!DOCTYPE
hibernate-mapping PUBLIC

    "-//Hibernate/HibernateMapping DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>

    <class
name="com.fish.testdao.Employee"
>

       <id
name="id"
column="id"type="integer">

           <generator
class="increment"></generator>

       </id>

       <property
name="name"
></property>

       <many-to-one
name="department"  
></many-to-one>

    </class>

</hibernate-mapping>

 

 

我們寫一個測試類別

package com.fish.domain;

 

import java.util.List;

 

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

import com.fish.testdao.Department;

import com.fish.testdao.Employee;

 

public
class
Test2 {

    //寫一個得到session的模板

    public
static
Session getMySession() {

       Configuration configuration = new Configuration();

       configuration.configure("hibernate.cfg.xml");

       SessionFactory factory = configuration.buildSessionFactory();

       Session session = factory.openSession();

       return session;

    }

//這是一個對部門有多少員工的尋找

    public
static void
query() {

       String hql = "fromDepartment";

       Session session = getMySession();

       Query query = session.createQuery(hql);

       List<Department> list = query.list();

       for (Department i : list) {  

           System.out.println(i.getName()+"有");

              for(Employee j:i.getEmployee()){

              System.out.print( j.getName());

              }

              System.out.println("");

              System.out.println("");

       }

       }

    public
static void
main(String[] args) {

       query();

}

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.