我在 PostgreSQL 数据库的表 history
中有一个枚举类型字段 current_status
。枚举定义为:
CREATE TYPE et_status AS ENUM
('PENDING', 'SUCCESS', 'LATE', 'EARLY', 'FAILED', 'IN_PROGRESS', 'EXPIRED');
我在服务器端添加了一个枚举类型,并在相关字段和getter和setter上对类型进行了一些修改。这看起来像:
@Entity
@Table(name = "history")
@XmlRootElement
@NamedQueries({ ... })
public class History implements Serializable {
public enum StatusType implements Serializable
{PENDING, SUCCESS, SUCCESS_LATE, SUCCESS_EARLY,
FAILED, IN_PROGRESS, EXPIRED};
...
@Lob
@Column(name = "current_status")
private StatusType currentStatus;
...
public StatusType getCurrentStatus() {
return currentStatus;
}
public void setCurrentStatus(StatusType currentStatus) {
this.currentStatus = currentStatus;
}
}
在测试网络服务时,出现以下错误:
The object [PENDING], of class [class org.postgresql.util.PGobject], from mapping
[org.eclipse.persistence.mappings.DirectToFieldMapping[currentStatus-->history.current_status]]
with descriptor [RelationalDescriptor(entities.History --> [DatabaseTable(history)])],
could not be converted to [class java.lang.Integer].
上网搜索得到this solution ,但它似乎需要使用 org.hibernate.annotations.TypeDef,我的安装中似乎没有。
问题:通过 RESTful Web 服务将 PostgreSQL 枚举类型转换为 Java 枚举类型的正确方法是什么?
数据库:PostgreSQL 8.4
应用服务器:Glassfish 3.1.2.2
集成开发环境:NetBeans 7.2
JDK: 7 更新 7
最佳答案
不要使用@Lob,请尝试使用@Enumerated(EnumType.STRING)。
但是您必须确保 java 中的名称与 postgresql 中的名称完全匹配(它们在您当前的代码中不匹配)。
关于java - 在 Restful 客户端中检索枚举的 PostgreSQL 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12900452/