hibernate多对多的问题

来源:百度知道 编辑:UC知道 时间:2024/06/23 23:46:56
MyEclipse怎样生成many-to-many关系
我现在建好了3张表
表1.商品表
表2.订单表(描述此订单的收件人、地址、电话等.....)
表3.订单包含的商品 表(2个字段,分别是订单ID和商品ID,如果此订单包含3个商品,就有3条数据)

请问这种情况,表3是用many-to-many关系吧,MyEclipse要怎么自动生成这种关系,我不想手写

不知道有没有这个功能诶,你用MyEclipse先自动建立表1表2的映射关系,然后再手动添加many-to-many关系,也很方便,只要在两张表里分别加入:
<set name="goodses" table="goods_order" lazy="true" inverse="true">
<key>
<column name="order_id" not-null="true"/>
</key>
<many-to-many class="Goods">
<column name="goods_id" not-null="true"/>
</many-to-many>
</set>

<set name="orders" table="goods_order" lazy="true">
<key>
<column name="goods_id" not-null="true"/>
</key>
<many-to-many class="Orders">
<column name="order_id" not-null="true"/>
</many-to-many>
</set>

记住inverse="true"只需要设一个表,这是建立双向关联用的,一张表会通过另一张表获得子节点的引用

有种方法是:
先设计好数据库。用插件根据数据库表的结构生成hibernate的文件。