Mapping an enumeration

Posted on

Question :

I have a problem with hibernate:
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’m trying to use these annotations in an unmapped class, but this class in question is an enumeration.

private List<Generos> generos;

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

I have tried to use the @Enumerated(EnumType.ORDINAL) annotation in the getter method, as suggested by the site I searched for, but it did not solve my problem.


Answer :

Let’s assume that what you want to map is that the genders are food. That is, a Alimento entity contains List<Generos> .

Try doing something like this:

@ElementCollection(targetClass = Generos.class)
@CollectionTable(name = "tb_alimentos_generos", joinColumns = @JoinColumn(name = "alimento_id"))
@Column(name = "genero_id", nullable = false)
private Set<Generos> generos;


You must use @OneToMany to relate entities. It happens that enum is not an entity. For lists of String s, numbers, dates, enum s and @Embedded s, in JPA 2 there is the annotation @CollectionTable .

The @CollectionTable annotation specifies that the table where the genres of each food are to be stored will be tb_alimentos_genero . The foreign key of this table for the modeled table in the bounding entity will be modeled by the alimento_id column.

In this table, the column genero_id (according to the annotation @Column ) will contain the ordinal value of the genre stored in the tuple (according to @Enumerated(Enumtype.ORDINAL) ).

So, in the tb_alimentos_genero table 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, please note that I used Set , not List . The reason is that the genera of a food have neither order nor repetition, since it probably does not make sense to say that the genus A appears before the genus B and that the genus C can appear two or three times. If you need to consider repeating or a well-defined ordering that is not as simple as the order in which the elements are declared in enum , then I recommend that you create an entity ( @Entity ) to model this.

Ah, and since it does not make sense the Set contains 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 @JoinColumn s and you should also set referencedColumnName to every @JoinColumn to do the mapping correctly.

EXTRA : In case you want to do something like String or int instead of enum , @Enumerated can be waived. To do this with date fields, also use @Temporal . If you want a @Embedded instead of the enum, use the @AttributeOverrides annotation to specify how the @Embedded fields will be mapped (but you may want to define a new entity there).


Leave a Reply

Your email address will not be published. Required fields are marked *