public class RepH201 implements java.io.Serializable {         private RepH201Id id;      
     
     private String ftableno1;
     private String ftableno2;
     private String ftableno3;
     private String fbuildaddrcode;
     .
     .
     .
     (属性太多省略。。)
}
public class RepH201Id  implements java.io.Serializable {
     private String fmonth;
     private String funitcode;
     private String fitemname;     ....get()  set()方法.....     public boolean equals(Object other) {
         if ( (this == other ) ) return true;
 if ( (other == null ) ) return false;
 if ( !(other instanceof RepH201Id) ) return false;
 RepH201Id castOther = ( RepH201Id ) other; 
         
 return ( (this.getFmonth()==castOther.getFmonth()) || ( this.getFmonth()!=null && castOther.getFmonth()!=null && this.getFmonth().equals(castOther.getFmonth()) ) )
 && ( (this.getFunitcode()==castOther.getFunitcode()) || ( this.getFunitcode()!=null && castOther.getFunitcode()!=null && this.getFunitcode().equals(castOther.getFunitcode()) ) );
 && ( (this.getFitemname()==castOther.getFitemname()) || ( this.getFitemname()!=null && castOther.getFitemname()!=null && this.getFitemname().equals(castOther.getFitemname()) ) );
   }
   
   public int hashCode() {
         int result = 17;
         
         result = 37 * result + ( getFmonth() == null ? 0 : this.getFmonth().hashCode() );
         result = 37 * result + ( getFunitcode() == null ? 0 : this.getFunitcode().hashCode() )
         result = 37 * result + ( getFitemname() == null ? 0 : this.getFitemname().hashCode() );
         return result;
   }  映射文件:<hibernate-mapping>
    <class name="com.wedz.hibernater.dao.h201.RepH201" table="REP_H201" schema="dbo" catalog="report">
        <composite-id name="id" class="com.wedz.hibernater.dao.h201.RepH201Id">
            <key-property name="fmonth" type="java.lang.String">
                <column name="fmonth" length="7" />
            </key-property>
            <key-property name="funitcode" type="java.lang.String">
                <column name="funitcode" length="9" />
            </key-property>
            <key-property name="fitemname" type="java.lang.String">
                <column name="fitemname" length="60" />
            </key-property>
       </composite-id>
       
            <property name="ftableno1" type="java.lang.String">
                <column name="ftableno1" length="9" />
            </property>
            <property name="ftableno2" type="java.lang.String">
                <column name="ftableno2" length="2" />
            </property>
             ......
 我要做修改操作:public int update(RepH201 h201,RepH201Id id){
Session session=this.beginTransaction();
RepH201Id h201id;
RepH201 h201t;
try{
Object obj=session.get(RepH201.class,id);
if(obj==null){
return -1;
}
h201t=(RepH201)obj;
h201id=h201t.getId();
h201id.setFitemname(h201.getId().getFitemname());
h201t.setId(h201id);                           h201t.setFtableno3(..);
         h201t.setFtableno2(..);
         h201t.setFtableno1(..);                            ....                           session.update(h201t);
this.endTransaction(true);
return 0;

}catch(HibernateException e){
this.endTransaction(false);
e.printStackTrace();
return -9;
}
                            
  }每次如果 修改了 fitemname 就会出问题
怎么办啊?数据库我不能该 
有没有办法可以实现对主键的修改啊???

解决方案 »

  1.   

    每次如果 修改了 fitemname 就会出问题 抛出异常:
    org.hibernate.StaleStateException:Unexpected row count:0 expected:1急啊!!!!
      

  2.   

    如果是复合主键,那么就不能修改了。这样就会出现跟新错误。修改主键也可以用sql实现。你也可以建个新表试试。做个单独的测试。
      

  3.   

    这不是Hibernate的问题,是数据库关于主键的基础知识,楼主怎么给忘了?呵呵