JPA:使用EnumConverter在Oracle数据库中为枚举持久化整数和字符串值。

huangapple go评论52阅读模式
英文:

JPA: Persist both integer and string values for an enum in oracle database using EnumConverter

问题

以下是您要的代码部分的翻译:

我有一个枚举称为MyEnum

Enum MyEnum {
ONE,
TWO;
}


我有一个JPA实体,称为MyEntity

@Entity
class MyEntity {
int id;
String name;
MyEnum someValue;
MyEnum someValueFreezed;


对于MyEntity表中的`MyEnum`值之一,我想将其存储为整数(0, 1, ..),而对于第二列,我想将值存储为字符串(one, two等)。表格应如下所示

| ID | Name | Some_Value | Some_Value_Freezed |
| --- | --- | --- | --- |
| 1 | abc | 1 | One |
| 2 | efg | 2 | Two |

我已经放置了`AttributeConvertes`。但是,使用它,我只能持久化整数或字符串,但不能同时持久化两者。

尽管这似乎是一个不好的想法,但由于一些业务要求,我必须这样做。

在同一个表格中的两个不同列中,同时将相同的枚举持久化为整数和字符串的方法是什么?


<details>
<summary>英文:</summary>

I have an enum, say MyEnum

Enum MyEnum {
ONE,
TWO;
}

and I have a JPA entity, say MyEntity

@Entity
class MyEntity {
int id;
String name;
MyEnum someValue;
MyEnum someValueFreezed;



For one of the `MyEnum` value in the MyEntity table, I want to store it as a integer (0, 1, ..) and for second column, I want to store the value in DB as string (one, two, etc.). Table should look like

| ID | Name | Some_Value | Some_Value_Freezed |
| --- | --- | --- | --- |
| 1 | abc | 1 | One |
| 2 | efg | 2 | Two |


I have `AttributeConvertes` in place. But with that I am able to persist only either integer or string, but not both. 

Although it may seem to be a bad idea, but due to some business requirement, I has to be this way. 

What is the way to persist same enum both as integer and as a string, at same time, in same table, in two different columns?

</details>


# 答案1
**得分**: 3

@Enumerated(EnumType.STRING) 会将枚举保存为数据库列中的字符串,使用枚举常量的名称(在您的情况下为 "ONE" 和 "TWO")来保存。

@Enumerated(EnumType.ORDINAL) 将使用表示枚举常量的序数值的整数值保存到数据库列中。

在使用 EnumType.STRING 时,请确保不要稍后更改枚举常量的名称,因为这会破坏映射;在使用 EnumType.ORDINAL 时,请注意不要稍后更改声明枚举的顺序,因为这会更改它们的序数值。

<details>
<summary>英文:</summary>

When mapping Enums to a database column JPA has 2 default mapping modes that you choose with an annotation:

    @Enumerated(EnumType.STRING)

Will save the enum by saving the name (&quot;ONE&quot; and &quot;TWO&quot; in your case) of the enum constant as a String in the database column.

    @Enumerated(EnumType.ORDINAL)

Will use an Integer value representing the ordinal value of the Enum constant to save to the database column.

When using EnumType.STRING make sure you don&#39;t later change the names of Enum constants as that would break the mapping and when using EnumType.ORDINAL take care not to later change the order you declared the Enums as that would change their ordinal value.

</details>



# 答案2
**得分**: 0

你可以使用一个设置器来设置两个字段。

    private String id;
    private String name;
    
    @Transient
    public void setMyEnum(MyEnum e) {
        this.id = e.getId();
        this.name = e.getName();
    }

<details>
<summary>英文:</summary>

You could use a single setter to set two fields.

    private String id;
    private String name;
    
    @Transient
    public void setMyEnum(MyEnum e) {
        this.id = e.getId();
        this.name = e.getName();
    }

</details>



huangapple
  • 本文由 发表于 2023年2月8日 21:06:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定