多对一关系:
在一的一端配置set
<set name="users">
<key column="addressid"/>
<one-to-many class="User"/>
</set>
name必须,key column必须,one-to-many必须。
在多的一端配置many-to-one:
<many-to-one name="address" class="Address" column="addressid" ></many-to-one>
name,column必须
<property name="age" column="age" type="int"/>
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配置文件
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class>
Address.hbm.xml:
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
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:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
Address.hbm.xml:
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
(2)基于主键关联的单向一对一关联通常使用一个特定的 id 生成器,然而在这个例子中我们掉换了关联的方向:
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )
person.hbm.xml:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
</class>
address.hbm.xml:
<class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property"
>person</param>
</generator>
</id>
<one-to-one name="person" constrained="true"/>
</class>
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
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses">
<key column="personId"
not-null="true"/>
<one-to-many class="Address"/>
</set>
</class>
address.hbm.xml:
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
注:通常情况下,一对多建议使用连接表的方式来实现
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:
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
unique="true"
class="Address"/>
</set>
</class>
address.hbm.xml:
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
(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
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true">
<key column="personId"
unique="true"/>
<many-to-one name="address"
column="addressId"
not-null="true"
unique="true"/>
</join>
</class>
address.hbm.xml
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
(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 )
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>