Primary Key: Hashmap or Common Object?

Asked

Viewed 73 times

0

Good afternoon guys, all right?

My question is this... I want to use Hashmap as the primary key. Is there a problem? Or should I use Object?

NOTE: I am modeling the database, which will be with Mongodb.

From now on, thank you!

  • Hashmap as primary key? What would be the logic of this?

  • I was looking to use a composite primary key, and since I’ve never used it, I’m wondering if it’s possible and recommended... Object would be the best option?

  • What would your composite key look like?

  • It would have two attributes, the "type" of the machine and its "number". It wanted to use a very simple form, without creating an object.

  • Why not use 2 integer?

  • I thought about it... but would like to "try" to put a composite key. Is there any simple way? Otherwise, I will use these attributes

Show 1 more comment

1 answer

0

You can create your object with the fields normally and then create the composite key with the fields you want. In your case, you would create "machine type" and "number" and then write down both to create the composite key.

Your class could be something like this:

import javax.persistence.*;

@Entity
@IdClass(MaquinaID.class)
public class Maquina {

    @Id
    private Integer tipoMaquina;

    @Id
    private Integer numero;

    // [...]
}

Class for composite key:

import java.io.Serializable;

public class MaquinaID implements Serializable {
    private Integer tipoMaquina;
    private Integer numero;

    public MaquinaID() { }

    public MaquinaID(Integer tipoMaquina, Integer numero) {
        this.tipoMaquina = tipoMaquina;
        this.numero = numero;
    }

    // getter e setter
}

Below is the link for you to take a closer look and other options of how to make a composite key:

https://receitasdecodigo.com.br/hibernate/chaves-compostas-com-hibernate

I can’t say what the Hashmap in the comic book would look like, but I believe it’s not the best way to create a composite key.

  • Friend, thank you so much for your help, really! You gave a light to me! Stay with God!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.