3

I'm trying to use an enum as a mapkey for a map in Hibernate, but Hibernate stores my enum as a RAW:

I have this enum:

public enum AccountType implements Serializable {
    CASH,
    CREDIT_CARD,
    GIRO,
    INVOICE,
    OTHER;
}

Which I'm trying to use as a key in a map:

@CollectionOfElements
@MapKey(columns =  @Column(name="ACC_TYPE"), 
  targetElement = AccountType.class)
@Column(name="ACCOUNT_NO")
public Map<AccountType, String> getAccounts() {
  return accountMap;
}

What happens here is that Hibernate stores the enum as a raw in the database instead of a varchar:

"Column Name"   "Data Type" 
"COMPANY_ID"    "NUMBER(19,0)"
"ACC_TYPE"      "RAW"   
"ACCOUNT_NO"    "VARCHAR2(255 CHAR)"

I want this to be stored as a varchar. I've tried to add @Enumerated(value = EnumType.STRING), but it looks like that doesn't work on the mapkey.

4

2 回答 2

1

您可以尝试为枚举映射定义 Hibernate UserType。这将允许您指定要在 DDL 中使用的 db 列类型。

https://www.hibernate.org/265.html

HTH 汤姆

于 2009-05-14T16:42:55.337 回答
0

有 @MapKeyEnumerated JPA 注释,但从 Hibernate 核心 3.6.5 开始,它似乎对更改生成的 DDL 类型没有太大作用。我仍然看到原始的:(

于 2011-10-18T15:15:05.117 回答