java - 使用枚举作为映射键会在数据库中生成 RAW

标签 java hibernate enums map

我正在尝试使用枚举作为 Hibernate 中 map 的映射键,但 Hibernate 将我的枚举存储为 RAW:

我有这个枚举:

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

我试图将其用作 map 中的键:

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

这里发生的是 Hibernate 将枚举作为原始数据而不是 varchar 存储在数据库中:

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

我希望将其存储为 varchar。我尝试添加 @Enumerated(value = EnumType.STRING),但它似乎对映射键不起作用。

最佳答案

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

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

HTH 汤姆

关于java - 使用枚举作为映射键会在数据库中生成 RAW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/681462/

相关文章:

java - 使用随机行顺序测试 csv 文件是否相等 (Junit)

java - 当我按转义键时 JFrame 没有处理

java - 使用数组进行 JPA 查找

java - Jersey Web 服务和 hibernate 一对多映射

swift - 似乎 Swfit.AnyHashable 不适合使用枚举 (as!, as?, as)

java - WEPOS 的 JVM

java - 为什么这个线程在这种情况下不停止?

swift 2 : How to refer to a type of an element in default implementation of a function in a protocol

java - 如何动态编写条件查询?

java - java 通用枚举迭代器