hibernate中對象集合的儲存

來源:互聯網
上載者:User

標籤:tle   display   技術分享   java web   多個   builds   一對多   actor   集合   

 

一、在java web設計中經常使用對象進行操作,在hibernate中對象集合的儲存(一對多)

1需要進行如下步驟:

1) 設計資料表關係

2)引入jar包,需要注意引入資料庫connector

3)寫實體類

4)配置對應檔和hibernate.cfg.xml

 

2例子如下:

員工和部門的關係,多個員工屬於一個部門,一個部門有多個員工。可以在員工表上設定一個外鍵解決一對多的關係。

2.1)建立表

CREATE TABLE employee(id INT PRIMARY KEY AUTO_INCREMENT,ename VARCHAR(50) NOT NULL,depid INT,FOREIGN KEY (depid) REFERENCES dept(id) ON DELETE CASCADE);CREATE TABLE dept(id INT PRIMARY KEY AUTO_INCREMENT,dname VARCHAR(100));

2.2)引入jar包

 

 2.3)寫實體類

員工類

public class Employee {    private int id;    private String ename;    private Dept dept = new Dept();    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getEname() {        return ename;    }    public void setEname(String ename) {        this.ename = ename;    }    public Dept getDept() {        return dept;    }    public void setDept(Dept dept) {        this.dept = dept;    }        }
View Code

部門類

public class Dept {    private int id;    private String dname;    private Set<Employee> employees = new HashSet<Employee>();    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getDname() {        return dname;    }    public void setDname(String dname) {        this.dname = dname;    }    public Set<Employee> getEmployees() {        return employees;    }    public void setEmployees(Set<Employee> employees) {        this.employees = employees;    }}
View Code

 

2.4)配置對應檔

部門對應檔,dept.hbm.xml

<hibernate-mapping     package="com.baidu.entity">    <class name="Dept" table="dept">        <id name="id" column="id">            <generator class="native"/>        </id>        <property name="dname" column="dname"></property>                <set name="employees" table="emploee">            <key column="depid"></key>            <one-to-many class="Employee"/>        </set>    </class></hibernate-mapping>

注意:在配置一對多的時候,部門對員工是一對多,要配置的內容set

1)集合屬性是哪一個,employees

2)該集合屬性儲存區對象對應的表,employee

3)該表的外鍵,depid

4)集合中儲存物件的類型,class=Employee

 

員工對應檔,employee.hbm.xml

<hibernate-mapping     package="com.baidu.entity">    <class name="Employee" table="employee">        <id name="id" column="id">            <generator class="native"/>        </id>                <property name="ename" column="ename"></property>        <many-to-one name="dept" class="Dept" column="depid"></many-to-one>    </class></hibernate-mapping>

注意:在配置多對一的時候,員工對部門是多對一。要配置的內容many-to-one

1)哪個對象要映射

2)該表哪個外鍵與對象關聯

3)映射對象的類型

 

全域配置hibernate.cfg.xml,可以查看之前部落格hibernate開發流程

 

3 簡單測試

public void fun1(){        Configuration configuration = new Configuration();        configuration.configure();                SessionFactory sessionFac = configuration.buildSessionFactory();        Session session = sessionFac.openSession();        Transaction bt = session.beginTransaction();                Dept dept = new Dept();        dept.setDname("test");        Employee employee = new Employee();        employee.setEname("test3");        employee.setDept(dept);                        Employee employee2 = new Employee();        employee2.setEname("test4");        employee2.setDept(dept);                session.save(employee);        session.save(employee2);        session.save(dept);                bt.commit();                session.close();        sessionFac.close();            }

 

 

 需要說明的是

在表中是通過外鍵來表達一對多或多對一的關係;在類中是通過對象集合來表達,例如set/list/map;在設定檔中需要注意set/list/map的配置

哪一張表有外鍵就操作該表對應那個對象進行添加另外的對象,不然會出問題。

在hibernate中想要將一個對象映射成一個外鍵欄位,只有many-to-one

 

hibernate中對象集合的儲存

聯繫我們

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