aspose file tools*
The moose likes Java in General and the fly likes Storing Enum Types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Storing Enum Types" Watch "Storing Enum Types" New topic
Author

Storing Enum Types

Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

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


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
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.
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
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
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Kevin Tysen wrote:

No offense, but I think that's bad style. You should instead use the fact that enums are also objects:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
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

Joined: Apr 23, 2009
Posts: 157

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.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing Enum Types