【发布时间】:2012-08-22 03:26:03
【问题描述】:
我尝试使用@Enumerated 注释来映射我的枚举类型,但出现以下错误:
Exception in thread "main" java.lang.ClassCastException: org.postgresql.util.PGobject cannot be cast to java.lang.String
我做什么: 在postgres中
create type "test_type" as enum ('test_1', 'test_2);
在java中
公共枚举 TestType{ test_1, test_2 }
@Entity @Table(name="test_table") 公共类 TestTable { ...
@Enumerated(EnumType.STRING) @Column(name="col") 私有 TestType 上校; ... }
【问题讨论】:
-
这是使用试图与数据库无关的数据库访问层的成本之一。它通常无法使用底层数据库引擎的全部功能。对于枚举,您可以编写特定于持久性提供者的数据类型转换器或使用特定于提供者的扩展注释,如 EclipseLink 的枚举映射,但当然它只适用于该持久性提供者。参见例如wiki.eclipse.org/EclipseLink/Examples/JPA/… 和stackoverflow.com/questions/10898369/…
标签: postgresql jpa enums