hibernate中实现判断属性值是否相同

附截图

homework hibernate 4(1314010420 王弘智)

public class User {

    private int id;
    private String username;
    private String password;
    private int age;




    public int getId() {
        return id;
    }




    public void setId(int id) {
        this.id = id;
    }




    public String getUsername() {




        return username;
    }




    public void setUsername(String username) {
        this.username = username;
    }




    public String getPassword() {
        return password;
    }




    public void setPassword(String password) {
        this.password = password;
    }




    public int getAge() {
        return age;
    }




    public void setAge(int age) {
        this.age = age;
    }




    @Override
    public boolean equals(Object otherObject) {
        // 判断内存地址是否相同
        if (this == otherObject) {
            return true;
        } else {
            // 判断是否为User对象
            if (!(otherObject instanceof User))
                return false;
        }
        User user = (User) otherObject;
        // 判断各个属性值是否相同
        if (id == user.id && username.equals(user.username) && password.equals(user.password) && age == user.age) {
            return true;
        }




        return false;
    }




    @Override
    public int hashCode() {
        int result;
        result = username.hashCode() + password.hashCode();
        result = 29 * age + result;
        return result;
    }




}
      
}  

相关文章: