回顾总结2

五、一对多关系映射(回归一个班级只有多个学生)

1、  基于外键关联的单向一对多关联

<set name=”students”>

           <key column=”clazz_id” not-null=”true”/>

           <one-to-many class=”Student”/>

</set>

2、  基于连接表的单向一对多关联 应该优先被采用。请注意,通过指定unique="true",我们可以把多样性从多对多改变为一对多。

<set name=”students” table=”t_studentclazz”>

           <key column=”clazz_id”/>

           <many-to-many column=”student_id” unique=”true” class=”clazz”/>

</set>

六、单向多对多关联

1基于连接表的单向多对多关联,修改上述一对多去掉unique=”true”即可

七、双向关联

1、  一对多(one to many) / 多对一(many to one),双向多对一关联是最常见的关联关系。(这也是标准的父/子关联关系)

<class name=”student”>

           <id name=”id” column=”student_id”>

                    <generator class=”native”/>

           </id>

           <many-to-one name=”clazz” column=”clazz_id” not-null=”true”/>

</class

 

<class name=”clazz”>

           <id name=”id” column=”clazz_id”>

                    <generator class=”native”/>

           </id>

           <set name=”students” inverse=”true”>

                    <key column=”clazz_id”/>

                    <one-to-many class=”Student”/>

           </set>

</class>

如果你使用List(或者其他有序集合类),你需要设置外键对应的key列为 not null,Hibernate来从集合端管理关联,维护每个元素的索引(通过设置update="false" and insert="false"来对另一端反向操作)。假若集合映射的<key>元素对应的底层外键字段是NOT NULL的,那么为这一key元素定义not-null="true"是很重要的。不要仅仅为可能的嵌套<column>元素定义not-null="true"<key>元素也是需要的。

2、 一对一(one to one

(1)、外键关联的双向一对一关联

<class name=”student”>

           <id name=”id” column=”student_id”>

                    <generator class=”native”/>

           </id>

           <many-to-one name=”clazz” column=”clazz_id” unique=”true” not-null=”true”/>

</class>

 

<class name=”clazz”>

           <id name=”id” column=”clazz_id”>

                    <generator class=”native”/>

           </id>

           <one-to-one name=”student” property-ref=”clazz”/>

</class>

(2)、基于主键关联的一对一关联需要使用特定的id生成器。

<class name=”student”>

           <id name=”id” column=”student_id”>

                    <generator class=”native”/>

           </id>

           <one-to-one name=”clazz” />

</class>

 

<class name=”clazz”>

           <id name=”id” column=”clazz_id”>

                    <generator class=”foreign”>

                             <param name=”property”>student</param>

                    </generator>

           </id>

           <one-to-one name=”student” constrained=”true”/>

</class>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值