hibernate關聯關係映射

來源:互聯網
上載者:User

標籤:hibernate

    本文將介紹hibernate的幾種關聯關係映射:多對一、一對一、一對多、多對多。

多對一

以使用者和組為例,假設多個使用者對應一個組,使用者即為多的一端,組即為一的一端。

關鍵代碼和配置:

使用者:

public class User implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;private Group group;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Group getGroup() {return group;}public void setGroup(Group group) {this.group = group;}}
<hibernate-mapping package="com.ado.test.hibernate.po"><class name="User" table="t_user"><id name="id" column="id" type="long"><generator class="increment" /></id><property name="name" column="name" /><!-- cascade="all" fetch="join" --><many-to-one name="group" column="groupid" /></class></hibernate-mapping>

組:

public class Group implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
<hibernate-mapping package="com.ado.test.hibernate.po"><class name="Group" table="t_group"><id name="id" column="id" type="long"><generator class="increment" /></id><property name="name" column="name" /></class></hibernate-mapping>
一對一

以人和社會安全號碼為例,一個人對應一個社會安全號碼。

使用<many-to-one>實現一對一

關鍵代碼和配置:

人:

public class Person implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;private IdCard idCard;public Long getId() {return id;}public String getName() {return name;}public void setId(Long id) {this.id = id;}public void setName(String name) {this.name = name;}public IdCard getIdCard() {return idCard;}public void setIdCard(IdCard idCard) {this.idCard = idCard;}}
<hibernate-mapping package="com.ado.test.hibernate.po.one2one"><class name="Person" table="t_person1"><id name="id" column="id" type="long"><generator class="increment" /></id><property name="name" column="name" /><many-to-one name="idCard" column="cardid" /></class></hibernate-mapping>

社會安全號碼:

public class IdCard implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;public Long getId() {return id;}public String getName() {return name;}public void setId(Long id) {this.id = id;}public void setName(String name) {this.name = name;}}
<hibernate-mapping package="com.ado.test.hibernate.po.one2one"><class name="IdCard" table="t_idcard"><id name="id" column="id" type="long"><generator class="increment" /></id><property name="name" column="name" /></class></hibernate-mapping>
使用<one-to-one>實現一對一

代碼可參考上一小節內容,這裡只列出關鍵配置:

人:

<hibernate-mapping package="com.ado.test.hibernate.po.one2oneforeign"><class name="Person" table="t_person2"><id name="id" column="id" type="long"><generator class="foreign"><param name="property">idCard</param></generator></id><property name="name" column="name" /><one-to-one name="idCard" /></class></hibernate-mapping>

社會安全號碼:

<hibernate-mapping package="com.ado.test.hibernate.po.one2oneforeign"><class name="IdCard" table="t_idcard2"><id name="id" column="id" type="long"><generator class="increment" /></id><property name="name" column="name" /></class></hibernate-mapping>
一對多

以班級和學生為例,一個班級有多個學生,即班級為一的一端,學生為多的一端。

關鍵代碼和配置:

班級:

public class Classes implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;private Set<Student> students;public Long getId() {return id;}public String getName() {return name;}public void setId(Long id) {this.id = id;}public void setName(String name) {this.name = name;}public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}}
<hibernate-mapping package="com.ado.test.hibernate.po.one2many"><class name="Classes" table="t_class"><id name="id" column="id" type="long"><generator class="increment" /></id><property name="name" column="name" /><set name="students" <span style="color:#ff0000;">inverse="true"</span>><key column="classid" not-null="true" /><one-to-many class="Student" /></set></class></hibernate-mapping>

注意:上面的配置中,增加了inverse="true"屬性,可以避免儲存Classes時,產生update語句維護映射關係(update t_student)。設定該屬性後,儲存資料時,要注意順序,否則外鍵可能被設定空值。

學生:

public class Student implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;public Long getId() {return id;}public String getName() {return name;}public void setId(Long id) {this.id = id;}public void setName(String name) {this.name = name;}}
<hibernate-mapping package="com.ado.test.hibernate.po.one2many"><class name="Student" table="t_student"><id name="id" column="id" type="long"><generator class="increment" /></id><property name="name" column="name" /></class></hibernate-mapping>




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.