set的使用
<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>
<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>
关联表的使用
<join table="PersonAddress" optional="true">
<key column="personId" unique="true"/>
<many-to-one name="address"
column="addressId"
not-null="true"
cascade="all"
.../>
</join>
//set,Array,bag,map都行
<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>
<map name="events" table="event">
<key column="PERSON_ID"/>
<index column="name" type="string"/>
<element column="content" type="string"/>
</map>
<many-to-one name="address"
class="com.jason.associations.one2one.unilateralism.foreign.Address"
unique="true"
column="addressId"
cascade="all"
not-null="true"/>
<one-to-one name="person"
class="com.jason.associations.one2one.unilateralism.primary.Person"
constrained="true"/>
1、都表示一对一 ,many-to-one指定了对应的column(和column相关的属性cascade,not-null...)
而one-to-one没有对应的column,它主要用于id的generator class="foreign"中
2、同样也适用于<many-to-many unique="true">和<one-to-many>
<one-to-many>
<many-to-one>
不管哪个,对应的表是这样的,把one那张表的key,放到many对应的表中,相当于外键。
<one-to-?>
<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>
<id name="id" column="addressId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person"
class="com.jason.associations.one2one.unilateralism.primary.Person"
constrained="true"/>
<one-to-many>和<one-to-one>都没有column属性,因为one对应的那边不需要相应的列,
那么如何保持关系呢?
1、最好不使用,
2、向上面那样指定一下
column的位置
1、大多数时候在这个class对应的表中
2、在关联表中
3、在指定的class对应的表中
例如:
<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>
unique
<many-to-?
unique="true"
..."/>
unique是相对于前面的many
inverse
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<join table="PersonAddress"
inverse="true"
optional="true">
<key column="addressId"/>
<many-to-one name="person"
column="personId"
not-null="true"/>
</join>
</class>
1、为了更好地提高执行效率,需要将“多”的一端设为主控方,就是将关联关系的维护工作交给“多”的一端。inverse="true" 说明主控表是Person表(Address表对面)inverse="false"本身,默认值:false
2、inverse="true"可以出现在关联的任意一端,即collection端或者join端。
Constrained : 约束,表明主控表的主键上是否存在一个外键(foreigh key)对其进行约束。
property-ref:关联类中用于与主控类相关联的属性名,默认为关联类的主键属性名
lazy=false:被动方的记录由hibernate负责记取,之后存放在主控方指定的 Collection类型属性中
batch-size:采用延迟加载特征时,一次读入的数据数昨