澳门银河网上官方赌场_老品牌值得信赖

ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决java - Unable to Generate enum type in MySql from JPA entity

itPublisher 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

推荐:Java EE 学习笔记 - JPA Entity的生命周期

? JPA即Java Persistence API,是Java EE中针对持久化数据提供的规范。在使用JPA中,我们经常会提到Entity,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?

java java-ee jpa java-ee-7
|
  this question
asked Jun 24 '13 at 5:40 Masudul 16k 2 23 38 1   You might want to give this a try - stackoverflow.com/a/8710305/738746 –  Bhesh Gurung Jun 24 '13 at 5:51

 | 

2 Answers
2

解决方法

Use following code:

public enum Gender {
    MALE, FEMALE;
}

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

|
  this answer
answered Jul 1 '13 at 16:28 user2539717      Not only does this not compile, but also it mixes up Gender and Role and therefore doesn't answer the question, nor explain anything. Why would people upvote this? –  Michel Jung Mar 7 at 22:55

 | 

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 :

推荐:JPA教程

任何企业应用程序通过存储和检索大量数据进行数据库操作。尽管所有的存储管理提供技术,应用程序开发人员通常很难有效地执行数据库操作。 一般情况下,Java开发

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


|
  this answer
answered Jun 24 '13 at 5:57 CycDemo 5,332 7 33 84      mysql has an "enum" data-type: dev.mysql.com/doc/refman/5.7/en/enum.html –  Creature Nov 4 '16 at 17:52

 | 

推荐:Java EE 5的JPA实现与比较(1)

据我个人所了解现在相对名气比较大的JPA的实现有: ?OpenJPA ?GlassFish/TopLink Essentials ?Hibernate EntityManager ?SAP JPA OpenJPA 维护组织:Apache Foud


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。

澳门银河网上官方赌场