How to create JPA class from a N to N relationship

1
I have two tables where the relationship is ManyToMany, so I went to the database, I created a third table with the key of the other two, but at the time of generating the entities through the bank data, does not work, could anyone give me a help? Thank you.

    
asked by anonymous 02.04.2016 / 20:20

1 answer

2

To create this table you must first have 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 both table keys that you want to join, 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
}
    
05.04.2016 / 20:18