甜咖啡

我的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"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    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 甜咖啡 阅读(574) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

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

统计

常用链接

留言簿(1)

我参与的团队

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜