单向关联(Unidirectional association)

1.many-to-one:最常见的单向关联关系

<class name="Person">
	<id name = "id" column="personId">
		<generator class="native"/>
	</id>
	<many-to-one name="Address"
		column="addressId"
		not-null="true"/>
</class>

<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
</class>

2.one-to-one:有两种

(1)基于外键关联的一对一关联

<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<many-to-one name="address"
		column="adddressId"
		unique="true"
		not-null="true"/>
</class>

<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
</class>
(2)基于主键关联的一对一关联

<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
</class>


<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:不常使用,也不推荐使用

<class name="Person">
	<id name="id" column="personId">
		<generator class="native"/>
	</id>
	<set column="addresses">
		<key column="personId" not-null="true"/>
		<one-to-many class="Address"/>
	</set>
</class>
<class name="Address">
	<id name="id" column="addressId">
		<generator class="native"/>
	</id>
</class>

你可能感兴趣的:(单向关联(Unidirectional association))