• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fix value and strategy to display value  RSS feed

 
mark smith
Ranch Hand
Posts: 258
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 258
  • Mark post as helpful
  • send pies
  • 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...

 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!