枚举映射在hibernate中 [英] Enum Mapping in hibernate

查看:217
本文介绍了枚举映射在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)

我在这里做错什么?

推荐答案

枚举中的属性与枚举映射无关。就Hibernate而言,您的数据库必须包含值 SOURCE_MANUAL SOURCE_RE_EDITING 。由于其中一个值包含空格,可能无法在不迁移数据库的情况下使用常规枚举映射。可能有一些黑客,但似乎你最好只是使用一个字符串进行此映射并手动转换为枚举。

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.

这篇关于枚举映射在hibernate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆