使用JPA存储枚举值 [英] Storing Enum Values with JPA

查看:205
本文介绍了使用JPA存储枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个枚举:

public enum NotificationType {

    Store("S"),
    Employee("E"),
    Department("D"),
    All("A");

    public String value;

    NotificationType(String value) {
        this.value = value;
    }
}

我想存储 S E 而不是商店员工在数据库中。目前,我已将其映射到实体中,如下所示:

I want to store S or E rather than Store or Employee in the database. Currently, I've mapped it in the entity as follows:

@Enumerated(EnumType.STRING)
private NotificationType notificationType;

但如果可能的话,不确定如何得到我想要的东西。

but unsure how to get what I want, if possible.

推荐答案

您可以声明自己的用户类型来进行字符串和枚举之间的转换,您可以在本文中找到更多信息:

You can declare own user type to do the conversion between the strings and the enum you can find out more in this article:

http://javadata.blogspot。 no / 2011/07 / hibernate-and-enum-handling.html

一个小注释UserType是特定于Hibernate的。如果您只想使用JPA。你需要属性转换器。怎么做你可以在这里找到:

One small remark UserType is Hibernate specific. If you want to use JPA only. You need attribute converter. How to do that you can find here:

http://www.thoughts-on-java.org/jpa-21-type-converter-better-way-to/

Stackoverflow上另外一个处理Atribute转换器的链接显示了这种情况的确切实现。
是否可以写一个JPA的通用枚举转换器?

And one more link on Stackoverflow dealing with Atribute converters shows exact implementation of such. Is it possible to write a generic enum converter for JPA?

这篇关于使用JPA存储枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆