How to create JPA class from an N to N relationship

Asked

Viewed 799 times

1

Good afternoon Guys, in my work of java Web I have two tables where the relationship is Manytomany, so I went to the database, created a third table with the key of the other two, but when it comes to generating the entities through the database, Can someone help me? thank you.inserir a descrição da imagem aqui

  • your question was a little superficial, I couldn’t quite understand your problem/doubt

  • then Brumazzi, I have a table in the database called exames_registered, with the key Idexame referencing the table exam the key idCliente referencing the table client, idExame and idCliente in the table exames_registered are foreign keys that form a composite primary key, and when I have the class created from these settings in the bank, it gives that error above.. wanted to know how to proceed when a relationship is n to n.. how to do this in JPA?

  • I believe you will have to create the class manually, by the alert, it seems that JPA does not have support to do the merge.

1 answer

2


To create this table you need instead an object that contains an Id for an entity (Embeddedid). For example:

@Embeddable
public class ExamesCadastradosPK {

    @ManyToMany
    @JoinColumn(name="EXCA_EXAM_ID", referencedColumnName="EXAM_ID")
    private Exame exame;

    @ManyToMany
    @JoinColumn(name="EXCA_CLIE_ID", referencedColumnName="CLIE_ID")
    private Cliente cliente;

    // getters e setters
}

This object has the two keys of the table that wants to merge, but it is not an entity. Note the @Embeddable annotation. Now create an object that uses this Embeddable as the primary key:

@Entity
@Table(name="exames_cadastrados")
public class ExamesCadastrados {

    @EmbeddedId
    private ExamesCadastradosPK examesCadastradosPK;

    // getters e setters
}

Browser other questions tagged

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