1

So, my question is next. How can I map enum type in a database as a table. I want to have a table with values of my Enum.I want to have such structure in database:Model

And such structure of classes in code:

@Entity
public class MainEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    @Column
    private Integer id;

    @Column
    private Enum enum;

    @NotNull
    @Column
    private String any_other_attribute;

}
public enum Enum implements Serializable {
    First, Second, Third
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
Dmitry
  • 43
  • 2
  • 4
  • 1
    Java SE enum types have `Serializable` baked in. In fact, the JVM provides a rock solid guarantee that your constants remain instance controlled at all times. You shouldn't declare an enum type as `Serializable` – scottb May 29 '18 at 18:34
  • @scottb Java enum values are de facto Serializable, not the fields that it may contain. This question may interest you : https://stackoverflow.com/questions/15521309/is-custom-enum-serializable-too. Now I think that having mutable fields in a enum is an anti pattern but well it is another matter... – davidxxx May 29 '18 at 18:40
  • Storing enum ordinal to value as a lookup table is not a great idea, due to then duplicating the info in Java and in the database. If you really want to do it, don't use an enum, and just have an entity with id and name. –  May 30 '18 at 14:58

1 Answers1

0

See below link for the detail explanation of Enum with JPA http://tomee.apache.org/examples-trunk/jpa-enumerated/README.html

You can use Enumerated Annotation.

@Enumerated(EnumType.STRING) private Rating rating;

Palla
  • 1,131
  • 9
  • 11