-->
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.  [ 5 posts ] 
Author Message
 Post subject: Persisting enums
PostPosted: Fri Sep 12, 2008 11:10 am 
Beginner
Beginner

Joined: Fri Sep 12, 2008 10:55 am
Posts: 33
I've been all over the place, but still seem to be lost on the best way to persist enum values that have string representations.

Code:
public enum Codes
{
    LATE("L"),
    ON_TIME("OT"),
    EARLY("E"),
    Unknown("U")
               
    String codeValue;

     Codes( String cv)
          { codeValue= cv; }

     public String valueOf() { return codeValue; } // or whatever here
}


Code:
@Entity
public class ...
{
  private Codes code;
   ...

  @Column(name="code")
  @Enumerated(EnumType.STRING) // not what I'm after
  public getCode()
  {
      return code;
   }
}


Using that as the enum, I want to be able to persist the values of the enum instead - Enumerated(EnumType.STRING) simply puts the enum itself as a string, not the assigned value of that enum (LATE vs. L)....

Maybe I'm asking to much of it ;) I have looked around so much and am still pretty clueless about this scenario. I have seen some very complicated examples of custom enum type classes, etc, but nothing that is any clearer than, well, mud![/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 6:16 am 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
Save it as a user type.For that you will have a class that implements UserType. In the methods that you will implement; get the approprate value of your enum and save and get from DB.
Have your entity property annotated as @Type(type="youEnumType")

_________________
Thanks and regards
Julius Fernandes


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 16, 2008 1:26 pm 
Beginner
Beginner

Joined: Fri Sep 12, 2008 10:55 am
Posts: 33
Is there some kind of decent example of this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2008 4:25 am 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
http://www.hibernate.org/189.html

_________________
Thanks and regards
Julius Fernandes


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2008 4:28 am 
Newbie

Joined: Wed Apr 06, 2005 11:41 am
Posts: 13
http://www.hibernate.org/189.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.