Nursultan
Nursultan

Reputation: 35

How to correctly display an enum in a separate table?

There is an Entity - 'Parameter' which has an enum filed - 'ValueType'

@Data
@Entity
public class Parameter {
    @Id
    @Column(name = "id", unique = true, nullable = false)
    private UUID id;

   @Enumerated(EnumType.String)
   private ValueType type;

}

public enum ValueType {
   STRING,
   NUMBER;
}

What is the best practice to make the 'ValueType' to be a separate table where these tables are related with OneToOne relationship

Upvotes: 0

Views: 390

Answers (1)

Thomas
Thomas

Reputation: 471

As a best practice you wouldn't use a separate table, if you need an enum - your approach looks very good.

If you want to store the values in a separate table, you need to create it as an entity (maybe with only one field, the enum value, which is the primary key also) and link it with ManyToOne / OneToMany. OneToOne doesn't seem to make a lot of sense. (you may look at Bootify.io to do any of this two variants)

Upvotes: 1

Related Questions