many-to-many(多对多关联)

多对多关联是常见的一种关联关系,如User与Role,一个用户可以对应多个角色,一个角色也可以对应多个用户。

要理解这个映射关系,必须了解set及many-to-many这两个标签中的相关属性。

下面以User与Role为例:

1.POJO类

User类

public class User {

	private int userId;
	private String userName;
	private Set roles;
//...set/get
}

 Role类

public class Role {

	private int roleId;
	private String roleName;
	private Set users;
//...get/set
}

 2.映射文件

User.hbm.xml

<hibernate-mapping package="org.zengge.hibernate.manytomany.pojo">
	<class name="User" table="t_user">
		<id name="userId" type="integer">
			<generator class="identity"></generator>
		</id>
		<property name="userName" type="string" ></property>
		<set name="roles" table="t_role_user">
			<key column="userId"/>
			<!-- key定义集合表到其拥有表的外键 -->
			<many-to-many class="Role" column="roleId"/>
			<!-- many-to-many 集合表到该元素的外键 -->
		</set>
	</class>
</hibernate-mapping>

 Role.hbm.xml

<hibernate-mapping package="org.zengge.hibernate.manytomany.pojo">
	<class name="Role" table="t_role">
		<id name="roleId" type="integer">
			<generator class="identity"></generator>
		</id>
		<property name="roleName" type="string" ></property>
		<set name="users" table="t_role_user">
			<key column="roleId"/>
			<!-- key中的 property-ref用于指定主非键的列作为参考外键,也就是说默认情况下会用主表的主键作为
				集合表的外键,在这里就是默认为t_role的主键作为t_role_user的外键
			 -->
			<many-to-many class="User" column="userId" />
			<!--
			 	column(必需):   这个元素的外键关键字段名  
			 	property-ref:	用于指定关联类(User)连接到外键的属性,即那个字段与集合表t_role_user中的
			 	userId关联,如果没指定就默认为关联类(User)的主键
			  -->
		</set>
		<!-- 
			使用<key>元素来申明从集合表到其拥有者类的表
			(from the collection table to the table of the owning class)的外键关键字。     
 			 <key column="column_name"/>   
   			 column(必需):外键字段的名称     
		 -->
	</class>
</hibernate-mapping>

 set用于映射集合,name表示属性名称,table表示集合对应的集合表

key:用于定义连接表中的外键,默认引用主表中的主键作外键

     column表示外键名称

     property-ref,上面有讲

 

对于key与many-to-many的作用就在于定义好集合表中的外键名称,及外键引用的是主表中的那个字段.

3.测试

导出表

public static void exportSchema(){
		SchemaExport schemaExport = new SchemaExport(configuration);
		schemaExport.create(true, true);
	}

 结果为

create table t_role (roleId integer not null auto_increment, roleName varchar(255), primary key (roleId))
create table t_role_user (userId integer not null, roleId integer not null, primary key (roleId, userId))
create table t_user (userId integer not null auto_increment, userName varchar(255), primary key (userId))
alter table t_role_user add index FK32E9F7E9217E219A (roleId), add constraint FK32E9F7E9217E219A foreign key (roleId) references t_role (roleId)
alter table t_role_user add index FK32E9F7E926D37704 (userId), add constraint FK32E9F7E926D37704 foreign key (userId) references t_user (userId)
构建了三张表,两张主表,一张中间表,并取主表主键作为中间表的外键。

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值