Hibernate generates VARBINARY column with JPA @ Added annotation

Hibernate generates a VARBINARY column with JPA @Enumerated annotation. I am using SQL Server 2005 driver with JTDS driver. The mapping is very simple:

@Basic
@Enumerated(EnumType.ORDINAL)
private MyEnum foo;

      

I would expect Hibernate to generate an integer column? I have also tried EnumType.STRING (expecting a varchar column) with no success.

Please note that my application is working fine; the column type makes it difficult to validate the database and will give the adhoc SQL address when loaded into the database.

+2


source to share


3 answers


It was a stupid user error. Contrary to what I wrote in my question, I messed up the "dirty simple" mapping. Instead of declaring my member variable as MyEnum, I had this instead:

@Basic
@Enumerated(EnumType.ORDINAL)
private Enum foo;

      



Well, of course Hibernate had to store binary data, since I said I wanted to store arbitrary enum values. Changing the foo type for MyEnum "fixed" the problem.

+1


source


Perhaps the problem is with SQL Server (perhaps you could look into the sources of Hibernate SQL Server dialects to find out the datatype being used).

In general, it is probably not recommended to use collation EnumType.ORDINAL

, for the same reason it is not recommended to use enum ordinal numbers in plain Java code: adding or removing enumeration values ​​can easily break your code, as it can change the ordinal of an existing enum value. This is discussed in more detail in Effective Java, 2nd Edition Ed. Chapter 6, paragraph 31.



I also found additional arguments for this from a DB perspective .

+1


source


I have not been able to reproduce the problem with the version of Hibernate I am using, but my guess is that Hibernate does not recognize your property enum

as a property @Enumerated

( MyEnum

is enum

, right?) And treat it as a Serializable

. I know the JPA spec says it can be used @Basic

with enums, but can you try?

0


source







All Articles