二、单向关系
1.“单向一对多关系”
基于外键关联的单向一对多关联是一种很少见的情况,并不推荐使用
<class name="com.jason.associations.one2many.unilateralism.one2many.Person" table="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" cascade="all">
<!--key 是Address表的字段,最后要update Address表.personId = Person表.personId-->
<key column="personId" not-null="true"/>
<one-to-many
class="com.jason.associations.one2many.unilateralism.one2many.Address"/>
</set>
<property name="name" column="personName" type="string" />
</class>
<class name="com.jason.associations.one2many.unilateralism.one2many.Address" table="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<property name="country" column="country" type="string" />
<property name="city" column="city" type="string" />
</class>
<!--
create table Person ( personId bigint not null primary key, personName varchar(20))
create table Address ( addressId bigint not null primary key, personId bigint not null, country varchar(20), city varchar(20) )
-->
2.“单向多对一关系”
<class name="com.jason.associations.one2many.unilateralism.many2one.Person" table="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
class="com.jason.associations.one2many.unilateralism.many2one.Address"
column="addressId"
cascade="all"
outer-join="auto"
update="true"
insert="true"
access="property"
not-null="true"/>
<property name="name" column="personName" type="string" />
</class>
<class name="com.jason.associations.one2many.unilateralism.many2one.Address" table="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<property name="country" column="country" type="string" />
<property name="city" column="city" type="string" />
</class>
<!--
create table Person ( personId bigint not null primary key, addressId bigint not null, personName varchar(20))
create table Address ( addressId bigint not null primary key, country varchar(20), city varchar(20) )
-->
3、“单向一对多连接表关系”
<hibernate-mapping>
<class name="com.jason.associations.one2many.unilateralism.one2many.table.Person" table="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress" cascade="all">
<key column="personId"/>
<many-to-many column="addressId"
unique="true"
class="com.jason.associations.one2many.unilateralism.one2many.table.Address"/>
</set>
<property name="name" column="personName" type="string" />
</class>
<class name="com.jason.associations.one2many.unilateralism.one2many.table.Address" table="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<property name="country" column="country" type="string" />
<property name="city" column="city" type="string" />
</class>
<!--
create table Person ( personId bigint not null primary key, personName varchar(20))
create table PersonAddress ( personId bigint not null, addressId bigint not null primary key)
create table Address ( addressId bigint not null primary key, country varchar(20), city varchar(20))
-->
4、“单向多对一连接表关系”
我们认为对于这种关联关系最好使用连接表。
<class name="com.jason.associations.one2many.unilateralism.many2one.table.Person" table="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<property name="name" column="personName" type="string" />
<!--join 必须放在最下面-->
<join table="PersonAddress" optional="true">
<key column="personId" unique="true"/>
<many-to-one name="address"
class="com.jason.associations.one2many.unilateralism.many2one.table.Address"
column="addressId"
not-null="true"
cascade="all"/>
</join>
</class>
<class name="com.jason.associations.one2many.unilateralism.many2one.table.Address" table="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<property name="country" column="country" type="string" />
<property name="city" column="city" type="string" />
</class>
<!--
create table Person ( personId bigint not null primary key, personName varchar(20))
create table PersonAddress ( personId bigint not null primary key, addressId bigint not null)
create table Address ( addressId bigint not null primary key, country varchar(20), city varchar(20))
-->
二、双向关系
双向一对多关系就是二者的组合
为了更好地提高执行效率,需要将“多”的一端设为主控方,就是将关联关系的维护工作交给“多”的一端。