高级映射
-----------------
集合类映射
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 = "字段名"
-----------------
集合类映射
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 = "字段名"