This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages 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

Storing Enum Types

 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys

I thought about posting this in the JDBC forum, but then I realised that it applies to more than just JDBC. When you need to store enum values, how do you go about it (regardless of whether you use plain old JDBC, Hibernate, properties or anything else)? See my example below. My standard approach is to add a human readable description field to my enum and to store that value. When I retrieve the value and I need the enum again, I use a method to do the mapping for me. This is how I was taught, but is there an obvious alternative approach?




Cheers,
Riaan
 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about something like this?



In this example, gender is an instance of type Gender.

Actually, your question answered a question I had. I was just wondering what the default value of an enum would be. You know, the default value of an int is 0, the default value of an object is null, the default of a boolean is false, so what is the default value of an enum? I suppose it is null? But that seems strange to me because I thought that an enum could have only the values which are in the curly braces, in this case, MALE or FEMALE.

Wait, I think I just got it. A REFERENCE to an enum can have two kinds of values: either an actual enum instance or a null,
and an actual enum instance can have only the values in the curly braces.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Riaan,
Your approach seems to be general in nature. In your code, you could store the name in the enum description. Is there a weakness that your are trying to overcome?

John
 
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
When you use Hibernate, you can tell it how to store it in the database with an annotation. I think that by default it stores the name of the enum constant as a string (so it would store "MALE" or "FEMALE" as a string, if your Person entity contains a Gender member variable). But you can also tell it to store the enum ordinal (as a number). One problem is that if you change the enum (change the names of the constants, or add, remove or put the constants in a different order) you have to be careful that the data in your database might not match the enum anymore.

You use the @Enumerated annotation to tell Hibernate how to map it. For example:

 
Sheriff
Posts: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin Tysen wrote:


No offense, but I think that's bad style. You should instead use the fact that enums are also objects:
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Riaan,
Enumeration allows the creation of constants to represent a finite, small, set of values that a variable can have. The enumeration values should be distinct enough to represent the information of uniqueness, without additional text descriptor. In the code, the values may need to be represented in different way, Mrs, Madam, etc, which could be handled with a switch statement at that location in the code, or the values can be represent by the string equivalent using enum.value().
I do think that enumeration plays an important role, that there are times when a variable that can assume a small set of values. One could write a class equivalent, that has a switch on an integer value, but it's hard to see the advantage.
I think it is better style to separate representation/storage of information (Mr/Mrs) from presentation of information (Mister/Miss).

John
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:When you use Hibernate, you can tell it how to store it in the database with an annotation. I think that by default it stores the name of the enum constant as a string (so it would store "MALE" or "FEMALE" as a string, if your Person entity contains a Gender member variable). But you can also tell it to store the enum ordinal (as a number). One problem is that if you change the enum (change the names of the constants, or add, remove or put the constants in a different order) you have to be careful that the data in your database might not match the enum anymore.

You use the @Enumerated annotation to tell Hibernate how to map it. For example:


Thank Jesper, the @Enumerated annotation was along the lines of what I was looking for. You make a valid point in that existing data will no longer be valid as soon as an enum is changed; but that applies to pretty much any code using enums.

@John
I agree that it's a good idea to separate presentation and storage. I thought about that as well, but how would you handle searching? I.e., your enum could contain M and F as constants, but you display "Man" and "Woman" on your user interface; now a user comes along and tries to search for all males using the word "Man". Somehow, when you construct a SQL query to return all males, your application needs to know that it should search on "M" instead of "Man". It's easy enough to program the app in such a way that "Man" will be converted to "M" before going into a SQL query, but that becomes trickier when your enum has a large number of values. To answer your question, I'm not trying to solve any particular problem, but I'm curious as to how other developers would handle this scenario.

@Kevin
You're spot on - enums can have either a valid enum value (i.e. one of the values between the curly brackets), or null. They're objects, so just like any other object, enums will default to null if you don't initialize it to a specific value. My question relates specifically to how one can store an enum value on persisted data. Yes, the gender field could be of type Gender, but if you tried to write that away to SQL (using JDBC for instance), your database will have no idea what the Gender type is.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Riaan Nel wrote:My question relates specifically to how one can store an enum value on persisted data. Yes, the gender field could be of type Gender, but if you tried to write that away to SQL (using JDBC for instance), your database will have no idea what the Gender type is.



Just store enums as strings.
Enum does all required conversions for you, the only you need is to call 2 functions: toString() and valueOf():


You can also define a constraint on a database column to prevent values (strings) other than strings from enum,
this will prevent errors when someone stores (via direct update on the database table) unpermitted string in the column.
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:Just store enums as strings.



Depends on how long you want to store it. When I store enums to MySql, I define the column as an enum, like this:


patentFlavor enum('design', 'plant','sir','utility', 'reexam')


That way MySql and the Java Enum can handle the mapping, and MySql only uses 3 bits in the database record
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic