How to reference more than one ID attribute using the jpa API?

Asked

Viewed 219 times

2

My question is whether I can use the annotation @ID several times in a row, if this is possible or if there is another way to reference attributes of the type ID after the first.

import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "t_Turma")
class Turma {
    @Id
    private int id_Professor;
    @Column(name = id_Disciplina)
    private int id_Disciplina;
    @Column(name = id_Quadrimestre)
    private int id_Quadrimestre;
    @Column
    private int id_Turma;
    @Column
    private String plano;

    public int getId_Turma(){
        return this.id_Turma;
        }
    public void setId_Turma(int id_Turma){
        this.id_Turma = id_Turma;
        }
    public String getPlano(){
        return this.plano;
    }
    public void setPlano(String plano){
        this.plano = plano;
    }
}
  • If I understand correctly. Are these ids in the same entity foreign keys? If so, you need to make relationships with the tags Many to Many, One to Many and One to One. In the JPA documentation you will find this detailed information.

1 answer

1


I will separate my answer into parts, answering your question:

Yes, it is possible to have more than one annotation @Id in your class, however, This is a facility that Hibernate provides and is not within the JPA specification.

If you only use JPA, then this nay will work.

Now I will explain how to make JPA patterns

Talk is Cheap, show me the code! - Linus Torvalds

Using @Idclass

This approach consists of creating a class containing the fields that form the identifiers of your entity, and referencing this class in the annotation @IdClass:

public class ProjetoId {
    Long departamentoId;
    Long projetoId;
}

@Entity 
@IdClass(ProjetoId.class)
public class Projeto {

    @Id 
    private Long departamentoId;

    @Id 
    private Long projetoId;

     ...
}

Using @Embeddedid

This approach consists of including a class that represents the entity’s keys directly as an attribute of the class:

@Embeddable
public class ProjetoId {
    Long departamentoId;
    Long projetoId;
}

@Entity 
public class Projeto {

    @EmbeddedId 
    private ProjetoId id;

     ...
}

This provides a good chance of reusing the code, and leaves the class cleaner.

Using multiple @Id

As I mentioned before, this approach only works if you are using Hibernate, you can check the documentation saying this here, and I can also put the quote:

5.1.2.1.2. Multiple id properties without Identifier type

Another, arguably more natural, approach is to place @Id on Multiple properties of your Entity. This approach is only supported by Hibernate (not JPA compliant) but does not require an extra embeddable Component.

With this you can simply declare multiple ids:

@Entity 
public class Projeto {

    @Id 
    private Long departamentoId;

    @Id 
    private Long projetoId;

     ...
}

I’ll leave the link to the JPA specification to take a look.

  • Thank you! It was very well explained :)

  • @Juliana Don’t forget to mark the answer as accepted :)

Browser other questions tagged

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