Hibernate级联实践之三(many-to-many)
一。mapping
使用三个表,分别为TBL_OSU_PRODUCT_I、TBL_OSU_BUSINESSFUNCTION_I和TBL_OSU_PRODUCTBUSINESS_C,1和2为多对多关系,3为其中间表
1.TBL_OSU_PRODUCT_I
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<classname="com.netqin.function.osuproduct.product.model.OsuProduct"table="TBL_OSU_PRODUCT_I"schema="OSU_WORK">
<idname="id"type="long">
<columnname="PRDU_ID"precision="10"scale="0"/>
<generatorclass="sequence">
<paramname="sequence">SEQ_OSU_PRODUCT_I</param>
</generator>
</id>
<propertyname="prduName"type="string">
<columnname="PRDU_NAME"length="200"not-null="true"/>
</property>
<propertyname="prduDescription"type="string">
<columnname="PRDU_DESCRIPTION"length="200"/>
</property>
<setname="business"table="TBL_OSU_PRODUCTBUSINESS_C"lazy="false"inverse="false"cascade="save-update"schema="OSU_WORK">
<keycolumn="PRBU_PRDUID_FK"/>
<many-to-manyclass="com.netqin.function.osuproduct.businessfunction.model.BusinessFunction"column="PRBU_BUFUID_FK"/>
</set>
</class>
</hibernate-mapping>说明:只有1中做了关联配置。lazy="false" :加载1时级联加载2,方便页面使用(看需要使用)。inverse="false":控制权由1负责。
cascade="save-update" :级联关系为新增和修改,不能加上delete,否则会一并删除2中的记录。
2.TBL_OSU_BUSINESSFUNCTION_I
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<classname="com.netqin.function.osuproduct.businessfunction.model.BusinessFunction"table="TBL_OSU_BUSINESSFUNCTION_I"schema="USERINFOV2">
<idname="bufuId"type="java.lang.String">
<columnname="BUFU_ID"length="10"/>
<generatorclass="uuid.hex"/>
</id>
<propertyname="bufuBusiidFk"type="java.lang.String">
<columnname="BUFU_BUSIID_FK"length="10"not-null="true"/>
</property>
<propertyname="bufuName"type="java.lang.String">
<columnname="BUFU_NAME"length="64"not-null="true"/>
</property>
<propertyname="bufuType"type="java.lang.Long">
<columnname="BUFU_TYPE"precision="4"scale="0"not-null="true"/>
</property>
</class>
</hibernate-mapping>说明:没有做关联
3.TBL_OSU_PRODUCTBUSINESS_C
没有映射文件,仅有两个字段,代表两个表的主键,分别为PRBU_PRDUID_FK和PRBU_BUFUID_FK,前者为1的主键,后者为2的主键
二。操作方法
1.新增
String[] businesses= request.getParameterValues("businesses");
if (businesses != null&& businesses.length > 0) {
for(inti=0;i<businesses.length;i++){
BusinessFunctionbf=(BusinessFunction)dao.findById(BusinessFunction.class,businesses[i]);//根据主键从数据库中查询出另一方的对象
object.getBusiness().add(bf);//关联对象
}
}dao.save(object);
2.修改
OsuProduct object_tem = this.findById(object.getId());//从数据库中查询出主表对象
object_tem.getBusiness().removeAll(object_tem.getBusiness());//清空原有级联对象
copyProject(object_tem, object);//其余属性复制,同many-to-one
String[] businesses= request.getParameterValues("businesses");
if (businesses != null&& businesses.length > 0) {
for(inti=0;i<businesses.length;i++){
BusinessFunctionbf=(BusinessFunction)dao.findById(BusinessFunction.class,businesses[i]);
object_tem.getBusiness().add(bf);
}
}dao.update(object_tem);
3.删除
删除主表即可
dao.delete(object);