一。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"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.netqin.function.osuproduct.product.model.OsuProduct" table="TBL_OSU_PRODUCT_I" schema="OSU_WORK">
<id name="id" type="long">
<column name="PRDU_ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">SEQ_OSU_PRODUCT_I</param>
</generator>
</id>
<property name="prduName" type="string">
<column name="PRDU_NAME" length="200" not-null="true" />
</property>
<property name="prduDescription" type="string">
<column name="PRDU_DESCRIPTION" length="200" />
</property>
<set name="business" table="TBL_OSU_PRODUCTBUSINESS_C" lazy="false" inverse="false" cascade="save-update" schema="OSU_WORK">
<key column="PRBU_PRDUID_FK"/>
<many-to-many class="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"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.netqin.function.osuproduct.businessfunction.model.BusinessFunction" table="TBL_OSU_BUSINESSFUNCTION_I" schema="USERINFOV2">
<id name="bufuId" type="java.lang.String">
<column name="BUFU_ID" length="10" />
<generator class="uuid.hex" />
</id>
<property name="bufuBusiidFk" type="java.lang.String">
<column name="BUFU_BUSIID_FK" length="10" not-null="true" />
</property>
<property name="bufuName" type="java.lang.String">
<column name="BUFU_NAME" length="64" not-null="true" />
</property>
<property name="bufuType" type="java.lang.Long">
<column name="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 (int i = 0; i < businesses.length; i++) {
BusinessFunction bf = (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 (int i = 0; i < businesses.length; i++) {
BusinessFunction bf = (BusinessFunction)dao.findById(BusinessFunction.class, businesses[i]);
object_tem.getBusiness().add(bf);
}
}
dao.update(object_tem);
3.删除
删除主表即可
dao.delete(object);