Can I configure Play to use mysql enums instead of ints?

后端 未结 1 1617
谎友^
谎友^ 2021-01-21 19:14

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 conv

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 19:33

    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.

    0 讨论(0)
提交回复
热议问题