要注意此處的關係映射指的是對象之間的關係,並不是指數據庫的關係(數據庫中表與表之間只有”外鍵”的關係)。(1:1, 1:n, n:n)
(1) 1:1關係映射
l 1:1單向外鍵關聯
方法一: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表示成1:1
l 1:1雙向外鍵關聯
方法一: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屬性爲主導。
爲了更加清晰,給出類Student和StudentIdCard的屬性。
--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 將兩個類合成一張表,因此”妻子”類中就不需要加上@Entity,hibernate.hbm.xml中也不需要加入關於”妻子”類的配置<mapping class="com.yilong.hibernate.model.Wife"/>
l 既然合成一張表,那麼”丈夫”、”妻子”類中的屬性名就不能重複,否則只會被當成數據庫中同一個字段創建;
l “丈夫”類中將Wife類當成屬性,並在getWife()方法上加入註解@Embedded
@Embedded
public Wife getWife() {
return wife;
}
這樣執行出來創建出的就是Husband、Wife類合成的一張表:
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>