需要用到实体类Role的主键和Menu的主键结合起来作为实体类RoleMenu的主键,那么通过Hibernate具体实现如下:
RoleMenu实体类:(注意该实体类需要实现Serializable接口),且在Hibernate.cfg.xml文件中,复合主键用到的类的映射的xml文件需要放在改类映射的xml文件之前。
package com.cdms.model; import java.io.Serializable; /** * 角色权限类 * @author MY * */ public class RoleMenu implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Role role;//角色ID private MenuInfo menuInfo;//菜单id public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } public MenuInfo getMenuInfo() { return menuInfo; } public void setMenuInfo(MenuInfo menuInfo) { this.menuInfo = menuInfo; } }
RoleMenu.hbm.xml文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2016-11-24 20:04:53 by Hibernate Tools 3.5.0.Final --> <hibernate-mapping> <class name="com.cdms.model.RoleMenu" table="tb_roleMenu"> <composite-id> <key-many-to-one name="menuInfo" class="com.cdms.model.MenuInfo"> <column name="rm_menuInfoId" /> </key-many-to-one> <key-many-to-one name="role" class="com.cdms.model.Role"> <column name="rm_roleId" /> </key-many-to-one> </composite-id> </class> </hibernate-mapping>