org.apache.commons.lang.builder.HashCodeBuilder;这个类的用法??求助于各位大侠

解决方案 »

  1.   

    用法很简单的。
    给你举个例子,结合EqualsBuilder的用法:
    package com.bryantd.hibernate.entity;import java.io.Serializable;
    import java.util.*;import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;public class Address implements Serializable {
    private Integer id;
    private String address;
    private String telephone;

    private User user;

    public Address(String address, String telephone) {
    super();
    // TODO Auto-generated constructor stub
    this.address = address;
    this.telephone = telephone;
    }

    public Address() {}

    public String getAddress() {
    return address;
    }
    public void setAddress(String address) {
    this.address = address;
    }

    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }

    public String getTelephone() {
    return telephone;
    }
    public void setTelephone(String telephone) {
    this.telephone = telephone;
    }

    public User getUser() {
    return user;
    }
    public void setUser(User user) {
    this.user = user;
    }

    public boolean equals(Object object) {
    if (!(object instanceof Address)) {
    return false;
    }
    Address anotherAddress = (Address)object;
    return new EqualsBuilder().appendSuper(super.equals(object))
                              .append(this.getAddress(), anotherAddress.getAddress())
                              .append(this.getTelephone(), anotherAddress.getTelephone())
                              .isEquals();
    }

    public int hashCode() {
    return new HashCodeBuilder().appendSuper(super.hashCode())
                                .append(this.getAddress())
                                .append(this.getTelephone())
                                .hashCode();
    } /*
    public boolean equals(Object object) {
    // TODO Auto-generated method stub
    if (onject == this) {
        return true;
    }
    if (object instanceof Address) {
    Address address = (Address)object;
    if (getAddress() == address.getAddress()) return true;
    }
    return false;
    } public int hashCode() {
    // TODO Auto-generated method stub
    return getTelephone().hashCode() * getAddress().hashCode();
    }
    */
    }