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`)
);HBM defintionCREATE 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`)
);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)
提问和评论都可以,用心的回复会被更多人看到 评论发布评论 相关文章
Hiberante DetachedCriteria用法
java List 业务层 User Hiberante 架构 hibernate框架优点hibernate一.优点: 1.Hibernate是一种ORM持久性框架,它是对jdbc做了轻量级的封装,使我们可以用面向对象的思维对数据库进行操作 2.支持各种关系数据库,有很好的级联功能很好的支持着表与表之间的关系. 3.与spring整合之后连接数据库的代码和控制事物的代码都统一,由spring管理,简化了代码,方便我们更好的管理事务; 4.hibernate提供一级和二级缓存的功能,极大
Hiberante 架构 Hibernate整理 hibernate简单整理 hibernate概念 hibernate举报文章
请选择举报类型
内容侵权 涉嫌营销 内容抄袭 违法信息 其他补充说明
0/200
上传截图
格式支持JPEG/PNG/JPG,图片不超过1.9M
如有误判或任何疑问,可联系 「小助手微信:cto51cto」申诉及反馈。我知道了