javasql-serveroraclehibernateenums

Ordinal enum mapping with Hibernate 6.5


I'm upgrading Hibernate from 6.1.7 to 6.5.2, there are many enum columns in my entities marked with @Enumerated(EnumType.ORDINAL). My codebase has to run against both Oracle and SQL Server.

The problem is change in Hibernate to map enums from SMALLINT to TINYINT (if enums have only few values). I already have DB schema fixed for Oracle as NUMBER(10, 0) and for SQL Server as INT for ordinal enums.

I can't seem to find solution to map enums with annotations that would work for both DBs at the same time. Currently it works for Oracle, but schema validation for SQL Server throws:

Schema-validation: wrong column type encountered in column [XXXX] in table [XXX]; found [int (Types#INTEGER)], but expecting [smallint (Types#SMALLINT)]

Any idea how to fix this?

I've tried to add @JdbcTypeCode(Types.SMALLINT), same errors. Also @Column(columnDefinition = "int") which fixes SQL Server, but Oracle starts failing with schema validation:

Schema-validation: wrong column type encountered in column [definitionRef_targetType] in table [m_acc_cert_campaign]; found [number (Types#NUMERIC)], but expecting [int (Types#TINYINT)]


Solution

  • Solution is to annotate it like this:

    @JdbcType(IntegerJdbcType.class)
    @Enumerated(EnumType.ORDINAL)
    

    @JdbcType will force SqlTypes.SMALLINT as it was previously implemented directly in EnumJavaType.