在 Restful 客户端中检索枚举的 PostgreSQL 类型



我在PostgreSQL数据库中的表historycurrent_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;
    }
}

测试 Web 服务时,出现以下错误:

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].

搜索互联网产生了这个解决方案,但它似乎需要使用org.hibernate.annotations.TypeDef,而我的安装中似乎没有。

问:通过 RESTful Web 服务从 PostgreSQL 枚举类型转换为 Java 枚举类型的正确方法是什么?

数据库:PostgreSQL 8.4
应用服务器:眼镜鱼 3.1.2.2
IDE:NetBeans 7.2
JDK:7 更新 7

不要使用 @Lob,请尝试改用 @Enumerated(EnumType.STRING)。

但是,您必须确保java中的名称与postgresql中的名称完全匹配(它们在当前代码中没有)。

最新更新