简体   繁体   中英

Unable to Generate enum type in MySql from JPA entity

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?

Use following code:

public enum Gender {
    MALE, FEMALE;
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM