多對一關係:
在一的一端配置set
name必須,key column必須,one-to-many必須。
在多的一端配置many-to-one:
name,column必須
property:當column不寫是,預設列名與name相同。type也可省略,由hibernate自動匹配
1:單向 many-to-one 關聯是最常見的單向關聯關係。
對於單向映,假如從person端去維護關聯關係。則只需做如下配置:
表結構:
create table person ( personid bigint not null primary key, addressid bigint not null )
create table address ( addressid bigint not null primary key )
person.hbm.xml配置檔案
address.hbm.xml:
2: 一對一(one-to-one)
(1)基於外來鍵關聯的單向一對一關聯和單向多對一關聯幾乎是一樣的。唯一的不同就是單向一對一關聯中的外來鍵字段具有唯一性約束。
create table person ( personid bigint not null primary key, addressid bigint not null unique )
create table address ( addressid bigint not null primary key )
person.hbm.xml:
address.hbm.xml:
(2)基於主鍵關聯的單向一對一關聯通常使用乙個特定的 id 生成器,然而在這個例子中我們掉換了關聯的方向:
create table person ( personid bigint not null primary key )
create table address ( personid bigint not null primary key )
person.hbm.xml:
address.hbm.xml:
person
3:一對多(one-to-many)
基於外來鍵關聯的單向一對多關聯是一種很少見的情況,我們不推薦使用它。
create table person ( personid bigint not null primary key )
create table address ( addressid bigint not null primary key, personid bigint not null )
person.hbm.xml
address.hbm.xml:
注:通常情況下,一對多建議使用連線表的方式來實現
4:使用連線表的單向關聯(unidirectional associations with join tables)
(1):一對多(one-to-many)
基於連線表的單向一對多關聯 應該優先被採用。請注意,通過指定unique="true",我們可以把多樣性從多對多改變為一對多。
create table person ( personid bigint not null primary key )
create table personaddress ( personid not null, addressid bigint not null primary key )
create table address ( addressid bigint not null primary key )
person.hbm.xml:
address.hbm.xml:
(2)一對一(one-to-one)基於連線表的單向一對一關聯也是可行的,但非常少見。
create table person ( personid bigint not null primary key )
create table personaddress ( personid bigint not null primary key, addressid bigint not null unique )
create table address ( addressid bigint not null primary key )
person.hbm.xml
address.hbm.xml
(3):多對多(many-to-many)這裡是乙個單向多對多關聯的例子。
create table person ( personid bigint not null primary key )
create table personaddress ( personid bigint not null, addressid bigint not null, primary key (personid, addressid) )
create table address ( addressid bigint not null primary key )
Hibernate對映檔案之單向關聯關係對映
person類 如下 具體參見各類對映關係 address類 如下 public class address implements serializable1 單向n 1關聯person類 如下 public class person1 基於外來鍵的單向n 1關聯系統會在n的一端即person端對映...
hibernate 關係對映
color red hibernate 多對一對映 color 關聯對映的本質 將關聯關係對映到資料庫,關聯關係在物件模型域中體現為乙個或多個引用 標籤會在 多 的一端新增乙個外來鍵,指向 一 的一端,這個外來鍵是由 中的column的屬性定義的,如果忽略這個屬性,預設建立的外來鍵與實體類的屬性名相...
Hibernate 對映關係
對映關係通俗點來說 address實體類 不用配置 user實體類 編寫配置 public class user student實體類 不用配置 班級實體類 編寫配置 public class clazz.student實體類 不用配置 課程實體類 編寫配置 public class course....