Question

I am using JPA 2.1. I want to genrate mysql enum type column like: gender enum('male','female'). My Enum class is

public enum Gender {
  MALE,
  FEMALE
}

In JPA entity class

@Enumerated
private Gender gender;//generate int type column

And

@Enumerated(EnumType.STRING)
private Gender gender; //generate varchar type column. 

Is there any way to generate enum type column in MySql?

Était-ce utile?

La solution

Use following code:

public enum Gender {
    MALE, FEMALE;
}

@Enumerated(EnumType.STRING)
@Column(columnDefinition = "ENUM('User', 'Moderator', 'Admin')")
public Role role;
}

Autres conseils

If you would like to use string enumeration value in database, the column type must be varchar base on database sever. Actually, I am not clear I want to genrate mysql enum type column. But, if u would like to display the output MALE to male, reference as below :

public enum Gender {
    FEMALE("female"), MALE("male");

    private String label;

    private Gender(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }
}

test calss

public static void main(String args[]) {
    System.out.println(Gender.MALE)
}

Output : male

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top