dpsdce
dpsdce

Reputation: 5460

Enum Mapping in hibernate

  public enum Sources {


      SOURCE_MANUAL("manual"),

      SOURCE_RE_EDITING("re editing");


      private String source;

      private Sources(String source){
          this.source = source;
      }

     public String getSource() {
    return source;
     }
    }  


  Mapping in Domain object as
         @Column(name = "SOURCE")
      @Enumerated(EnumType.STRING)
       public Sources getSource() {
          return this.source;
       }

Issue : the source column in the DB have values (manual, re editing) so when ever i try to load the object i am getting the following exception

   Caused by: java.lang.IllegalArgumentException: No enum const class api.domain.Sources.manual
 [java]     at java.lang.Enum.valueOf(Enum.java:214)
 [java]     at org.hibernate.type.EnumType.nullSafeGet(EnumType.java:124)

am i doing something wrong here ?

Upvotes: 0

Views: 4404

Answers (2)

telm
telm

Reputation: 472

Try upgrading to Hibernate version 3.5.6.

If that doesn't work you could also try overriding the toString() method in the enum and return the enum name, it isn't pretty but it should get you through your problem in the short run.

public enum Sources {


  SOURCE_MANUAL("SOURCE_MANUAL", "manual"),

  SOURCE_RE_EDITING("SOURCE_RE_EDITING", "re editing");


  private String source;
  private String enumName;

  private Sources(String enumName, String source){
      this.source = source;
      this.enumName = enumName;
  }

 public String getSource() {
return source;
 }

 public String toString() {
return enumName;
 }

}

Upvotes: 0

Alex Gitelman
Alex Gitelman

Reputation: 24732

The source property in your enum has no relevance to enumeration mapping. As far as Hibernate is concerned your database must contain values SOURCE_MANUAL and SOURCE_RE_EDITING. Since one of values contains space, it may not be possible to use regular enumeration mapping without migrating database. There may be some hack but it seems that you are better off just using a string for this mapping and converting to enum manually.

Upvotes: 1

Related Questions