【问题标题】:Why Does this JPA enum not work?为什么这个 JPA 枚举不起作用?
【发布时间】:2011-02-03 01:17:26
【问题描述】:

它在数据库中存储一个整数,而不是我要求的字符串。

这是包含枚举的类。

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Document extends BaseModel {

    private String title = new String();
    private String description = new String();

    **@Enumerated(EnumType.STRING)
    private DocumentType documentType;**

    @Embedded
    private DocumentImage documentImage;
    // if document should be displayed or published on the web site.
    private Boolean published = new Boolean(false);

    public Document(DocumentType docType) {
        super();
        documentType = docType;
        setDocumentImage(new DocumentImage());

    }

}

这里是枚举类:

public enum DocumentType  {
    policy,procedure,webbookmark,newsrelease,collectionLetter,whitepaper,busform,
    newsarticle ;
}

我知道这应该可行。有什么想法吗?

【问题讨论】:

  • 如你所说,应该可以正常工作。适用于其他 JPA 提供程序(例如 DataNucleus)
  • 看起来不错。 - 你的方言配置是否正确,是否是正确包中的枚举注释?

标签: mysql hibernate jpa enums


【解决方案1】:

一个可能的原因是您的@Enumerated 注释没有生效,因为BaseModel 中的注释被放置在属性而不是字段上。字段或属性上的注释位置应在继承层次结构中保持一致。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2021-10-21
    相关资源
    最近更新 更多