How to store Java enumeration in JavaDB?

How to store Java enumeration in JavaDB?

Should I try to match the enumerations with SMALLINT and save the values โ€‹โ€‹only in the source code? The embedded database is used by only one application. Or just save the values โ€‹โ€‹as DECIMAL ? None of these solutions seem good / reliable to me. Are there any better alternatives?

Here is my listing:

 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 read other similar questions on this topic, but the problem or solution does not match my problem. Enum storage in Database field , The best way to store Enum in a database , The best way to store enum values โ€‹โ€‹in a database is String or Int

+6
java enums database orm javadb
source share
2 answers

My preference is as follows:

  • Create a dedicated enumeration table in your database using the columns: YourEnumId smallint, YourEnumName varchar (32).
  • In your business object table, add foreign key references to the enumeration table.
  • Deploy your Java DAO to match enumeration values with minimum values โ€‹โ€‹for the database when saving data or implementing stored procedures that take an enumeration name (i.e. varchar) and translate it to a small value when writing data.

<strong> Benefits

  • Increased normalization (and therefore lower overhead) compared to storing a string value in your database table.
  • Your database data will not be corrupted if your java enumeration (for example, if you change the order of values).
  • Your DAO class can speed up during initialization by checking that the Java enumeration definition matches the contents of the YourEnum table.
  • You can provide views in the database that return String values โ€‹โ€‹(for example, if you or the user want to directly query the table).

This is similar to the cletus solution, except that the enum encoding is stored explicitly in the database, and is not defined as part of the enum definition.

+3
source share

In JPA, you have two options:

  • By name;

  • In order (integer).

I do not like (2). If you change the order of your listing, it will break. This is usually more common in using (1) (in my experience).

I would do the same in JavaDB. Just save the enumeration name as text. The advantage is that you can look at the line and know what it means, rather than trying to figure out what โ€œ3โ€ means in the status_id column.

If space bothers you (and in 99% of cases I wonโ€™t), use an integer or use the 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); } } 
+8
source share

All Articles