简体   繁体   English

如何坚持使用JPA的枚举?

[英]How to persist an enum with JPA?

I'm using MySQL and JPA. 我正在使用MySQL和JPA。

I have an enum that has it's own table. 我有一个枚举,它有自己的表。

I have a field in an entity (entity1) that uses this enum. 我在实体(entity1)中有一个使用此枚举的字段。 This field is annotated with: @Enumeration(EnumType.STRING) . 该字段注释为: @Enumeration(EnumType.STRING)

1 - is it correct to persist this field in entity1 as a column in the db when it has it's own table? 1 - 在拥有它自己的表时,在entity1中将此字段作为db中的列保留是正确的吗?

2 - if I am using @JsonProperty on my other fields and answer to 1 is "yes", must I use @JsonProperty on the enum field too? 2 - 如果我在其他字段上使用@JsonProperty并且回答1是“是”,那么我也必须在枚举字段上使用@JsonProperty吗?

3 - what's the point in having the enum in a separate table? 3 - 将枚举放在单独的表中有什么意义?

Currently, with just the @Enumeration annotation and a column for the enum for entity1 I get error: was annotated as enumerated, but its java type is not an enum 目前,只有@Enumeration注释和entity1的枚举列我得到错误: was annotated as enumerated, but its java type is not an enum

1- How I addressed similar problem was as follows : 1-我如何处理类似的问题如下:

I defined the enum in a separate entity : 我在一个单独的实体中定义了枚举:

@Entity
@Table(name="CALC_METHOD")
public class CalculationMethod {


public CalculationMethod() {
    super();
    // TODO Auto-generated constructor stub
}


@Id
@Enumerated(EnumType.STRING)
@Column(name="METHOD_NAME")
private CalculationMethodId calcMethodID;

@Column(name="DISPLAY_TEXT")
private String displayName;
.
.
.
.

then I refered to it in another entity as follows : 然后我在另一个实体中提到它如下:

@OneToOne
@JoinColumn(name="CALCULATION_METHOD",referencedColumnName="METHOD_NAME")
private CalculationMethod calculationMethod;

that way it's stored in a seprate table, yet referenced from another entity with no duplication ... the point here is that you can't map enum scoped variables so when I needed to store a display name for the enum value, I needed to make it a separate attribute as you see 这种方式它存储在一个单独的表中,但是从另一个没有重复的实体引用...这里的要点是你不能映射枚举范围的变量,所以当我需要存储枚举值的显示名称时,我需要如你所见,使它成为一个单独的属性

3- why to store it in table? 3-为什么要将它存放在桌子上? because from the java POV it was really an enum , and I want to apply some calculation methods polymorpically (like calculate some value in the refering entity using the calculation method, so I defined calculate() method for each calculation method , each with a different implementation then call it while calculating a whole) the I wanted it to be always read with the same value and display name from many places in the code , and If I want to modify the display name, it's done only @ one place -thus consistency and maintainability- 因为从java POV它真的是一个枚举,我想多态地应用一些计算方法(比如使用计算方法在引用实体中计算一些值,所以我为每个计算方法定义了calculate()方法,每个都有不同的实现然后在计算整数时调用它)我希望它总是以相同的值读取并在代码中的许多地方显示名称,如果我想修改显示名称,它只完成@一个地方 - 正一致性和可维护性 -

2- it depends on the requirement and your json model 2-这取决于要求和你的json模型

For your situation I normaly use an entity on BBDD for the ENUM like: 根据您的情况,我通常使用BBDD上的实体作为ENUM:

AuthenticationType
id, name, value : (0, CERT, Certificate)

Where name is the real ENUM and value is the text I want to represent on the views. 其中name是真正的ENUM,value是我想在视图上表示的文本。

For that you need the following: 为此,您需要以下内容:

public enum AuthenticationTypeEnum{
   CERT, PASS;
}

@Entity
@Table(name = "AuthenticationType")
public class AuthenticationType{
    @Column(name = "ID")
    private Long id;
    @Column(name = "NAME")
    @Enumerated(EnumType.STRING)
    private AuthenticationTypeEnum name; // REAL ENUM TYPE
    @Column(name = "VALUE")
    private String value;
    ....
}

@Entity...
class Authentication{
    private String login;
    ...
    @ManyToOne
    private AuthenticationType type; // ENUM USE
    ...
}

In that way you can edit the value of your ENUM on BBDD without changing your code, for me this is one of the best options. 通过这种方式,您可以在不更改代码的情况下在BBDD上编辑ENUM的值,对我而言,这是最佳选择之一。 Hope this helps. 希望这可以帮助。

When you persist the entity use the cascade all on JPA to persist also the enum entity. 当你持久化实体时,在JPA上使用cascade all来持久化enum实体。

NOTE: On normal situations, the enums not change, so you set them only ones. 注意:在正常情况下,枚举不会更改,因此您只设置枚举。 They are a prerequisite to the application so they change on rare circumstances. 它们是应用程序的先决条件,因此它们在极少数情况下会发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM