Topic: hibernate复合主键关系不能更新问题

  Print this page

1.hibernate复合主键关系不能更新问题 Copy to clipboard
Posted by: fourfire
Posted on: 2004-08-13 15:11

有两个表,一对多,主表主键包括4个字段,主表主键也是子表的主键
主表:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.longtop.oa.bean.OutstandingAchievementBean" table="RY_YJKH">
<composite-id>
<key-property name="year" column="kh_nd" />
<key-property name="month" column="kh_yf" />
<key-property name="object" column="kh_dx" type="string" length="40"/>
<key-property name="type" column="kh_dxfl" />
</composite-id>
<property name="fraction" column="kh_kf" />
</class>
</hibernate-mapping>

package com.longtop.oa.bean;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class OutstandingAchievementBean implements Serializable {

/** identifier field */
private int year;

/** identifier field */
private int month;

/** identifier field */
private String object;

/** identifier field */
private int type;

/** nullable persistent field */
private double fraction;

/** full constructor */
public OutstandingAchievementBean(int year, int month, java.lang.String object, int type, double fraction) {
this.year = year;
this.month = month;
this.object = object;
this.type = type;
this.fraction = fraction;
}

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

/** minimal constructor */
public OutstandingAchievementBean(int year, int month, java.lang.String object, int type) {
this.year = year;
this.month = month;
this.object = object;
this.type = type;
}

public int getYear() {
return this.year;
}

  public void setYear(int year) {
    this.year = year;
  }

public int getMonth() {
return this.month;
}

  public void setMonth(int month) {
    this.month = month;
  }

public java.lang.String getObject() {
return this.object;
}

  public void setObject(java.lang.String object) {
    this.object = object;
  }

public int getType() {
return this.type;
}

  public void setType(int type) {
    this.type = type;
  }

public double getFraction() {
return this.fraction;
}

  public void setFraction(double fraction) {
    this.fraction = fraction;
  }

public String toString() {
return new ToStringBuilder(this)
.append("year", getYear())
.append("month", getMonth())
.append("object", getObject())
.append("type", getType())
.toString();
}

public boolean equals(Object other) {
if ( !(other instanceof OutstandingAchievementBean) ) return false;
OutstandingAchievementBean castOther = (OutstandingAchievementBean) other;
return new EqualsBuilder()
.append(this.getYear(), castOther.getYear())
.append(this.getMonth(), castOther.getMonth())
.append(this.getObject(), castOther.getObject())
.append(this.getType(), castOther.getType())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getYear())
.append(getMonth())
.append(getObject())
.append(getType())
.toHashCode();
}

}
子表:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
  <class name="com.longtop.oa.bean.FractionDetailBean" table="RY_KFMXB">
    <composite-id unsaved-value="none">
      <key-many-to-one name="outstandingAchievementBean" class="com.longtop.oa.bean.OutstandingAchievementBean">
        <column name="kh_dxfl"/>
        <column name="kh_dx"/>
        <column name="kh_yf"/>
        <column name="kh_nd"/>
      </key-many-to-one>
      <key-property name="id" column="kfmx_xh" type="string" length="20"/>
    </composite-id>
    <property name="reason" column="kfmx_yy" type="string" length="100"/>
    <property name="number" column="kfmx_sl" />
    <property name="unitfraction" column="kfmx_dwfs" />
    <property name="remark" column="kfmx_bz" type="string" length="40"/>
  </class>
</hibernate-mapping>

package com.longtop.oa.bean;

import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class FractionDetailBean implements Serializable {

/** identifier field */
private OutstandingAchievementBean outstandingAchievementBean;



/** identifier field */
private String id;

/** nullable persistent field */
private String reason;

/** nullable persistent field */
private int number;

/** nullable persistent field */
private double unitfraction;

/** nullable persistent field */
private String remark;

/** full constructor */
public FractionDetailBean(OutstandingAchievementBean outstandingAchievementBean, java.lang.String id, java.lang.String reason, int number, double unitfraction, java.lang.String remark) {
this.outstandingAchievementBean = outstandingAchievementBean;

this.id = id;
this.reason = reason;
this.number = number;
this.unitfraction = unitfraction;
this.remark = remark;
}

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

/** minimal constructor */
public FractionDetailBean(OutstandingAchievementBean outstandingAchievementBean, java.lang.String id) {
this.outstandingAchievementBean = outstandingAchievementBean;

this.id = id;
}

public OutstandingAchievementBean getOutstandingAchievementBean() {
return this.outstandingAchievementBean;
}

  public void setOutstandingAchievementBean(OutstandingAchievementBean outstandingAchievementBean) {
    this.outstandingAchievementBean = outstandingAchievementBean;
  }


  

public java.lang.String getId() {
return this.id;
}

  public void setId(java.lang.String id) {
    this.id = id;
  }

public java.lang.String getReason() {
return this.reason;
}

  public void setReason(java.lang.String reason) {
    this.reason = reason;
  }

public int getNumber() {
return this.number;
}

  public void setNumber(int number) {
    this.number = number;
  }

public double getUnitfraction() {
return this.unitfraction;
}

  public void setUnitfraction(double unitfraction) {
    this.unitfraction = unitfraction;
  }

public java.lang.String getRemark() {
return this.remark;
}

  public void setRemark(java.lang.String remark) {
    this.remark = remark;
  }

public String toString() {
return new ToStringBuilder(this)
.append("outstandingAchievementBean", getOutstandingAchievementBean())
.append("id", getId())
.toString();
}

public boolean equals(Object other) {
if( !(other instanceof FractionDetailBean) ) return false;
FractionDetailBean castOther = (FractionDetailBean) other;
return new EqualsBuilder()
.append(this.getOutstandingAchievementBean(), castOther.getOutstandingAchievementBean())
.append(this.getId(), castOther.getId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getOutstandingAchievementBean())

.append(getId())
.toHashCode();
}

}
当用以下类插入数据时
private void testAdd()
  {
    OutstandingAchievementBean oab = new OutstandingAchievementBean();
    oab.setYear(2004);
    oab.setMonth(10);
    oab.setObject("abc");
    oab.setType(1);
    oab=new OutstandingAchievement().findBeanById(oab);
    
    FractionDetailBean fdb=new FractionDetailBean(oab,"1");
    fdb.setNumber(1);
    fdb.setUnitfraction(2d);
    fdb.setId("1");
    fdb.setReason("");
    fdb.setRemark("");
    new FractionDetail().addFractionDetail(fdb);
    
  }
报错:
net.sf.hibernate.JDBCException: Could not execute JDBC batch update
  at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:133)
  at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2421)
  at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371)
  at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240)
  at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
  at com.longtop.oa.business.FractionDetail.addFractionDetail(FractionDetail.java:31)
  at com.longtop.oa.test.FractionDetailTest.testAdd(FractionDetailTest.java:46)
  at com.longtop.oa.test.FractionDetailTest.main(FractionDetailTest.java:25)
Caused by: java.sql.BatchUpdateException: ORA-01722: 无效数字

  at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:458)
  at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3907)
  at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
  at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:126)

2.Re:hibernate复合主键关系不能更新问题 [Re: fourfire] Copy to clipboard
Posted by: fourfire
Posted on: 2004-08-26 12:49

解决


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923