Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Fix value and strategy to display value

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have a spring boot, application with rest architecture and JQuery on the client side

Many time in a applicaiton, we have static value: like type of communication (mobile, fix, email...), this kind of value don't change.



Database

Using direct value


1 - Email
2 - Mobile
3 - Fix



or use properties key


1 - EMAIL
2 - MOBILE
3 - FIX




Properties file


EMAIL=Email
MOBILE=Mobile
FIX= Fix




if these values can be displayed in a html page, do you think is better to use an enum, store it in a database maybe there are other solutions.

what to do if we want to support multiple langage? we put key value in the db?

i would like to know what do you in this kind of case.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are multiple issues going on here.

The first one is: how do you store an enum in the database? There are different ways to do this, each with pros and cons.

1. You can store the enum ordinal (the index of the enum constant in the definition of the enum). Pros: simple and you don't need to add extra code to the enum. Cons: if you re-order the enum constants, or add a new constant in between existing constants, the ordinals will change and things break.

2. You can store the name of the enum constant as a string in the database. Pros: simple, you don't need extra mapping code. Cons: strings take up more space than a simple int value (such as an ordinal) in the database.

3. Have some explicit mapping from enum constants to codes, store those codes in the DB, and then translate back and forth as you save / load values from the DB. Pros: You don't have the problem of changing ordinals as with solution 1. Cons: Needs more code, because you need to do this mapping.

The second issue is displaying on the client side. This is a completely separate issue from how you store enum values in the database. You'll need to have some mapping mechanism (for example a Properties file) to convert enum values to display texts. But as I already said, this is separate from how you store enum values in the database. There is no need to store the display texts in the database whenever you need to store an enum value (which you seem to imply when you say "store key/value pairs in the database?").
 
mark smith
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:There are multiple issues going on here.

The first one is: how do you store an enum in the database? There are different ways to do this, each with pros and cons.

1. You can store the enum ordinal (the index of the enum constant in the definition of the enum). Pros: simple and you don't need to add extra code to the enum. Cons: if you re-order the enum constants, or add a new constant in between existing constants, the ordinals will change and things break.

2. You can store the name of the enum constant as a string in the database. Pros: simple, you don't need extra mapping code. Cons: strings take up more space than a simple int value (such as an ordinal) in the database.

3. Have some explicit mapping from enum constants to codes, store those codes in the DB, and then translate back and forth as you save / load values from the DB. Pros: You don't have the problem of changing ordinals as with solution 1. Cons: Needs more code, because you need to do this mapping.

The second issue is displaying on the client side. This is a completely separate issue from how you store enum values in the database. You'll need to have some mapping mechanism (for example a Properties file) to convert enum values to display texts. But as I already said, this is separate from how you store enum values in the database. There is no need to store the display texts in the database whenever you need to store an enum value (which you seem to imply when you say "store key/value pairs in the database?").



thanks for theses information

Actually to store information, i use @Enumerated(EnumType.STRING)
On the client side, i display directly the value of this... that bad

Need to find a library on the client side to be able to do i18n.

Don't know if one exist where i could put a kind of 18n tag in the html file

coderanch is maybe not the place to talk about js...

 
Sheriff
Posts: 26963
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mark smith wrote:coderanch is maybe not the place to talk about js...



Not at all! We have a Javascript /HTML forum: go ahead and ask questions there.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic