我可以配置Play来使用mysql枚举而不是int吗? [英] Can I configure Play to use mysql enums instead of ints?
问题描述
我的模型包含一个枚举,它由JPA映射到我的mysql数据库的 int
列中。
mysql有一个本地枚举
类型,这会更方便使用。
如何配置JPA使用此类型?
@Entity
public class用户扩展模型{
public enum Role {
用户,
主持人,
管理员,
}
公共角色角色;
对于枚举AFAIK,但也许你可以尝试这个解决方法(我从来没有测试它认为):
@Entity
public class User extends Model {
public enum Role {
User,
Moderator,
Admin,
}
@Enumerated(EnumType。 STRING)
@Column(columnDefinition =ENUM('User','Moderator','Admin'))
public role role;
}
您可以使用 EnumType.STRING ,它会将值存储为数据库中的字符串。
但是使用native ENUM要求您使用 @Column 注释定义columnDefinition这需要你所有的角色在那里进行编码,你看?在这里重复。
My model contains an enum, which are mapped by JPA to an int
column on my mysql database.
mysql has a native enum
type, that would be more convenient to use.
How can I configure JPA to use this type?
@Entity
public class User extends Model {
public enum Role {
User,
Moderator,
Admin,
}
public Role role;
}
There is no built in support for enum AFAIK, but maybe you can try this as workaround (I never test it thought):
@Entity
public class User extends Model {
public enum Role {
User,
Moderator,
Admin,
}
@Enumerated(EnumType.STRING)
@Column(columnDefinition = "ENUM('User', 'Moderator', 'Admin')")
public Role role;
}
You can use EnumType.STRING which will store the value as String in the database.
But using native ENUM require you to define the columnDefinition using @Column annotation which need all your roles to be harcoded there, you see? duplication here.
这篇关于我可以配置Play来使用mysql枚举而不是int吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!