Hiberante Collection Mapping Samples
Collection mapping table
HBM Element | Java Interface | Java Implementation |
<set> | Set | HashSet |
<set> with order | SortedSet | TreeSet |
<list> | List | ArrayList |
<bag>, <idbag> | Collection | ArrayList |
<map> | Map | HashMap |
<map> with order | SortedMap | TreeMap |
<array>, <primitive-array> | N/A | array |
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 <set>, <bag>, <list> is similar.Defines a collection whose element type is simple data type.
<class name="SampleCompany" table="core_sample_company"><bag name="roleNames" table="core_sample_role" lazy="false" >
<key column="companyId"/>
<element column="roleName" type="string"/>
</bag></class>Query HQL: select c.id, c.name, r from SampleCompany c left join c.roleNames rDefines a collection whose element type is another mapped java class
<class name="SampleCompany" table="core_sample_company"><bag name="roles" cascade="none">
<key column="companyId"/>
<one-to-many class="SampleRole" not-found="ignore"/>
</bag></class>Query HQL: select c.id, c.name, r.name from SampleCompany c left join c.role rPay attention that key column is a foreign column of SampleRole table.Defines a list with list-index
<list> 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.<class name="SampleUser" table="core_sample_user"><list name="roles" table="core_sample_user_role" cascade="all" lazy="false" >
<key><column name="userId" sql-type="integer"/></key>
<index column="priority"></index>
<many-to-many class="SampleRole">
<column name="roleId"></column>
</many-to-many>
</list></class>The benifit of <list> 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 1This should be a hibernate bug.Defines a bag with relationship table
<class name="SampleRole" table="core_sample_role"><bag name="users" table="core_sample_user_role" cascade="none" lazy="false">
<key><column name="roleId" sql-type="integer"/></key>
<many-to-many class="SampleUser">
<column name="userId"></column>
</many-to-many>
</bag></class>
- 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)
©著作权归作者所有:来自51CTO博客作者welkin.hu的原创作品,如需转载,请与作者联系,否则将追究法律责任0
收藏
推荐专栏更多
猜你喜欢
我的友情链接 测试一下雅虎统计 Java线程:线程的调度-休眠 用光影魔术手制作一寸照片(8张一寸) Linux关闭休眠和屏保模式 Windows7删除休眠文件hiberfil.sys节省大量C盘空间 康熙对容妃的最后的心里话 《一位IT退休老兵的感言、工作、生活、前途、选择》 加入域时遇到“找不到网络路径”错误解决办法汇总 史上最全:怎样买到最便宜的机票 超级实用篇 易都市三维城市地图网址 新东方王强经典语录 Spring Boot 中 10 行代码构建 RESTful 风格应用 Java核心库实现AOP过程 RabbitMQ如何保证队列里的消息99.99%被消费? 几种简单的负载均衡算法及其Java代码实现 RabbitMQ如何保证消息99.99%被发送成功? IT兄弟连 JavaWeb教程 经典案例 在阿里架构师眼中构建一个较为通用的业务技术架构就是如此简单 Spring Boot 整合 Mybatis 的完整 Web 案例![]()
扫一扫,领取大礼包
转载于:https://blog.51cto.com/welkinhu/107234
Ctrl+Enter 发布
发布
取消