在Hibernate中的一对多,多对一,多对多的配置

一对多
例如:一个部门(Dept)有多个雇员(Emp)
在Dept类中配置 Set emps = new hashSet();

<Set name="emps">
  <key column="deptno"></key>
  <one-to-many class="com.web.bean.Emp"></one-to-many>
</Set>

多对一
例如:多个雇员对应一个部门
在Emp类中定义Dept dept = new Dept();

<many-to-one name="dept" class="com.web.bean.Dept"  column="deptno"></many-to-one>

多对多
例如有三个表(product,orders,users)
多个产品有多个用户
在用户类中 Set products = new HashSet();
多个用户也可以购买多个产品
在产品类中 Set users = new HashSet();
在用户类的映射文件中,

<Set name="products" table="orders">
  <key column="userid"></key>
  <many-to-many class="com.web.bean.Product" column="pid"></many-to-many>

在product的映射文件中配置

<Set name="users" table="orders">
  <key column="productid"></key>
  <many-to-many class="com.web.bean.User" column="userid"></many-to-many>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值