2013-04-05 127 views
4

我有一个枚举如下。在hibernate hbm中映射枚举?

public enum ExampleEnum { 
    ONE(1), TWO(2); 

    private int action; 

    private ExampleEnum (int action){ 
     this.action = action; 
    } 

    /** 
    * @return the action 
    */ 
    public int getAction() { 
     return action; 
    } 

    /** 
    * @param action the action to set 
    */ 
    public void setAction(int action) { 
     this.action = action; 
    } 


} 

我需要保存整数值而不是ONE和TWO。我怎样才能做到这一点?我有我的hbm下面的配置:

<property name="action"> 
      <column name="ACTION" /> 
      <type name="org.hibernate.type.EnumType"> 
       <param name="enumClass">com.ExampleEnum</param> 
      </type> 
     </property> 

我需要有任何其他配置来保存整数?请帮帮我!

谢谢!

+0

'所有的持久化类必须有一个默认的构造方法(可以是非公共的)这样的话Hibernate就可以使用Constructor.newInstance实例化它们()。建议您有一个默认的构造函数,至少包可见性,以便在Hibernate中生成运行时代理代理。“ – 2013-04-05 10:47:19

+0

https://forum.hibernate.org/viewtopic.php?p=2377095 – Thihara 2013-04-05 10:53:14

+0

可能的重复。看到这个问题:http://stackoverflow.com/questions/9839553/hibernate-map-enum-to-varchar?rq=1 – dcernahoschi 2013-04-05 10:54:19

回答

0

`

<class name="package.class" table="database table name"> 
<id name="get/set parameter....your first attribute" type="int" column="data base columnname"> 
<generator class="increment"/><!--using auto increment--> 
</id> 
<property name="get/set parameter your second attribute"> 
<column name="data base column name"/> 
</property> 
</class>`