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

时间:2012-10-15 17:01:04

标签: java postgresql jpa netbeans glassfish

我在PostgreSQL数据库的表current_status中有一个枚举类型字段history。枚举定义为:

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

搜索互联网产生了this solution,但似乎需要使用org.hibernate.annotations.TypeDef,我在安装中似乎没有。

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

数据库:PostgreSQL 8.4
App Server:Glassfish 3.1.2.2
IDE:NetBeans 7.2
JDK:7更新7

1 个答案:

答案 0 :(得分:1)

请勿使用@Lob,请尝试使用@Enumerated(EnumType.STRING)。

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