Hi Lukas, Pay,
> You're right. not all RDBMS support ENUM type. It's not good idea to let
> ENUM class have ID value.
> The number of enum type in mySql is beginning with 1. Maybe other RDBMS
> would not. even no support number.....
I am not sure I understand the issue here, but it may be similar to
something we do.
In our database, we store all sorts of enumerations as numbers.
There are no particular checks in the database that enforces valid
values, but they are manipulated as Java enums in the application. Our
enums may contain sequential or non-sequential values, in some cases
may have negative values, holes, null allowed. The Java enum enforces
those conditions.
Here is a standard enumeration, with a simple numbering logic:
public enum CivilStatus implements NumberBasedEnum {
SINGLE(1, "Single"),
MARRIED(2, "Married"),
DIVORCED(3, "Divorced"),
WIDOW(4, "Widow"),
;
private static EnumRegistry<CivilStatus> registry = new
EnumRegistry<>(CivilStatus.class);
private String name;
private CivilStatus(int intValue, String name) {
this.name = name;
if(ordinal() != intValue - 1) {
throw new IllegalStateException("The ordinal value must be
the same as the constant value - 1!");
}
}
@Override
public int intValue() {
return ordinal() + 1;
}
@Override
public String toString() {
return name;
}
public static CivilStatus getEnum(int enumvalue) {
return registry.getEnum(enumvalue);
}
}
The interface is very simple:
public interface NumberBasedEnum {
public int intValue();
}
The EnumRegistry class builds an internal map using
"EnumSet.allOf(enumClass)". It even has other methods like "public T
getEnum(String iName)". It also validates that intValues are unique.
We have about 200-300 such enums, so if jOOQ allowed us to say that
column X of table Y should be mapped to a number-based enum, it would
be excellent. I guess it would mean adding the getEnum(int) method to
the super interface, and maybe have to generify that super interface
but these are implementation considerations.
Appologies if I misunderstood and was off-topic.
Cheers,
-Christopher