Hibernate学习笔记7

本文深入探讨了Hibernate中的三种核心映射技术:Component映射、Composite映射及Collection映射。Component映射实现了对象模型的细粒度划分,提高了复用率;Composite映射用于复合主键场景;Collection映射则涵盖了Set、List、Array和Map等集合类型的映射方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Hibernate学习笔记7

学习课程:

Component映射——组建映射

Hibernate_15_Compontent_Mapping

Composite映射——复合主键映射

Hibernate_16_Composite_Mapping

Collection映射——集合映射

Hibernate_17_Collection_Mapping

学习内容:

Component映射——组建映射

对象模型:

Component映射——组件映射

称为值,对象实体的逻辑组成部分,它与实体的根本区别是没有OID

优点:实现了对象模型的细粒度划分,层次会更分明,复用率更高

Contact: (String)email , (String)address , (String)zipCode , (String)contactTel

User:(int)id , (String)name , (Contact)contact

Employee: (int)id , (String)name , (Contact)contact

映射文件:

  1. <class name="User" table="t_user" lazy="true">
  2. …………
  3. …………
  4. <component name="contact" >
  5. <property name="email" />
  6. <property name="address" />
  7. <property name="zipCode" />
  8. <property name="contactTel" />
  9. </component>
  10. </class>

Composite映射——复合主键映射

对象模型:

Component映射——组件映射

称为值,对象实体的逻辑组成部分,它与实体的根本区别是没有OID

优点:实现了对象模型的细粒度划分,层次会更分明,复用率更高

Contact: (String)email , (String)address , (String)zipCode , (String)contactTel

User:(int)id , (String)name , (Contact)contact

Employee: (int)id , (String)name , (Contact)contact

映射文件:

  1. <class name="Child" table="t_child" lazy="true">
  2. <composite-id name="father">
  3. <key-property name="year" />
  4. <key-property name="month" />
  5. <key-property name="day" />
  6. </composite-id>
  7. <property name="name" />
  8. </class>

Collection映射——集合映射

映射:

Set setValue

List listValue

array String[] arrayValue

Map mapValue

建立五张表

第一张表t_collection

id

name

第二张表 t_set_value

字段信息:

set_id(外键关联于id) 

set_value

映射文件:

  1. <set name="setValue" table="t_set_value">
  2. <key column="set_id"/>
  3. <element type="string" column="set_value"/>
  4. </set>

第三张表 t_list_value,list保存时需要保存索引顺序,不可以打乱

字段信息:

list_id(外键关联于id)

list_value

list_index

映射文件

  1. <list name="listValue" table="t_list_value">
  2. <key column="list_id"/>
  3. <list-index column="list_index"/>
  4. <element type="string" column="list_value"/>
  5. </list>

第四张表 t_array_value,注需索引

字段信息:

array_id

array_value

array_index

映射文件:

  1. <array name="arrayValue" table="t_array_value">
  2. <key column="array_id" />
  3. <list-index column="array_index"/>
  4. <element type="string" column="array_value"/>
  5. </array>

第五张表:t_map_value key和value

字段信息:

map_id

map_key 

map_value

映射文件:

  1. <map name="mapValue" table="t_map_value">
  2. <key column="map_id"/>
  3. <map-key type="string" column="map_key"/>
  4. <element type="string" column="map_value"/>
  5. </map>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值