Last week I was working on the backend of a project(SEF AcadeMiX) which uses JPA and spring boot. There, I had to add translations of the data also into the database. (Actually, we had already created the relevant tables for that in the initial project) There was a separate table for languages and separate tables for translations. I’m sure this is not clear. So here’s a diagram.
上周,我正在研究使用JPA和spring boot的项目( SEF AcadeMiX )的后端 。 在那里,我不得不将数据的翻译也添加到数据库中。 (实际上,我们已经在最初的项目中为该表创建了相关表。)这里有一个单独的语言表和一个翻译表。 我确定这还不清楚。 所以这是一个图。
Few days before we figured out something which we had missed that is we don’t need a separate table for languages because we hope to use only two languages. So we had a discussion and decided to use Enums for languages.
几天前,我们发现了我们所错过的东西,因为我们只希望使用两种语言,因此不需要单独的语言表。 因此,我们进行了讨论,并决定将Enums用于语言。
什么是枚举? (What are Enums?)
Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. (It has to be a small set of possible values)
枚举是常量列表。 当您需要一个预定义的值列表(它们确实表示某种数字或文本数据)时,应使用枚举。 (必须是一小部分可能的值)
So in this case, our pre-defined languages were 🇱🇰 SI_LK and TA_LK. So first I created the Enum file.
因此,在这种情况下,我们的预定义语言是🇱🇰SI_LK和TA_LK。 所以首先我创建了Enum文件。
public enum Language {
SI_LK,
TA_LK
}
Then I added enums to the relevant entity.
然后,我将枚举添加到相关实体。
@Enumerated
@Column(name = "language", length = 5)
private Language language;//getter and setter
Full code here.
完整代码在这里 。
Here we have to use @Enumerated annotation to say that this is an Enum. In addition, we can use an enum type within the annotation. There are two types.
在这里,我们必须使用@Enumerated批注说这是一个Enum。 另外,我们可以在注释中使用枚举类型。 有两种类型。
EnumType.ORDINAL
EnumType.ORDINAL
EnumType.STRING
EnumType.STRING
Fact: Enum types are also Enums
事实:枚举类型也是枚举
序数 (Ordinal)
Ordinal is the default type. This will map only the index of Enum which is taken from the order of enum list. So, the enum value that’s defined first gets mapped to 0, the second one to 1, and so on. Therefore it saves only an integer in the database. This is efficient but Readability is minimum.
序数是默认类型。 这将仅映射从枚举列表的顺序中获取的Enum索引。 因此,首先定义的枚举值将映射为0,第二个映射为1,依此类推。 因此,它仅在数据库中保存一个整数。 这是有效的,但可读性最低。
串 (String)
This will save the string values of the enum in the database. So the database will be more readable if we use this but this is not much efficient.
这会将枚举的字符串值保存在数据库中。 因此,如果我们使用它,数据库将更具可读性,但是效率不高。
I chose the second option. EnumTupe.STRING so we can get a readable table in the database.
我选择了第二个选项。 EnumTupe.STRING,因此我们可以在数据库中获得可读的表。
@Enumerated(EnumType.STRING)
@Column(name = "language", length = 5)
private Language language;//getter and setter
Now data stores in translation table like this:
现在,数据存储在转换表中,如下所示:
And also it is possible to use customized values of an Enum.
也可以使用枚举的自定义值。
Do you know? — In earlier versions of JPA, they didn’t allow us to use enums as composite keys. But now it’s possible. Just use it normally.
你知道吗? —在JPA的早期版本中,它们不允许我们将枚举用作复合键。 但是现在有可能。 只需正常使用即可。
That's all for today. Thanks for reading.
今天就这些。 谢谢阅读。
翻译自: https://levelup.gitconnected.com/enums-with-jpa-and-springboot-f4351a85216d