使用枚举作为映射键会在数据库中生成 RAW
我正在尝试使用枚举作为 Hibernate 中地图的映射键,但 Hibernate 将我的枚举存储为 RAW:
我有这个枚举:
public enum AccountType implements Serializable {
CASH,
CREDIT_CARD,
GIRO,
INVOICE,
OTHER;
}
我正在尝试将其用作地图中的键:
@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),但看起来这对映射键不起作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试为枚举映射定义 Hibernate UserType。 这将允许您指定要在 DDL 中使用的数据库列类型。
请参阅 https://www.hibernate.org/265.html
HTH
汤姆
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
有 @MapKeyEnumerated JPA 注释,但从 Hibernate core 3.6.5 开始,它似乎对更改生成的 DDL 类型没有多大作用。 我仍然看到原始:(
There's the @MapKeyEnumerated JPA annotation, but as of Hibernate core 3.6.5 it doesn't seem to do much to change the generated DDL type. I still see raw :(