1

I have an update page of users of a website. Some columns of user were coded in an enum. All the information of user goes to a POJO and all the columns were hashed. My problem is, when I open this update page and update some information of any user, It Works successfully. However, when I open this web page in another computer, the update process does not work clearly. I looked at the hash codes of columns by using 'system.out.println' and I saw two different computers produced two different hash codes in columns which are done with the enum base. I hope that my problem is clear. Thanks for any reply.

 @Override
    public int hashCode() {
        int hash = 3;
        hash = 79 * hash + (this.getId() != null ? this.getId().hashCode() : 0);
        hash = 79 * hash + (this.kullaniciTipi != null ? this.kullaniciTipi.hashCode() : 0);
        hash = 79 * hash + (this.kullaniciAdi != null ? this.kullaniciAdi.hashCode() : 0);
        hash = 79 * hash + (this.parola != null ? this.parola.hashCode() : 0);
        hash = 79 * hash + Arrays.hashCode(this.parmakIzi);
        hash = 79 * hash + (this.zamanAsimi != null ? this.zamanAsimi.hashCode() : 0);
        hash = 79 * hash + (this.tcKimlikNo != null ? this.tcKimlikNo.hashCode() : 0);
        hash = 79 * hash + (this.adiSoyadi != null ? this.adiSoyadi.hashCode() : 0);
        hash = 79 * hash + (this.epostaAdresi != null ? this.epostaAdresi.hashCode() : 0);
        hash = 79 * hash + (this.ekranKisayol != null ? this.ekranKisayol.hashCode() : 0);
        hash = 79 * hash + (this.hataliGirisSayisi != null ? this.hataliGirisSayisi.hashCode() : 0);
        hash = 79 * hash + Objects.hashCode(this.durum);
        hash = 79 * hash + (this.durum != null ? this.durum.hashCode() : 0);
        System.out.println("id : " + this.getId() + "id hash : " +this.getId().hashCode());
        System.out.println("zid : " + this.zamanAsimi + "zid hash : " +this.zamanAsimi.hashCode());
        System.out.println("this.kullaniciTipi.hashCode()" + this.kullaniciTipi.hashCode());
        System.out.println("this.kullaniciAdi.hashCode()" + this.kullaniciAdi.hashCode());
        System.out.println("this.parola.hashCode()" + this.parola.hashCode());
     //   System.out.println("this.parmakIzi.hashCode()" + this.parmakIzi.hashCode());
        System.out.println("this.zamanAsimi.hashCode()" + this.zamanAsimi.hashCode());
        System.out.println("this.tcKimlikNo.hashCode()" + this.tcKimlikNo.hashCode());
        System.out.println("this.adiSoyadi.hashCode()" + this.adiSoyadi.hashCode());
        System.out.println("this.epostaAdresi.hashCode()" + this.epostaAdresi.hashCode());
      //  System.out.println("this.ekranKisayol.hashCode()" + this.ekranKisayol.hashCode());
        System.out.println("this.hataliGirisSayisi.hashCode()" + this.hataliGirisSayisi.hashCode());
        System.out.println("this.durum.hashCode()" + this.durum.hashCode());
        System.out.println("sistem hash : " +hash);
        return hash;
    }
Deniz
  • 127
  • 1
  • 5
  • 14
  • Can we see the code for `hashCode()`? – dkatzel Oct 21 '13 at 14:13
  • Did you override hashCode()? – James Dunn Oct 21 '13 at 14:13
  • I added hashcode and yes it was overrided – Deniz Oct 21 '13 at 14:21
  • Hmmm. So it's working for you in Computer A, but not Computer B. You said, "when I open this web page in another computer". I'm calling the other computer "Computer B." When you opened the web page on computer B, were you actually running the program itself on computer B? If so, I think I have an answer. If you were just using a browser on Computer B to look at Computer A, then I'm not sure what's going on. – James Dunn Oct 21 '13 at 14:32
  • Yes ,I run the project in computer A and also computer B. When ı run the Project in computer A,update process worked successfully.Then I run the same project in Computer B ,then update process did not work.So I printted hashcodes and I saw outputs of some columns were different values in different computers. – Deniz Oct 21 '13 at 14:37

1 Answers1

3

Here's the problem:

  1. The default implementation of hashCode() converts the memory address of the object into an int. The memory address of an object is going to be different not only from computer to computer, but also probably each time you run the program. See this question.
  2. Your overridden hashCode() uses hashCodes of other objects in its calculation.
  3. For at least one of these other objects, hashCode() is not overridden.
  4. Therefore, your overridden hashCode() is calling a non-overridden hashCode(), which is causing inconsistent results.
Community
  • 1
  • 1
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • It is so logical..Thanks for your answer.I cannot remember that project has non-overridded hashcode().I will examine that. – Deniz Oct 21 '13 at 15:10
  • It could also be that all the hashCode()s you are calling are overridden, but one of those hashCode()s itself is calling a non-overridden hashCode(). You may have to do some tracing to find it. – James Dunn Oct 21 '13 at 15:12
  • +1 but "The default implementation of hashCode() converts the memory address of the object into an int" has been nothing more than 'a typical implementation' for many years: it's not required. However there is certainly nothing that says it is required to be the same on different plaforms or even different executions of the same program. – user207421 Oct 22 '13 at 01:05