hibernate一对多级联保存及删除(总结)
//采购单表头:(一方)public class PssBuyHead implements java.io.Serializable { private Integer buyid; private PssDepot pssDepot; private PssSupplier pssSupplier; private TSysUser TSysUser; private String buynum; private String buybills; private Date buydate; private String buydes; private Integer buytotal; private String isok; private String buytype; private Date createdate; private Integer createpeople; private Integer sumnum; private Float sumprice; private Set<PssBuyLine> pssBuyLines = new HashSet<PssBuyLine>(0); (get...set...方法省略)
???采用set集合对行信息的关联??
?? private Set<PssBuyLine> pssBuyLines = new HashSet<PssBuyLine>(0);
??? 采购单行信息对象 ("多方"对象)
public class PssBuyLine implements java.io.Serializable {private Integer productlinid;private PssBuyHead pssBuyHead;private String productbm;private String productname;private String standard;private String unit;private Integer productsum;private Float inprice;private Float buysummoney;private String product;private String productnum;private String productdes;(get... set ...)
??? 头信息对象
?
???? private PssBuyHead pssBuyHead;
?
2. 映射对象 .hbm.xml对象
??? ?"一方" 采购单头信息
<hibernate-mapping> <class name="com.business.buy.pojo.PssBuyHead" table="pss_buy_head"> <id name="buyid" type="java.lang.Integer"> <column name="BUYID" precision="8" scale="0"/> <generator lazy="false" fetch="select"> <column name="DEPOTID" precision="8" scale="0"> <comment>仓库代码</comment> </column> </many-to-one> <many-to-one name="pssSupplier" lazy="false" fetch="select"> <column name="SUPPLIERID" precision="8" scale="0"> <comment>供应商代码</comment> </column> </many-to-one> <many-to-one name="TSysUser" lazy="false" fetch="select"> <column name="USERID" precision="8" scale="0"> <comment>用户ID</comment> </column> </many-to-one> <property name="buynum" type="java.lang.String"> <column name="BUYNUM" length="20" /> </property> <property name="buybills" type="java.lang.String"> <column name="BUYBILLS" length="20" /> </property> <property name="buydate" type="java.util.Date"> <column name="BUYDATE" length="19" /> </property> <property name="buydes" type="java.lang.String"> <column name="BUYDES" length="40" /> </property> <property name="buytotal" type="java.lang.Integer"> <column name="BUYTOTAL" /> </property> <property name="isok" type="java.lang.String"> <column name="ISOK" length="10" /> </property> <property name="buytype" type="java.lang.String"> <column name="BUYTYPE" length="10" /> </property> <property name="createdate" type="java.util.Date"> <column name="CREATEDATE" length="19" /> </property> <property name="createpeople" type="java.lang.Integer"> <column name="CREATEPEOPLE" /> </property> <property name="sumnum" type="java.lang.Integer"> <column name="SUMNUM" /> </property> <property name="sumprice" type="java.lang.Float"> <column name="SUMPRICE" precision="10" /> </property> <set name="pssBuyLines" inverse="false" lazy="false" cascade="all"> <key> <column name="BUYID" precision="8" scale="0" /> </key> <one-to-many /> </set> </class></hibernate-mapping>
????"一"方关联"多方"对象
?
?????? <set name="pssBuyLines" inverse="false" lazy="false" cascade="all">
??????????? <key>
??????????????? <column name="BUYID" precision="8" scale="0" />
??????????? </key>
??????????? <one-to-many />
??????? </set>
???
??? 采购单行信息(“多方”)
<hibernate-mapping> <class name="com.business.buy.pojo.PssBuyLine" table="pss_buy_line"> <id name="productlinid" type="java.lang.Integer"> <column name="PRODUCTLINID" precision="8" scale="0"/> <generator fetch="select"> <column name="BUYID" precision="8" scale="0" /> </many-to-one> <property name="productbm" type="java.lang.String"> <column name="PRODUCTBM" length="20" /> </property> <property name="productname" type="java.lang.String"> <column name="PRODUCTNAME" length="30" /> </property> <property name="standard" type="java.lang.String"> <column name="STANDARD" length="20" /> </property> <property name="unit" type="java.lang.String"> <column name="UNIT" length="8" /> </property> <property name="productsum" type="java.lang.Integer"> <column name="PRODUCTSUM" /> </property> <property name="inprice" type="java.lang.Float"> <column name="INPRICE" precision="10" /> </property> <property name="buysummoney" type="java.lang.Float"> <column name="BUYSUMMONEY" precision="10" /> </property> <property name="product" type="java.lang.String"> <column name="PRODUCT" length="20" /> </property> <property name="productnum" type="java.lang.String"> <column name="PRODUCTNUM" length="20" /> </property> <property name="productdes" type="java.lang.String"> <column name="PRODUCTDES" length="30" /> </property> </class></hibernate-mapping>
????"多方"关联"一方"对象
?
?????? <many-to-one name="pssBuyHead" fetch="select">
??????????? <column name="BUYID" precision="8" scale="0" />
??????? </many-to-one>
?(以上代码调试通过)
??? 总结: 1,一对多关联;
??????????????? 2,“一”方维护关系;(inverse="false" lazy="false" cascade="all")
??????????????? 3;级联操作并删除孤儿;
??????????? (inverse,cascade不能再“多”方的配置文件<many-to-one.../>中配置)
???
??? ?-----配置文件相关配置参数说明-----
???? cascade:级联,可选值为:none,save-update,delete,all,all-delete-orphan?
????inverse: 反转,表示由哪方维护关系false为自己维护,可选值:true,false
??? lazy: true/false?; false 为非延迟加载
?
?
?
?
?