2013-04-05 126 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>`