我的表两个主键sx、bianhao
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
    <class
        name="com.olive.czpgl.domain.Czpmx"
        table="CZPMX"
        dynamic-update="false"
        dynamic-insert="false"
        select-before-update="false"
        > <composite-id>
     <key-property name="sx" />
     <key-property name="bianhao" />
     </composite-id>        <property
            name="czxm"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="CZXM"
            length="200"
        />
<property
            name="cz"
            type="int"
            update="true"
            insert="true"
            access="property"
            column="CZ"
         
        />       
    </class></hibernate-mapping>
.java文件如下
/*
 * Created on 2004-11-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.olive.czpgl.domain;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.Serializable;import com.olive.security.domain.SysMenuAssign;/**
 * @author Wumh
 * 
 * @hibernate.class
 *  table="CZPMX"
 *
 */
public class Czpmx implements Serializable{
private int    sx;
private int    bianhao; 
    private String  czxm;
private int    cz; 
 

 
  
  public Czpmx() {
super();
} /* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
public String toString() {
return super.toString();
}
 /**
   *因为是联合主键,所以要重写equals方法hashCode方法
   * @param object
   * @return
   */
public boolean equals(Object obj) {
if (obj == null)
return false; if (!(obj instanceof Czpmx))
return false; if (((Czpmx) obj).getSx()!=(this.getSx()))
return false;

if (((Czpmx) obj).getBianhao()!=(this.getBianhao()))
return false; return true;
}

/**
 * 生成本对象的hash码
 * 
 * @return int
 * @roseuid 440BD93C0021
 */
public int hashCode() {
return 29 * sx.toString().hashCode() + bianhao.hashCode() + super.hashCode();
}

public int getBianhao() {
return bianhao;
}
public void setBianhao(int bianhao) {
this.bianhao = bianhao;
}
public int getCz() {
return cz;
}
public void setCz(int cz) {
this.cz = cz;
}
public String getCzxm() {
return czxm;
}
public void setCzxm(String czxm) {
this.czxm = czxm;
}
public int getSx() {
return sx;
}
public void setSx(int sx) {
this.sx = sx;
}


}提示我没有equals和hashCode,这两个方法是做什么用的,怎么写啊?

解决方案 »

  1.   

    联合主键.自定义hashCode和equals方法,保证主键的unique约束hashCode和equals可以利用myeclipse等插件自动产生,或者EqualsBuilder,HashCodeBuilder等工具类产生
      

  2.   

    public boolean equals(Object other) {
             if ( (this == other ) ) return true;
     if ( (other == null ) ) return false;
     if ( !(other instanceof CzpmxId) ) return false;
     CzpmxId castOther = ( CzpmxId ) other; 
             
     return ( (this.getSx()==castOther.getSx()) || ( this.getSx()!=null && castOther.getSx()!=null && this.getSx().equals(castOther.getSx()) ) )
     && ( (this.getBianhao()==castOther.getBianhao()) || ( this.getBianhao()!=null && castOther.getBianhao()!=null && this.getBianhao().equals(castOther.getBianhao()) ) );
       }
       
       public int hashCode() {
             int result = 17;
             
             result = 37 * result + ( getSx() == null ? 0 : this.getSx().hashCode() );
             result = 37 * result + ( getBianhao() == null ? 0 : this.getBianhao().hashCode() );
             return result;
       }
      

  3.   

    用myeclipse等插件自动产生,生成4个文件,都有用吗?