Mapping an enumeration

Asked

Viewed 226 times

3

I have a problem with Ibernate: When I try to give wakeup on the server, it gives me the following error message:

Use of @Onetomany or @Manytomany Targeting an unmapped class.

Saying that I am trying to use these annotations in an uncharted class, but this class in question is an enumeration.

@OneToMany
private List<Generos> generos;

Which annotation should I use to map this enumeration and solve the problem?

NOTE: I have tried using the note @Enumerated(EnumType.ORDINAL) in the getter method, as suggested by the site I searched for but did not solve my problem.

1 answer

3


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).

  • 1

    It worked here man, thank you very much!! Great answer, besides giving the solution explained everything right. Vlw!!!

Browser other questions tagged

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