Collection mapping table
HBM Element | Java Interface | Java Implementation |
| Set | HashSet |
| SortedSet | TreeSet |
List | ArrayList | |
, | Collection | ArrayList |
Map | HashMap | |
SortedMap | TreeMap | |
, | 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
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)