Hiberante Collection Mapping Samples

Collection mapping table

HBM ElementJava InterfaceJava Implementation
SetHashSet
with order SortedSetTreeSet
ListArrayList
, CollectionArrayList
MapHashMap
with orderSortedMapTreeMap
, N/Aarray

Sample Tables

CREATE TABLE `core_sample_company` (
`companyId` decimal(18,0) NOT NULL,
`companyName` varchar(128) NOT NULL,
`description` varchar(1024) default NULL,
PRIMARY KEY (`companyId`)
);

CREATE TABLE `core_sample_role` (
`roleId` decimal(18,0) NOT NULL,
`roleName` varchar(128) NOT NULL,
`companyId` decimal(18,0) NOT NULL,
`description` varchar(1024) default NULL,
PRIMARY KEY (`roleId`)
);

CREATE TABLE `core_sample_user` (
`userId` decimal(18,0) NOT NULL,
`userName` varchar(128) NOT NULL,
`companyId` decimal(18,0) NOT NULL,
`defaultRoleId` decimal(18,0) default NULL,
`description` varchar(1024) default NULL,
PRIMARY KEY (`userId`)
);

CREATE TABLE `core_sample_user_role` (
`userId` decimal(18,0) NOT NULL,
`roleId` decimal(18,0) NOT NULL,
`pripority` int(11) NOT NULL,
PRIMARY KEY (`userId`,`roleId`)
);

HBM defintion

The definiton of , , is similar.

Defines a collection whose element type is simple data type.




Query HQL: select c.id, c.name, r from SampleCompany c left join c.roleNames r

Defines a collection whose element type is another mapped java class




Query HQL: select c.id, c.name, r.name from SampleCompany c left join c.role r

Pay attention that key column is a foreign column of SampleRole table.

Defines a list with list-index

is not a popular element. It request a index column in table. The index column is the index of java List, it has to be a sequence starts from 0.







The benifit of is it alwasy sorts list by index column. However, It is hard to resort the list. I tried remove a role from role list and add it to another poisition. When save the role list, an exception throwed:

java.sql.BatchUpdateException: Duplicate entry 'user001-role003 for key 1

This should be a hibernate bug.

Defines a bag with relationship table






  • Key column is foreign column from relationship table to current table(SampleRole>
  • many-to-many sub column is foreign column from relationship table to target table (SampleUser)

Technorati : ,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值