Answer the question
In order to leave comments, you need to log in
How to save a Java object with a field of type Enum?
There is a cats table. The color column is of type Enum cats_color.
There is a Cat class
@Entity
@Table(name = "cats")
public class Cat {
@Id
@NotNull
private String name;
@Column(name = "color")
CatColor color;
@NotNull(message = "Возраст кота должен Zбыть")
@Column(name = "tail_length")
private int tailLength;
@Column(name = "whiskers_length")
private int whiskersLength;
public Cat() {
}
public Cat(@NotNull String name, CatColor color, @NotNull(message = "Возраст кота должен Zбыть") int tailLength, int whiskersLength) {
this.name = name;
this.color = color;
this.tailLength = tailLength;
this.whiskersLength = whiskersLength;
}
public enum CatColor {
BLACK,
WHITE;
CatColor() {
}
}
Answer the question
In order to leave comments, you need to log in
Here is my example:
public class User {
@ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER)
@CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
@Enumerated(EnumType.STRING)
private Set<Role> roles;
}
public enum Role {
AUTHOR,
CUSTOMER,
ADMIN
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question