-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate @Enumerated mapping
PostPosted: Sun Mar 27, 2011 1:50 pm 
Newbie

Joined: Sun Mar 27, 2011 1:26 pm
Posts: 2
(Reposting this as I had earlier posted in the wrong section)

Hibernate provides @Enumerated annotation which supports two types of Enum mapping either using ORDINAL or STRING. When we map using EnumType.STRING, it takes the "name" of the Enum and not the toString() representation of the Enum. This is a problem in scenarios where the database column consists of only one character. For example, I have the following Enum:

Code:
public enum Status{
      OPEN{
       @Override
       public String toString(){
         return "O";}
       },
   
      WAITLIST{
       @Override
       public String toString(){
         return "W";}
       },
   
      COMPLETE{
       @Override
       public String toString(){
         return "C";}
       }
   
    }



When I persist the enum Status.OPEN using @Enumerated(EnumType.STRING), the value that Hibernate tries to store in the database is OPEN. However, my database column consists only one column and hence it throws exception.

One way to overcome this issue is to change the Enum type to hold single characters (like STATUS.O, STATUS.W instead of STATUS.OPEN, STATUS.WAITLIST). However, this reduces readability. Any suggestions to preserve the readability as well as mapping the Enum to a single character column?

Thanks.


Top
 Profile  
 
 Post subject: Re: Hibernate @Enumerated mapping
PostPosted: Sun Mar 27, 2011 6:15 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
You could create a CustomType. With CustomTypes you can completely control how the object is created from Database, and how it is persisted.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.