我应该如何在 JavaDB 中存储 Java 枚举?

发布于 2024-09-01 02:54:33 字数 942 浏览 3 评论 0原文

我应该如何在 JavaDB 中存储 Java 枚举?

我是否应该尝试将枚举映射到 SMALLINT 并仅将值保留在源代码中?嵌入式数据库仅由单个应用程序使用。或者我应该将值存储为DECIMAL?这些解决方案对我来说都感觉不好/稳健。有更好的选择吗?

这是我的枚举:

import java.math.BigDecimal;

public enum Vat {
    NORMAL(new BigDecimal("0.25")),
    FOOD(new BigDecimal("0.12")),
    BOOKS(new BigDecimal("0.06")),
    NONE(new BigDecimal("0.00"));

    private final BigDecimal value;

    Vat(BigDecimal val) {
        value = val;
    }

    public BigDecimal getValue() {
        return value;
    }
}

我已经阅读过有关此主题的其他类似问题,但问题或解决方案与我的问题不匹配。 数据库字段中的枚举存储在数据库中存储枚举的最佳方法在数据库中存储枚举值的最佳方式 - String 或 Int

How should I store an Java Enum in JavaDB?

Should I try to map the enums to SMALLINT and keep the values in source code only? The embedded database is only used by a single application. Or should I just store the values as DECIMAL? None of these solutions feels good/robust for me. Is there any better alternatives?

Here is my enum:

import java.math.BigDecimal;

public enum Vat {
    NORMAL(new BigDecimal("0.25")),
    FOOD(new BigDecimal("0.12")),
    BOOKS(new BigDecimal("0.06")),
    NONE(new BigDecimal("0.00"));

    private final BigDecimal value;

    Vat(BigDecimal val) {
        value = val;
    }

    public BigDecimal getValue() {
        return value;
    }
}

I have read other similar questions on this topic, but the problem or solution doesn't match my problem. Enum storage in Database field, Best method to store Enum in Database, Best way to store enum values in database - String or Int

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-09-08 02:54:33

在 JPA 中,您有两种选择:

  1. 按名称;

  2. 按序数(整数)。

我不喜欢(2)。如果您更改枚举的顺序,它就会中断。因此,更常见的是使用 (1)(根据我的经验)。

我也会在 JavaDB 中做同样的事情。只需将枚举名称存储为文本即可。它的优点是您可以查看一行并知道它的含义,而不是试图找出 status_id 列中“3”的含义。

如果您担心空间(99% 的情况下我不会),请使用整数或使用代码。例如:

public enum Vat {
  NORMAL(new BigDecimal("0.25")),
  FOOD(new BigDecimal("0.12")),
  BOOKS(new BigDecimal("0.06")),
  NONE(new BigDecimal("0.00"));

  private static final Map<String, Vat> LOOKUP = new HashMap<String, Vat>();

  static {
    Vat[] values = values();
    for (Vat vat : values) {
      LOOKUP.put(vat.code, vat);
    }
  }

  private final String code;
  private final String value;

  private Vat(String code, BigDecimal value) {
    this.code = code;
    this.value = value;
  }

  public String getCode() { return code; }
  public String getValue() { return value; }

  public Vat fromCode(String code) {
    return LOOKUP.get(code);
  }
}

In JPA, you have two choices:

  1. By name;

  2. By ordinal (integer).

I don't like (2). If you change the order of your enum it breaks. As such it's more common to use (1) (in my experience).

I would do the same in JavaDB. Just store the enum name as text. It has the advantage that you can look at a row and know what it means rather than trying to figure out what "3" means in the status_id column.

If you're concerned about space (and 99% of the time I wouldn't be) either use an integer or use a code. For example:

public enum Vat {
  NORMAL(new BigDecimal("0.25")),
  FOOD(new BigDecimal("0.12")),
  BOOKS(new BigDecimal("0.06")),
  NONE(new BigDecimal("0.00"));

  private static final Map<String, Vat> LOOKUP = new HashMap<String, Vat>();

  static {
    Vat[] values = values();
    for (Vat vat : values) {
      LOOKUP.put(vat.code, vat);
    }
  }

  private final String code;
  private final String value;

  private Vat(String code, BigDecimal value) {
    this.code = code;
    this.value = value;
  }

  public String getCode() { return code; }
  public String getValue() { return value; }

  public Vat fromCode(String code) {
    return LOOKUP.get(code);
  }
}
表情可笑 2024-09-08 02:54:33

我的偏好是执行以下操作:

  • 在数据库中创建一个专用枚举表,其中包含以下列:YourEnumIdsmallint、YourEnumName varchar(32)。
  • 在业务对象表中添加对枚举表的外键引用。
  • 实现 Java DAO 以在持久化数据时将枚举值映射到特定于数据库的smallint 值,或者实现接受枚举名称(即 varchar)并在写入数据时将其转换为smallint 的存储过程。

优点

  • 与将字符串值显式存储在数据库表中相比,标准化程度更高(因此存储开销更低)。
  • 您的数据库数据不会
    如果您的 java 枚举已损坏

    定义更改(例如,如果您
    更改值的顺序)。
  • 您的 DAO 类可能会快速失败
    通过检查初始化
    Java 枚举定义匹配
    YourEnum 表的内容。
  • 您可以提供返回字符串枚举值的数据库视图(例如,如果您或用户希望直接查询表)。

这与 cletus 的解决方案类似,只不过枚举编码显式存储在数据库中,而不是定义为枚举定义的一部分。

My preference is to do as follows:

  • Create a dedicated enum table in your database with columns: YourEnumId smallint, YourEnumName varchar(32).
  • Within your business object table add foreign key references to the enum table.
  • Implement your Java DAO to map enum values to database-specific smallint values when persisting data OR implement stored procedures that accept the enum name (i.e. varchar) and translate it into a smallint when writing the data.

Advantages

  • Increased normalisation (and hence lower storage overhead) compared with storing the string value explcitly in your database table.
  • Your database data will not be
    corrupted
    if your java enum
    definition changes (e.g. if you
    change the ordering of the values).
  • Your DAO class can fail-fast during
    initialisation by checking that the
    Java enum definition matches the
    contents of the YourEnum table.
  • You can provide views onto the database that return the String enum values (e.g. If you or a user wishes to query the table directly).

This is similar to cletus's solution except that the enum encoding is stored explicitly in the database rather than being defined as part of the enum definition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文