views:

432

answers:

1

Hi,

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.

+1  A: 

You might try defining a Hibernate UserType for the enum mapping. This would allow you to specify the db column type to use in the DDL.

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

HTH Tom

Tom