hibernate的關係映射(1)

要注意此處的關係映射指的是對象之間的關係,並不是指數據庫的關係(數據庫中表與表之間只有外鍵的關係)(1:1, 1:n, n:n)

   (1) 1:1關係映射

l  11單向外鍵關聯

方法一:annotation

涉及兩個註解:@OneToOne@JoinColumn

以丈夫、妻子關聯爲例(會自動將妻子表的主鍵當成外鍵),例如:

@OneToOne

    @JoinColumn(name="wifeId")

    public Wife getWife() {

       return wife

}

public void setWife(Wife wife) {

           this.wife = wife;

}

創建出的兩張表分別如下:

create table Husband (id integer not null auto_increment, name

varchar(255), wifeId integer, primary key (id))

create table Wife (id integer not null auto_increment, name varchar(255),

primary key (id))

alter table Husband add index FKAEEA401B389C8730 (wife_id), add constraint FKAEEA401B389C8730 foreign key (wifeId) references Wife (id)

    方法二:xml配置

在要參考的類上對應的Class.hbm.xml文件配置上加上<many-to-one>標籤,下面以學生-學生證爲例:

<hibernate-mapping>

    <class name=”com.yilong.hibernate.StuIdCard”>

       <id name=”id”>

           <generator class=”native”></generator>

       </id>

       <property name=”num”/>

       <many-to-one name=”student” column=”studentId” unique=”true”>

    </class>

</hibernate-mapping>

    說明:其中name=”student”中的student爲類StuIdCard上的其中的一個屬性:

Student student;表示將以類Student裏的主鍵爲外鍵。column爲外鍵在表中的字段名;unique=”true”將把many-to-one表示成11

l  11雙向外鍵關聯

方法一:annotation

還是以上面的爲例,雙向外鍵關聯只需要在妻子類的丈夫屬性上加上@OneToOne

的註解,並說明以丈夫類的外鍵爲主導即可。雙向關聯中,mappedBy屬性都應該設置。

    @OneToOne(mappedBy="wife")

    public Husband getHusband() {

       return husband;

    }

    public void setHusband(Husband husband) {

       this.husband = husband;

    }

    方法二:xml配置

    還是以學生-學生證爲例,只需要在上面的基礎上,在Student.hbm.xml文件中設置:

    <hibernate-mapping package="com.yilong.hibernate.model">

    <class name="Student" table="_student">

       <id name="id" column="id"></id>

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

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

      

       <one-to-one name="stuid" property-ref="student"></one-to-one>

    </class>

</hibernate-mapping>

說明:其中name=”stuid”含義爲:類Student參考StudentIdCard,然後peoperty-ref=”student”則是以StudentIdCard裏的Student student屬性爲主導。

爲了更加清晰,給出類StudentStudentIdCard的屬性。

--Student類裏面的屬性:

private int id;

    private String name;

    private int age;

private StudentIdCard stuid;

--StudentIdCard類裏面的屬性:

private int id;

private Student student;

小結:雙向外鍵關聯必須設置以對方爲主導這一特性

ü  Annotation @OneToOne(mappedBy="wife")

ü  XML配置

<one-to-one name="stuid" property-ref="student"></one-to-one>

    注意:單向映射和雙向映射在數據庫中沒什麼區別,區別主要表現在程序中,單向映射只能從一方找到另一方,而雙向則可以互相找到。

l  1:1單向、雙向主鍵關聯(主鍵關聯是指雙方的主鍵一致,用來關聯)

(2) 組件映射

即將其中一個類的屬性加到另一個類屬性中合成一張表。

方法一:annotation

以上述的丈夫-妻子爲例,需要注意的方面有:

l  丈夫類中已有id作爲主鍵,因此妻子類不需要id了;

l  將兩個類合成一張表,因此妻子類中就不需要加上@Entityhibernate.hbm.xml中也不需要加入關於妻子類的配置<mapping class="com.yilong.hibernate.model.Wife"/>

l  既然合成一張表,那麼丈夫妻子類中的屬性名就不能重複,否則只會被當成數據庫中同一個字段創建;

l  丈夫類中將Wife類當成屬性,並在getWife()方法上加入註解@Embedded

@Embedded

    public Wife getWife() {

       return wife;

    }

    這樣執行出來創建出的就是HusbandWife類合成的一張表:

create table Husband_Wife (id integer not null auto_increment, name

varchar(255), wifeAge integer not null, wifeName varchar(255),

primary key (id))

方法二:xml配置

<component name=”wife”>

    <property name=”wifename”></property>

    <property name=”age”></property>

</component>

    (3) n:1 單向關聯

   方法一:annotation

    n:1含義:n那方的類擁有1那方類的實例對象作爲屬性。

思路:在n的一方加入1那方的id

    需要設置的內容:

l  n那方的類中加入1那方的類作爲屬性;

l  添加@ManyToOne註解;

下面以-成員模式爲例(一個組包含若干位成員)

@Entity

@Table(name="_user")

public class User {

   

    private int id;

    private String name;

    private Group group;

   

    @ManyToOne

    @JoinColumn(name="groupId")

    public Group getGroup() {

        return group;

    }

    public void setGroup(Group group) {

        this.group = group;

    }

    @Id

    @GeneratedValue

    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;

    }

}

方法二:使用xml配置

作爲1的一方的類.hbm.xml不需要改變什麼

<hibernate-mapping package="com.yilong.hibernate.model">

    <class name="Group" table="_group">

       <id name="id" column="id">

           <generator class="native"></generator>   

       </id>

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

    </class>

</hibernate-mapping>

    作爲n的那方的類.hbm.xml則需要添加<many-to-one>標籤

<hibernate-mapping package="com.yilong.hibernate.model">

    <class name="User" table="_user">

       <id name="id" column="id">

           <generator class="native"></generator>   

       </id>

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

       <many-to-one name="group" column="gruopId"></many-to-one>

    </class>

</hibernate-mapping>

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章