一。mapping
这里使用两张表,分别为TBL_OSU_PRODUCTFILTERDETAIL_I和TBL_OSU_PRODUCTFILTER_I,前者为复合主键,后者与前者为一对多的关系。
1.TBL_OSU_PRODUCTFILTERDETAIL_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.filter.model.ProductFilterDeatil" table="TBL_OSU_PRODUCTFILTERDETAIL_I" schema="OSU_WORK">
<composite-id name="id" class="com.netqin.function.osuproduct.filter.model.ProductFilterDeatilId">
<key-many-to-one name="filter" entity-name="com.netqin.function.osuproduct.filter.model.ProductFilter" lazy="false">
<column name="PFDE_PRFIID_FK"></column>
</key-many-to-one>
<key-many-to-one name="product" entity-name="com.netqin.function.osuproduct.product.model.OsuProduct" lazy="false">
<column name="PFDE_PRDUID_FK"></column>
</key-many-to-one>
</composite-id>
<property name="pfdeOrder" type="java.lang.Long">
<column name="PFDE_ORDER" precision="10" scale="0" not-null="true" />
</property>
</class>
</hibernate-mapping>
说明:该表为复合主键,且每个字段与其它表为many-to-one关系,这样做方便在页面中直接使用关联对象,注意lazy="false"。
2.TBL_OSU_PRODUCTFILTER_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.filter.model.ProductFilter" table="TBL_OSU_PRODUCTFILTER_I" schema="OSU_WORK">
<id name="prfiId" type="java.lang.Long">
<column name="PRFI_ID" precision="10" scale="0" />
<generator class="sequence">
<param name="sequence">SEQ_OSU_PRODUCTFILTER_I</param>
</generator>
</id>
<property name="prfiBusiidFk" type="java.lang.String">
<column name="PRFI_BUSIID_FK" length="10" not-null="true" />
</property>
<set name="productFilterDetailes" cascade="all-delete-orphan" inverse="true" lazy="false">
<key column="PFDE_PRFIID_FK"/>
<one-to-many class="com.netqin.function.osuproduct.filter.model.ProductFilterDeatil"/>
</set>
</class>
</hibernate-mapping>
说明:这里说明即便是复合主键,也可以使用one-to-many只关联其中的一个字段。
二。操作方法
1.新增
ProductFilter pf = new ProductFilter();
pf.setPrfiBusiidFk(business[i]);
//筛选明细
for(int j=0;j<par.length;j++){
tem = par[j].split(",");//相应的子表属性,这里仅为举例
ProductFilterDeatil pfd = new ProductFilterDeatil();
ProductFilterDeatilId pfdfk = new ProductFilterDeatilId();//复合主键对象
OsuProduct product = (OsuProduct)dao.findById(OsuProduct.class, Long.parseLong(tem[0]));
pfdfk.setFilter(pf);
pfdfk.setProduct(product);
pfd.setId(pfdfk);//封装复合主键
pfd.setPfdeOrder(Long.parseLong(tem[2]));
pf.getProductFilterDetailes().add(pfd);
}
dao.save(pf );
2.修改和删除方法与many-to-one类似,不在赘述。
这里需要注意的是,复合主键是不能重复的,所以在新增或修改时需要做异常处理,或在前台做控制。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hanqunfeng/archive/2009/08/27/4489811.aspx