0
  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 ?

2
  • What version of hibernate are you using? I'm using version 3.5.6-Final and I can't duplicate your problem. Commented Jun 21, 2011 at 2:54
  • i am using 3.5.3 version Commented Jun 21, 2011 at 6:31

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, but not that helpful. If Dhirendra wants to map some arbitrary strings to an enum it would be best to make a specialized UserType for that.
0

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;
 }

}

1 Comment

This is not working and i guess it will not as inside hibernate hibernate is doing Enum.valueOf(EnumType.class, "value"), which will fail as no enum exists for this "manual" string value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.