多对多单向关联
使用多对多关联,需要借助一个起中介作用的连接表完成。一个多对多的关联通常可以分拆成两个一对多的关联
<set name="product" table="orderitem"> <key column="order_id"></key> <many-to-many class="com.rbh.examples.Product" column="product_id"/> </set>
多对多双向关联
MyEclipse支持多对多双向关联可以自动生成
在多对多关联,可以拆分成两个一对多的关联
<set name="orderItems" inverse="true" cascade="save-update"> <key> <column name="ORDER_ID" precision="8" scale="0" /> </key> <one-to-many class="com.rbh.examples.OrderItem" /> </set>
<!--需要把生成的set集合删除-->
<!--只保留主键属性其他的删除--> <hibernate-mapping> <class name="com.rbh.examples.OrderItem" table="ORDERITEM" schema="SCOTT"> <composite-id> <key-many-to-one name="order" class="com.rbh.examples.Order"> <column name="ORDER_ID" precision="8" scale="0" /> </key-many-to-one> <key-many-to-one name="product" class="com.rbh.examples.Product"> <column name="PRODUCT_ID" precision="8" scale="0" /> </key-many-to-one> <key-property name="quantity" type="java.lang.Long"> <column name="QUANTITY" precision="8" scale="0" /> </key-property> <key-property name="purchasePrice" type="java.lang.Double"> <column name="PURCHASE_PRICE" precision="8" /> </key-property> </composite-id> </class> </hibernate-mapping>