甜咖啡

          我的IT空間

          hibernate對于復合主鍵映射后 jsp中如何使用復合主鍵

          剛接觸java的ssh框架,有很多東西都不了解,最近遇到復合主鍵的問題,不知道在前臺jsp頁面中如何調用復合主鍵,網上查到了一篇博文(http://blog.csdn.net/ttaaoo36/archive/2010/07/26/5766810.aspx)解決了這個問題,現在也把我的內容貼出來。

          1.oracle中建表語句

          drop table CUSTRICH cascade constraints;

          /*==============================================================*/
          /* Table: CUSTRICH                                              */
          /*==============================================================*/
          create table CUSTRICH  (
             BRANCH_NO            CHAR(10)                        not null,
             YYYYMM               DATE                            not null,
             CNY_BAL              NUMBER,
             NONCNY_BAL           NUMBER,
             LOAN_TOT_CNY         NUMBER,
             LOAN_TOT_NONCNY      NUMBER,
             RICH_BALCASHUNIT     NUMBER,
             BDF10FR_TOT_BAL      NUMBER,
             FOUND_TOT            NUMBER,
             INSURANCEFEEAMT      NUMBER,
             CD03_CURR_BAL        NUMBER,
             FN1TON_COUNT         NUMBER,
             FNSETPLN_COUNT       NUMBER,
             TPCCACCBAL           NUMBER,
             APPLICATION_ID       NUMBER,
             JGDM                 NUMBER,
             ZCRMBHJ              NUMBER,
             "Reserve1"           NUMBER,
             ASSET_TOT_CNY        NUMBER,
             "Reserve3"           NUMBER,
             "Reserve4"           NUMBER,
             "Reserve5"           NUMBER,
             "Reserve6"           NUMBER,
             "Reserve7"           NUMBER,
             "Reserve8"           NUMBER,
             "Reserve9"           NUMBER,
             "Reserve10"          NUMBER,
             constraint PK_CUSTRICH primary key (BRANCH_NO, YYYYMM)
          );

          兩個字段branch_no 和yyyymm 復合為一個主鍵

          2. hibernate映射為兩個java類文件  :Custrich.java和 CustrichId.java

          Custrich.java   內容如下

          package com.hljzr.bean;

          import java.math.BigDecimal;

          /**
           * Custrich entity. @author MyEclipse Persistence Tools
           */

          public class Custrich implements java.io.Serializable {

           // Fields

           private CustrichId id;
           private BigDecimal cnyBal;
           private BigDecimal noncnyBal;
           private BigDecimal loanTotCny;
           private BigDecimal loanTotNoncny;
           private BigDecimal richBalcashunit;
           private BigDecimal bdf10frTotBal;
           private BigDecimal foundTot;
           private BigDecimal insurancefeeamt;
           private BigDecimal cd03CurrBal;
           private BigDecimal fn1tonCount;
           private BigDecimal fnsetplnCount;
           private BigDecimal tpccaccbal;
           private BigDecimal applicationId;
           private BigDecimal jgdm;
           private BigDecimal zcrmbhj;
           private BigDecimal reserve1;
           private BigDecimal assetTotCny;
           private BigDecimal reserve3;
           private BigDecimal reserve4;
           private BigDecimal reserve5;
           private BigDecimal reserve6;
           private BigDecimal reserve7;
           private BigDecimal reserve8;
           private BigDecimal reserve9;
           private BigDecimal reserve10;

           // Constructors

           /** default constructor */
           public Custrich() {
           }

           /** minimal constructor */
           public Custrich(CustrichId id) {
            this.id = id;
           }

           /** full constructor */
           public Custrich(CustrichId id, BigDecimal cnyBal, BigDecimal noncnyBal,
             BigDecimal loanTotCny, BigDecimal loanTotNoncny,
             BigDecimal richBalcashunit, BigDecimal bdf10frTotBal,
             BigDecimal foundTot, BigDecimal insurancefeeamt,
             BigDecimal cd03CurrBal, BigDecimal fn1tonCount,
             BigDecimal fnsetplnCount, BigDecimal tpccaccbal,
             BigDecimal applicationId, BigDecimal jgdm, BigDecimal zcrmbhj,
             BigDecimal reserve1, BigDecimal assetTotCny, BigDecimal reserve3,
             BigDecimal reserve4, BigDecimal reserve5, BigDecimal reserve6,
             BigDecimal reserve7, BigDecimal reserve8, BigDecimal reserve9,
             BigDecimal reserve10) {
            this.id = id;
            this.cnyBal = cnyBal;
            this.noncnyBal = noncnyBal;
            this.loanTotCny = loanTotCny;
            this.loanTotNoncny = loanTotNoncny;
            this.richBalcashunit = richBalcashunit;
            this.bdf10frTotBal = bdf10frTotBal;
            this.foundTot = foundTot;
            this.insurancefeeamt = insurancefeeamt;
            this.cd03CurrBal = cd03CurrBal;
            this.fn1tonCount = fn1tonCount;
            this.fnsetplnCount = fnsetplnCount;
            this.tpccaccbal = tpccaccbal;
            this.applicationId = applicationId;
            this.jgdm = jgdm;
            this.zcrmbhj = zcrmbhj;
            this.reserve1 = reserve1;
            this.assetTotCny = assetTotCny;
            this.reserve3 = reserve3;
            this.reserve4 = reserve4;
            this.reserve5 = reserve5;
            this.reserve6 = reserve6;
            this.reserve7 = reserve7;
            this.reserve8 = reserve8;
            this.reserve9 = reserve9;
            this.reserve10 = reserve10;
           }

           // Property accessors

           public CustrichId getId() {
            return this.id;
           }

           public void setId(CustrichId id) {
            this.id = id;
           }

           public BigDecimal getCnyBal() {
            return this.cnyBal;
           }

           public void setCnyBal(BigDecimal cnyBal) {
            this.cnyBal = cnyBal;
           }

           public BigDecimal getNoncnyBal() {
            return this.noncnyBal;
           }

           public void setNoncnyBal(BigDecimal noncnyBal) {
            this.noncnyBal = noncnyBal;
           }

           public BigDecimal getLoanTotCny() {
            return this.loanTotCny;
           }

           public void setLoanTotCny(BigDecimal loanTotCny) {
            this.loanTotCny = loanTotCny;
           }

           public BigDecimal getLoanTotNoncny() {
            return this.loanTotNoncny;
           }

           public void setLoanTotNoncny(BigDecimal loanTotNoncny) {
            this.loanTotNoncny = loanTotNoncny;
           }

           public BigDecimal getRichBalcashunit() {
            return this.richBalcashunit;
           }

           public void setRichBalcashunit(BigDecimal richBalcashunit) {
            this.richBalcashunit = richBalcashunit;
           }

           public BigDecimal getBdf10frTotBal() {
            return this.bdf10frTotBal;
           }

           public void setBdf10frTotBal(BigDecimal bdf10frTotBal) {
            this.bdf10frTotBal = bdf10frTotBal;
           }

           public BigDecimal getFoundTot() {
            return this.foundTot;
           }

           public void setFoundTot(BigDecimal foundTot) {
            this.foundTot = foundTot;
           }

           public BigDecimal getInsurancefeeamt() {
            return this.insurancefeeamt;
           }

           public void setInsurancefeeamt(BigDecimal insurancefeeamt) {
            this.insurancefeeamt = insurancefeeamt;
           }

           public BigDecimal getCd03CurrBal() {
            return this.cd03CurrBal;
           }

           public void setCd03CurrBal(BigDecimal cd03CurrBal) {
            this.cd03CurrBal = cd03CurrBal;
           }

           public BigDecimal getFn1tonCount() {
            return this.fn1tonCount;
           }

           public void setFn1tonCount(BigDecimal fn1tonCount) {
            this.fn1tonCount = fn1tonCount;
           }

           public BigDecimal getFnsetplnCount() {
            return this.fnsetplnCount;
           }

           public void setFnsetplnCount(BigDecimal fnsetplnCount) {
            this.fnsetplnCount = fnsetplnCount;
           }

           public BigDecimal getTpccaccbal() {
            return this.tpccaccbal;
           }

           public void setTpccaccbal(BigDecimal tpccaccbal) {
            this.tpccaccbal = tpccaccbal;
           }

           public BigDecimal getApplicationId() {
            return this.applicationId;
           }

           public void setApplicationId(BigDecimal applicationId) {
            this.applicationId = applicationId;
           }

           public BigDecimal getJgdm() {
            return this.jgdm;
           }

           public void setJgdm(BigDecimal jgdm) {
            this.jgdm = jgdm;
           }

           public BigDecimal getZcrmbhj() {
            return this.zcrmbhj;
           }

           public void setZcrmbhj(BigDecimal zcrmbhj) {
            this.zcrmbhj = zcrmbhj;
           }

           public BigDecimal getReserve1() {
            return this.reserve1;
           }

           public void setReserve1(BigDecimal reserve1) {
            this.reserve1 = reserve1;
           }

           public BigDecimal getAssetTotCny() {
            return this.assetTotCny;
           }

           public void setAssetTotCny(BigDecimal assetTotCny) {
            this.assetTotCny = assetTotCny;
           }

           public BigDecimal getReserve3() {
            return this.reserve3;
           }

           public void setReserve3(BigDecimal reserve3) {
            this.reserve3 = reserve3;
           }

           public BigDecimal getReserve4() {
            return this.reserve4;
           }

           public void setReserve4(BigDecimal reserve4) {
            this.reserve4 = reserve4;
           }

           public BigDecimal getReserve5() {
            return this.reserve5;
           }

           public void setReserve5(BigDecimal reserve5) {
            this.reserve5 = reserve5;
           }

           public BigDecimal getReserve6() {
            return this.reserve6;
           }

           public void setReserve6(BigDecimal reserve6) {
            this.reserve6 = reserve6;
           }

           public BigDecimal getReserve7() {
            return this.reserve7;
           }

           public void setReserve7(BigDecimal reserve7) {
            this.reserve7 = reserve7;
           }

           public BigDecimal getReserve8() {
            return this.reserve8;
           }

           public void setReserve8(BigDecimal reserve8) {
            this.reserve8 = reserve8;
           }

           public BigDecimal getReserve9() {
            return this.reserve9;
           }

           public void setReserve9(BigDecimal reserve9) {
            this.reserve9 = reserve9;
           }

           public BigDecimal getReserve10() {
            return this.reserve10;
           }

           public void setReserve10(BigDecimal reserve10) {
            this.reserve10 = reserve10;
           }

          }

          CustrichId.java  內容如下:

          package com.hljzr.bean;

          import java.util.Date;

          /**
           * CustrichId entity. @author MyEclipse Persistence Tools
           */

          public class CustrichId implements java.io.Serializable {

           // Fields

           private String branchNo;
           private Date yyyymm;

           // Constructors

           /** default constructor */
           public CustrichId() {
           }

           /** full constructor */
           public CustrichId(String branchNo, Date yyyymm) {
            this.branchNo = branchNo;
            this.yyyymm = yyyymm;
           }

           // Property accessors

           public String getBranchNo() {
            return this.branchNo;
           }

           public void setBranchNo(String branchNo) {
            this.branchNo = branchNo;
           }

           public Date getYyyymm() {
            return this.yyyymm;
           }

           public void setYyyymm(Date yyyymm) {
            this.yyyymm = yyyymm;
           }

           public boolean equals(Object other) {
            if ((this == other))
             return true;
            if ((other == null))
             return false;
            if (!(other instanceof CustrichId))
             return false;
            CustrichId castOther = (CustrichId) other;

            return ((this.getBranchNo() == castOther.getBranchNo()) || (this
              .getBranchNo() != null
              && castOther.getBranchNo() != null && this.getBranchNo()
              .equals(castOther.getBranchNo())))
              && ((this.getYyyymm() == castOther.getYyyymm()) || (this
                .getYyyymm() != null
                && castOther.getYyyymm() != null && this.getYyyymm()
                .equals(castOther.getYyyymm())));
           }

           public int hashCode() {
            int result = 17;

            result = 37 * result
              + (getBranchNo() == null ? 0 : this.getBranchNo().hashCode());
            result = 37 * result
              + (getYyyymm() == null ? 0 : this.getYyyymm().hashCode());
            return result;
           }

          }

          hashCode和equals方法是用來控制主鍵內容不能為空和不能重復的。

          3.Custrich.hbm.xml的配置內容為:

          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          " <!--
              Mapping file autogenerated by MyEclipse Persistence Tools
          -->
          <hibernate-mapping>
              <class name="com.hljzr.bean.Custrich" table="CUSTRICH" schema="YHXM">
                  <composite-id name="id" class="com.hljzr.bean.CustrichId">
                      <key-property name="branchNo" type="java.lang.String">
                          <column name="BRANCH_NO" length="10" />
                      </key-property>
                      <key-property name="yyyymm" type="java.util.Date">
                          <column name="YYYYMM" length="7" />
                      </key-property>
                  </composite-id>
                  <property name="cnyBal" type="java.math.BigDecimal">
                      <column name="CNY_BAL" precision="22" scale="0" />
                  </property>
                  <property name="noncnyBal" type="java.math.BigDecimal">
                      <column name="NONCNY_BAL" precision="22" scale="0" />
                  </property>
                  <property name="loanTotCny" type="java.math.BigDecimal">
                      <column name="LOAN_TOT_CNY" precision="22" scale="0" />
                  </property>
                  <property name="loanTotNoncny" type="java.math.BigDecimal">
                      <column name="LOAN_TOT_NONCNY" precision="22" scale="0" />
                  </property>
                  <property name="richBalcashunit" type="java.math.BigDecimal">
                      <column name="RICH_BALCASHUNIT" precision="22" scale="0" />
                  </property>
                  <property name="bdf10frTotBal" type="java.math.BigDecimal">
                      <column name="BDF10FR_TOT_BAL" precision="22" scale="0" />
                  </property>
                  <property name="foundTot" type="java.math.BigDecimal">
                      <column name="FOUND_TOT" precision="22" scale="0" />
                  </property>
                  <property name="insurancefeeamt" type="java.math.BigDecimal">
                      <column name="INSURANCEFEEAMT" precision="22" scale="0" />
                  </property>
                  <property name="cd03CurrBal" type="java.math.BigDecimal">
                      <column name="CD03_CURR_BAL" precision="22" scale="0" />
                  </property>
                  <property name="fn1tonCount" type="java.math.BigDecimal">
                      <column name="FN1TON_COUNT" precision="22" scale="0" />
                  </property>
                  <property name="fnsetplnCount" type="java.math.BigDecimal">
                      <column name="FNSETPLN_COUNT" precision="22" scale="0" />
                  </property>
                  <property name="tpccaccbal" type="java.math.BigDecimal">
                      <column name="TPCCACCBAL" precision="22" scale="0" />
                  </property>
                  <property name="applicationId" type="java.math.BigDecimal">
                      <column name="APPLICATION_ID" precision="22" scale="0" />
                  </property>
                  <property name="jgdm" type="java.math.BigDecimal">
                      <column name="JGDM" precision="22" scale="0" />
                  </property>
                  <property name="zcrmbhj" type="java.math.BigDecimal">
                      <column name="ZCRMBHJ" precision="22" scale="0" />
                  </property>
                  <property name="reserve1" type="java.math.BigDecimal">
                      <column name="RESERVE1" precision="22" scale="0" />
                  </property>
                  <property name="assetTotCny" type="java.math.BigDecimal">
                      <column name="ASSET_TOT_CNY" precision="22" scale="0" />
                  </property>
                  <property name="reserve3" type="java.math.BigDecimal">
                      <column name="RESERVE3" precision="22" scale="0" />
                  </property>
                  <property name="reserve4" type="java.math.BigDecimal">
                      <column name="RESERVE4" precision="22" scale="0" />
                  </property>
                  <property name="reserve5" type="java.math.BigDecimal">
                      <column name="RESERVE5" precision="22" scale="0" />
                  </property>
                  <property name="reserve6" type="java.math.BigDecimal">
                      <column name="RESERVE6" precision="22" scale="0" />
                  </property>
                  <property name="reserve7" type="java.math.BigDecimal">
                      <column name="RESERVE7" precision="22" scale="0" />
                  </property>
                  <property name="reserve8" type="java.math.BigDecimal">
                      <column name="RESERVE8" precision="22" scale="0" />
                  </property>
                  <property name="reserve9" type="java.math.BigDecimal">
                      <column name="RESERVE9" precision="22" scale="0" />
                  </property>
                  <property name="reserve10" type="java.math.BigDecimal">
                      <column name="RESERVE10" precision="22" scale="0" />
                  </property>
              </class>
          </hibernate-mapping>
          4.在配置文件applicationContext-common.xml中mapping上面的映射文件

          <property name="mappingResources">
             <list>
              <value>com/hljzr/bean/Custrich.hbm.xml</value>
             </list>
            </property>
          5.在前臺jsp頁面中

           <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

              <c:if test="${!empty pm.list}">
               <c:set var = "sum" value = "0" />
              <c:forEach items="${pm.list}" var="cus">
               <tr>
                <td>
                 ${cus.id.branchNo}    <!--如果寫成cus.branchNo就不對了-->
                </td>
                <td>
                 ${cus.id.yyyymm}
                </td>
                <td>
                 ${cus.insurancefeeamt}
                </td>
                <td>
                 ${cus.foundTot}
                </td>
                <td>
                 ${cus.foundTot}
                </td>
                <td>
                 ${cus.foundTot}
                </td>
               </tr>
               <c:set value="${sum+cus.foundTot}" var="sum" />
              </c:forEach>
             </c:if>

          這里的pm是在action中設置的數據庫查詢結果集,這里就不詳述啦

          posted on 2011-03-26 23:38 甜咖啡 閱讀(579) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2011年3月>
          272812345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 翁源县| 肃南| 崇州市| 开封县| 缙云县| 台中市| 杭州市| 吴桥县| 苍山县| 沙坪坝区| 岑溪市| 朝阳区| 鞍山市| 武隆县| 平乐县| 吴旗县| 丰台区| 南木林县| 惠来县| 小金县| 平度市| 海安县| 昌吉市| 玛沁县| 文山县| 灵璧县| 柯坪县| 来宾市| 开封市| 台南市| 婺源县| 民丰县| 万荣县| 拉孜县| 文水县| 北安市| 巴林右旗| 济南市| 醴陵市| 溆浦县| 梁河县|