开发者

Hibernates generates VARBINARY column with JPA @Enumerated annotation

开发者 https://www.devze.com 2022-12-26 04:35 出处:网络
Hibernates is generating a VARBINARY column with JPA @Enumerated annotation. I\'m using SQL Server 2005 w/ JTDS driver. The mapping is dirt simple:

Hibernates is generating a VARBINARY column with JPA @Enumerated annotation. I'm using SQL Server 2005 w/ JTDS driver. The mapping is dirt simple:

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

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

Note that my app works fine; the column type makes it hard to inspect the DB, and to issue adhoc SQL when poking at th开发者_运维技巧e database.


Might be an SQL Server specific issue (maybe you could have a look into the Hibernate SQL Server dialect sources to find out the data type used).

In general, it is probably not a good idea to use EnumType.ORDINAL mapping, for the same reason it is not a good idea to use the enum ordinals in plain Java code: adding or removing enum values may easily break your code, since it might change the ordinal of an existing enum value. This is discussed in more details in Effective Java, 2nd Ed Chapter 6, Item 31.

I have also found further arguments for this from the DB perspective.


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

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

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


I couldn't reproduce the issue with the version of Hibernate I'm using but my guess is that Hibernate does not recognize your enum property as an @Enumerated property (MyEnum is an enum, right?) and treat it like a Serializable. I know the JPA spec says it's ok to use @Basic with enums but you try without?

0

精彩评论

暂无评论...
验证码 换一张
取 消