title: Save enum id instead of ordinal value using Hibernate
body: <p>I have an <code>enum</code> as shown below :</p>
  
  <pre><code>public enum SocialType {
  
      Facebook(1L),
      LinkedIn(2L),
      Twitter(3L),
      Skype(14L),
      Blog(16L),
      Google(21L);
  
      private Long id;
  
      SocialType(Long id) {
          this.id = id;
      }
  
      public Long getId() {
          return id;
      }
  
      public static SocialType parse(Long id) {
          SocialType socialType = null; 
          for (SocialType item : SocialType.values()) {
              if (item.getId() == id) {
                  socialType = item;
                  break;
              }
          }
          return socialType;
      }
  }
  </code></pre>
  
  <p>And I am using this <code>enum</code> in my entity as below (with some additional fields):</p>
  
  <pre><code>private SocialType  socialType ;
  public SocialType getSocialType() {
          return SocialType.parse(this.socialType.getId());
      }
  
      public void setSocialType(SocialType socialType) {
          this.socialType = socialType;
          this.socialType = SocialType.valueOf(socialType.name());
      }
  </code></pre>
  
  <p>When I save the entity to my database using Hibernate, the value is SocialType is being saved as the <code>Ordinal Value</code> of the Enum. Instead of this, I want to save the id to the DB. </p>
  
  <p>How can I do that ? Please help me.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I was still not able to solve it using the earlier given solutions. 
  But found a good tutorial on how to fix this. Sharing it for others benefit.</p>
  
  <p><a href="http://www.gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html" rel="nofollow">http://www.gabiaxel.com/2011/01/better-enum-mapping-with-hibernate.html</a></p>