Hibernate级联实践之二(many-to-one)

一。mapping

这里使用两张表,分别是TBL_OSU_PRODUCT_I和TBL_OSU_PRODUCTMESSAGE_I,前者与后者是一对多的关系,映射文件如下:

 

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="productMessage" cascade="all-delete-orphan" inverse="true" lazy="false">
       <key column="PRME_PRDUID_FK"/>
      <one-to-many class="com.netqin.function.osuproduct.productmessage.model.ProductMessage"/>
  </set>
  
    
  </class>
</hibernate-mapping>

 

说明:cascade="all-delete-orphan":级联删除、修改和更新; inverse="true":控制权交给对方,既新增或修改时,ProductMessage需要明确指定对OsuProduct的关联;

         lazy="false":一并加载子表,方便页面展示(该属性可以根据实际情况制定);<key column="PRME_PRDUID_FK"/> :子表中关联主表的字段名称

 

2.TBL_OSU_PRODUCTMESSAGE_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.productmessage.model.ProductMessage" table="TBL_OSU_PRODUCTMESSAGE_I"  schema="OSU_WORK">
        <id name="prmeId" type="java.lang.Long">
            <column name="PRME_ID" precision="10" scale="0" />
            <generator class="sequence">
             <param name="sequence">SEQ_OSU_PRODUCTMESSAGE_I</param>
            </generator>
        </id>

        <property name="prmeMecoidFk" type="java.lang.Long">
            <column name="PRME_MECOID_FK" precision="10" scale="0" not-null="true" />
        </property>

        <many-to-one name="product"
                class="com.netqin.function.osuproduct.product.model.OsuProduct">
               <column name="PRME_PRDUID_FK" />
       </many-to-one>
  
               
        </class>
</hibernate-mapping>

 

说明:配置比较简单。

 

二。使用方法

 

主表操控子表方法:

1.新增:

    String[] messageContentId = request.getParameterValues("messageContentId");//提示语主题ID
       
    //提示语
    if(messageContentId!=null&&messageContentId.length>0){
     for(int i=0;i<messageContentId.length;i++){
      ProductMessage pm = new ProductMessage();
      pm.setPrmeMecoidFk(Long.parseLong(messageContentId[i]));
      pm.setProduct(object);//因为是inverse="true",所以需要子表指定主表对象
      object.getProductMessage().add(pm);//这个步骤不能省,否则不能级联保存子表
     }
    }

 

   dao.save(object);//object为主表对象,可以通过new得到

 

2.修改:

    OsuProduct object_tem = this.findById(object.getId());//从数据库中取到主表对象

    object_tem.getProductMessage().removeAll(object_tem.getProductMessage());//清空提示语,先清空在添加

 

    String[] messageContentId = request.getParameterValues("messageContentId");//提示语主题ID
    //提示语
    if(messageContentId!=null&&messageContentId.length>0){
       for(int i=0;i<messageContentId.length;i++){
        ProductMessage pm = new ProductMessage();
        pm.setPrmeMecoidFk(Long.parseLong(messageContentId[i]));
        pm.setProduct(object_tem);
        object_tem.getProductMessage().add(pm);
        }
    }

  //将页面传递过来的数据copy到持久对象中
   copyProject(object_tem, object);

   dao.update(object_tem);//关于主表的其余属性的修改可以应用一个copy方法

 

copy方法:

private void copyProject(OsuProduct dest, OsuProduct sour) {
    dest.setPrduDescription(sour.getPrduDescription());// 描述
 
}

 

3.删除

删除主表即可

dao.delete(object);

 

你可能感兴趣的:(Hibernate级联实践之二(many-to-one))