简体   繁体   English

枚举Java类型的正确DB字段数据类型是什么

[英]What's correct DB field data type of an Enum Java Type

I have this enum created: 我创建了这个枚举:

    public enum CicloEnum {
        CL("Ciclo Largo"),
        CC("Ciclo Corto"),
        CI("Ciclo Intermedio");

        private String descripcion;

        private CicloEnum(String descripcion) {
            this.setDescripcion(descripcion);
        }

        public String getDescripcion() {
            return descripcion;
        }

        public void setDescripcion(String descripcion) {
            this.descripcion = descripcion;
        }
       }

and I want to persist the value in the DB, I'm using hibernate as ORM engine. 并且我想将值保留在数据库中,我正在使用休眠作为ORM引擎。

    @Entity
    @Table(name="bloquecruzamiento")
    public class BloqueCruzamiento {
         //...
         private CicloEnum ciclo;
         //...
        /**
         * @return the ciclo
         */
            public CicloEnum getCiclo() {
            return ciclo;
        }

        /**
         * @param ciclo the ciclo to set
         */
        public void setCiclo(CicloEnum ciclo) {
            this.ciclo = ciclo;
        }

The question is, that I want the field in the Database to be numeric, but is no big deal if it is creates as Varchar. 问题是,我希望数据库中的字段为数字,但是如果将其创建为Varchar则没什么大不了的。 How can I achieve the goal that the field in the database were created as int(2)? 如何实现将数据库中的字段创建为int(2)的目标? what do I need to add to the Enum definition? 我需要在Enum定义中添加什么?

Best Regards 最好的祝福

Since by default, the values of ENUMS in Java are ordinal, you can set them with an array like manner. 由于默认情况下,Java中的ENUMS值是序数,因此可以使用类似数组的方式设置它们。 The only change would be to convert this enum back to an integer value. 唯一的变化是将此枚举转换回整数值。 The change I made to your enum class was to add a function to do so: 我对您的枚举类所做的更改是添加一个函数来做到这一点:

public enum CicloEnum {
    CL("Ciclo Largo"), CC("Ciclo Corto"), CI("Ciclo Intermedio");

    private String descripcion;

    private CicloEnum(String descripcion) {
        this.setDescripcion(descripcion);
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }


    public int toIntForDatabase() {
        switch (this) {
        case CL:
            return 0;
        case CC:
            return 1;
        case CI:
            return 2;
        }
        return -1;
    }
}

Then, you can set the enum by calling MyEnum.values()[x] where MyEnum is the name of your enum, and x is the value of the integer that it would represent. 然后,可以通过调用MyEnum.values()[x]来设置枚举,其中MyEnum是枚举的名称,而x是它将表示的整数的值。 For example, CL is 0, CC is 1, CI is 2. An example test that I wrote is: 例如,CL为0,CC为1,CI为2。我编写的示例测试是:

public static void main(String[] args) throws Exception {

    int i = 1;
    CicloEnum ce = CicloEnum.values()[i];
    System.out.println(ce.getDescripcion());
    System.out.println(ce.toIntForDatabase());

}

This sets an enum based on an integer "i" (the value from your database), and then converts it back to an integer for the database using the toIntForDatabase() function. 这将基于整数“ i”(数据库中的值)设置一个枚举,然后使用toIntForDatabase()函数将其转换回数据库的整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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