hibernate 学习笔记三

高级映射
-----------------
集合类映射
Set
表上:
table item table image
id<PK> item_id<FK><PK>
name filename<PK>
类上:
class Item
id
name
Set
映射文件上:
Item.hbm.xml
<set name = "images" table = "hedong_image">
<key column = "item_id"/>
<element type = "string" column = "filename" not-null = "true"/>
</set>
lazy
可以设置Hibernate关联表中信息的加载策略
true:采用延迟加载
false:不采用延迟加载
如果采用延迟加载,在session关闭后还想进行操作可以使用Hibernate.initialize()进行预加载
Bag
表上:
table item table image
id<PK> id<PK>
name filename
item_id<FK>
类上:
class Item
id
name
List
映射文件上:
Item.hbm.xml
<idbag name="images" table = "hedong_image_bag" >
<collection-id type="int" column="id">
<generator class="increment"></generator>
</collection-id>
<key column = "item_id"></key>
<element type="string" column = "filename"></element>
</idbag>
List
表上:
table item table image
id<PK> item_id<FK,PK>
name filename
position<PK>

类上:
class Item
id
name
List
映射文件上:
Item.hbm.xml
<list name = "images" table = "hedong_image_list>
<key column = "item_id"/>
<index column = "position"/>
<element type = "string" column = "filename"/>
</list>
Map
表上:
table item table image
id<PK> item_id<FK,PK>
image_name<PK>
name filename


类上:
class Item
id
name
Map
映射文件上:
Item.hbm.xml
<map name="images" table = "hedong_image_map" order-by = "image_name">
<key column = "item_id"></key>
<index column = "image_name" type = "string"/>
<element type="string" column = "filename"></element>
</map>

组件映射(component)
有关联关系的多个POJO类映射到一张表上

继承映射

是否支持多态 更新维护是否方便 出具报表是否方便
每个子类一张表 No No Yes

整个类层次一张表 Yes No Yes

每个类一张表 Yes Yes No
1、每个子类一张表
表上:
create table hedong_cash_payment(
payment_id number(7) primary key,
amout number
);
create table hedong_credit_payment(
payment_id number(7) primary keym
amout number,
cardtype varchar2(20)
);
create table hedong_cheque_payment(
payment_id number(7) primary key,
amout number,
chequeno number
);
类上:
Payment CashPayment CreditPayment ChequePayment
映射文件上:
com.briup.sixth.concrete.Payment.hbm.xml
2、整个类层次一张表
表上:
create table hedong_payment(
payment_id number(7) primary key,
amount number,
cardtype varchar2(20),
chequeno number,
paytype varchar2(10)
);
类上:
Payment CashPayment CreditPayment ChequePayment
映射文件上:
com.briup.sixth.hierarchy.Payment.hbm.xml

Hibernate对集合元素排序
1、sort:在内存中排序
sort = "natural":按ASCII码值排序
2、order-by:在数据库中排序
order-by = "字段名"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值