对列> String <使用ENUMS

时间:2018-11-06 14:33:18

标签: java hibernate spring-boot enums

我想检查是否可以使用类型列枚举。 为什么我不能将字符串设置为枚举的值?

亲切的问候

        @Entity
        @Table(name = "rooms")
        public class Room extends BaseModel {


            private String name;


            private int capacity;

            @Column(name = "change_time")
            private int changeTime;


            @Enumerated(EnumType.STRING)
            public Type type;


            public enum Type {
                LABORATORY("Laboratory"), //This one is not working
                OFFICE,
                COMPUTER_LAB,
                LECTURE_ROOM,
                HALL;
            }


            public Room(String name, int capacity, int changeTime, Type type) {
                this.name = name;
                this.capacity = capacity;
                this.changeTime = changeTime;
                this.type = type;
            }

            Room() {

            }

           ...Getter and Setter........

3 个答案:

答案 0 :(得分:0)

您的枚举中缺少Hello构造函数Type(String)

public enum Type {
    LABORATORY("Laboratory"), //This one is not working
    OFFICE,
    COMPUTER_LAB,
    LECTURE_ROOM,
    HALL;

    private final String val;

    Type() {
        this.val = "";
    }

    Type(String val) {
        this.val = val;
    }
}

答案 1 :(得分:0)

您必须像这样编写适当的构造函数:

public enum Type {

    private String name;

    LABORATORY("Laboratory"),
    /// ...
    HALL("Hall");

    // constructor with String parameter
    Type(String name){
        this.name = name;
    }

    String getName(){
        return name;
    }

}

答案 2 :(得分:0)

您应该通过指定构造函数像下面那样创建枚举。

public enum Type {

    LABORATORY("Laboratory"),
    OFFICE("Office"),
    COMPUTER_LAB("Computer_lab"),
    LECTURE_ROOM("Lecture_room"),
    HALL("Hall");

    private String value;

    private Type(String value)
    {
        this.value = value;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }

}