Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 包含不调用equals方法来比较对象的函数_Java_Hibernate_Set_Equals_Gethashcode - Fatal编程技术网

Java 包含不调用equals方法来比较对象的函数

Java 包含不调用equals方法来比较对象的函数,java,hibernate,set,equals,gethashcode,Java,Hibernate,Set,Equals,Gethashcode,我正在使用SQL查询来获取Person对象。这个Person对象有一组兴趣,这也是我从SQL查询中得到的 现在,当我尝试检查Person对象中是否包含一个interest对象时,它并没有调用equals方法来检查它 下面是我的代码 class Interest { private Person person ; private Admin admin ; //getter setters for Person and admin public boolean equals(Object

我正在使用SQL查询来获取Person对象。这个Person对象有一组兴趣,这也是我从SQL查询中得到的

现在,当我尝试检查Person对象中是否包含一个interest对象时,它并没有调用equals方法来检查它

下面是我的代码

class Interest {


private Person person ;

private Admin admin ;

//getter setters for Person and admin 
public boolean equals(Object arg0) {
        // TODO Auto-generated method stub


        if (!(arg0 instanceof Interest)) {
            return false;
        }

        if (this==arg0){
            return true;
        }

        if(this.person.equals(((Interest)arg0).getPerson()) && this.admin.equals(((Interest)arg0).getAdmin()))
        {
            return true;
        }

        return false;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        int hc=0;

        if(this.person !=null)
            hc= hc + 19 * this.person.getName().length();

        if(this.admin !=null)
            hc= hc + 19 * this.admin.getName().length();

        return hc;
    }

    public int compareTo(Object arg0) {
    // TODO Auto-generated method stub
    String objStr = ((Interest)arg0).person.getName().toUpperCase();
    int value =  this.person.getName().toUpperCase().compareTo(objStr);

    return value;
}

}
现在在person类中,我有如下代码:

class Person {

private Set interest ;


//other fields 

//getter and setters 

public boolean isInterestExists(Interest interest){

return this.interest.contains(interest);
}

}
现在我正在执行这个命令

person.isInterestExists(interest);

它不是调用兴趣类的equals方法。我哪里出错了?

它在调用
hashCode
?不,先生,它也没有调用hashCode。它只在compareTo函数中出现。那么您的集合可能是一个树集,而不是哈希集。这就是TreeSet的工作原理。Java5是十多年前问世的。为什么还没有使用泛型类型?你为什么不缩进你的代码?是的,先生,集合是树集。对不起,忘了提那件事。先生,当我使用hql取出Person的完整对象,然后我确实包含它时,它会转到equals和hashCode。只有当我使用sql时,它才不会使用equals和hashCode方法。HQL是完全不相关的。HashSet使用equals和hashCode来比较对象。树集使用compareTo。物体从何而来无关紧要。这两种方法应该是等价的,即a.equals(b)iff a.compareTo(b)==0。不遵守这条规则是在乞讨问题。