Let’s assume that what you want to map is that the genera are from food. That is, an entity Alimento contains a List<Generos>.
Try to do something like this:
@ElementCollection(targetClass = Generos.class)
@CollectionTable(name = "tb_alimentos_generos", joinColumns = @JoinColumn(name = "alimento_id"))
@Column(name = "genero_id", nullable = false)
@Enumerated(EnumType.ORDINAL)
private Set<Generos> generos;
Explanation:
You must use @OneToMany to relate entities. It occurs that the enum is not an entity. For lists of Strings, numbers, dates, enums and @Embeddeds, in JPA 2 onwards there is the annotation @CollectionTable.
The annotation @CollectionTable specifies that the table where the genera of each food will be stored will be the tb_alimentos_genero. The foreign key of this table for the table modeled on the surrounding entity will be modeled by the column alimento_id.
On this tablet, the column genero_id (according to the annotation @Column) will contain the ordinal value of the genus stored in the tuple (according to the @Enumerated(Enumtype.ORDINAL)).
Therefore, in the table tb_alimentos_genero there are two columns: alimento_id which is foreign key and genero_id. Both columns are part of the primary key of this table.
Ah, notice I used Set, nay List. The reason is that the genera of a food do not have order or repetition, since it probably makes no sense to say that the genus A appears before the genus B nor that the genus C may appear two or three times. If you need to consider the possibility of repetitions or a well-defined ordering that is not something simple like the same order the elements are declared in enum, so I recommend that you create an entity (@Entity) to model this.
Ah, and since it doesn’t make sense Set contain the element null, then we have the nullable = false in the annotation @Column.
In case you have a composite primary key in the surrounding table, you will need to use an array of @JoinColumnand should also define the referencedColumnName in each @JoinColumn to do the mapping correctly.
EXTRA: In case you want to do something like a String or a int instead of a enum, the @Enumerated can be waived. To do this with date fields, also use the @Temporal. If you want a @Embedded instead of Enum, use the annotation @AttributeOverrides to specify how the fields of the @Embedded (but perhaps by then it would be better to define a new entity).
							
							
						 
It worked here man, thank you very much!! Great answer, besides giving the solution explained everything right. Vlw!!!
– Victor Hugo