0

I know that HashCode is a way, but I've noticed that after a while the HashCode change. So, I have an application that permit to buy things, every article is identified by a code generated by now from the hashcode and stored in the db PostgreSQL, but I have discovered this issue so I can't use it. Infact the next day that I try to identify this article on the db the hashcode changed so it doesn't works. What is a solution? Thanks a lot! My object that generate code for article is something like this

public class AcquistoDVDRichiesto implements IsSerializable, CustomEnum {
    private int codice_carrello;
    private String utente;
    private int numero;
    private String film;
    private int fornitura;

    public AcquistoDVDRichiesto(){}
    public AcquistoDVDRichiesto(int c, String user){
        utente=user;
        codice_carrello=c;
    }

    public void generateCodeBasket(){
        if(film!=null && numero!=0 && fornitura!=0){
            codice_carrello=Math.abs(film.hashCode()+((Integer)numero).hashCode()+
                    ((Integer)fornitura).hashCode()+tipo_supporto.DVD.hashCode());
        }
    }
}

-

anto150192
  • 539
  • 3
  • 13
  • 1
    I'd recommend looking into `serializable` objects. But it sounds more like maybe you simply need to store an arbitrary ID with your object. Might need some more information. – CollinD Sep 22 '15 at 15:42
  • 2
    default hashCode of the object is usually the internal memory address of the object. It will change in different runs. – karakfa Sep 22 '15 at 15:45
  • If you haven't already you should make sure to implement the `equals` method and in a way that is consistent with your `hashCode` implementation. – Ken Sep 22 '15 at 15:48

1 Answers1

1

You shouldn't generate db primary keys by hand. The best approach is to let the database generate the unique primary keys for each record. This way you can be sure that there will be no primary key collisions and the codes will not change.

In PostreSQL, you can use a SERIAL column type to achieve that. Example:

CREATE TABLE tablename (
   colname SERIAL
);

The other way is to use a sequence, but it is a bit more complicated.

Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28
  • Thanks! How I can generate a code from PostegreSQL? Is there something specific? I know md5() for example – anto150192 Sep 22 '15 at 16:28
  • 1
    You can use the primary key as a code to uniquely identify a record. It's a plain integer number. – Danail Alexiev Sep 22 '15 at 16:38
  • All right! I get it. But I have four table in db and in every of them I want a unique code to identify the entry, in this way I obtain the same code in different tables, but when I do join I have to deal with more than a duplication – anto150192 Sep 22 '15 at 17:23
  • There is no impediment to have the same primary key values in different tables. Having, for example, a Country with PK = 1 and a City with PK = 1 is totally fine in a relational database. If you do a join, you will normally use a PK -> FK (foreign key) relation between the two tables. The City table will have a Country_Id field that holds a valid Country PK. The join will still be valid and you won't need to deal with any duplicates. If you are 100% sure you want DB unique IDs, you can create a single sequence and use it to populate the PKs of all tables, but I won't recommend this approach – Danail Alexiev Sep 23 '15 at 07:53