我的枚举类型字段在hbm.xml文件中怎么映射???public enum Sex { NONE{
public String getName(){
return "男女不限";
}
},
Man{
public String getName(){
return "男";
}
},
WOMEN{
public String getName(){
return "女";
}
};
public abstract String getName();
}
public class ProductInfo {
private String id;
private String name;
private Sex sexRequest = Sex.NONE;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Sex getSexRequest() {
return sexRequest;
}
public void setSexRequest(Sex sexRequest) {
this.sexRequest = sexRequest;
}
}<hibernate-mapping package="com.shop.bean.product"> <class name="ProductInfo">
<id name="id" length="20">
<generator class="cn.com.hch.base.hibernate.utils.IdGenerator"/>
</id>
<property name="name" length="50" not-null="true"/>
....
</class>

</hibernate-mapping>在mapping文件中sexRequest的属性怎么映射?

解决方案 »

  1.   

    hibernate3自定义枚举映射类型
    1. 性别枚举类型类:Gender.java
      /**
      */
      package com.qiujy.common.myusertype;
      import java.io.Serializable;
      /**
      * 性别枚举类型
      */
      public enum Gender implements Serializable {
      Male("男", 0), Female("女", 1), Other("保密", 2);
      private String name;
      private int value;
      public String getName() {
      return name;
      }
      public int getValue() {
      return value;
      }
      private Gender(String name, int value) {
      this.name = name;
      this.value = value;
      }
      public static Gender getGender(int value) {
      if (0 == value){
      return Male;
      }else if (1 == value){
      return Female;
      }else{
      return Other;
      }
      }
      @Override
      public String toString(){
      return this.name;
      }
      }
      2.自定义枚举映射类型类:GenderType.java
      /**
      * Filename: ExportDBScript.java
      * Author: examda
      * Createtime:Nov 25, 2008
      * Copyrights 2008 qjyong All rights reserved.
      */
      package com.qiujy.common.myusertype;
      import java.io.Serializable;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import org.hibernate.Hibernate;
      import org.hibernate.HibernateException;
      import org.hibernate.usertype.UserType;
      /**
      * 自定义hibernate的性别枚举映射类型
      */
      public class GenderType implements UserType {
      /** 告诉Hibernate要使用什么SQL列类型生成DDL */
      public int[] sqlTypes() {
      return new int[]{Hibernate.SHORT.sqlType()};
      }
      /** 告诉Hibernate这个UserType用来映射的数据类型。这里是Gender类 */
      @SuppressWarnings("unchecked")
      public Class returnedClass() {
      return Gender.class;
      }
      /** 告诉hibernate这个类型是不可变的。有微小的性能优化 */
      public boolean isMutable() {
      return false;
      }
      /**这是用于Hibernate缓存生成的快照,由于Gender是不可变的,直接返回就好了。*/
      public Object deepCopy(Object arg0) throws HibernateException {
      return arg0;
      }
      /** hibernate把这个数据放入二级缓存时要调用的方法 */
      public Serializable disassemble(Object arg0) throws HibernateException {
      return (Serializable)arg0;
      }
      /** 从二级缓存中取这个对象数据时要调用的方法 */
      public Object assemble(Serializable arg0, Object arg1)
      throws HibernateException {
      return arg0;
      }
      /** 处理脱管对象状态的合并。*/
      public Object replace(Object original, Object target, Object owner)
      throws HibernateException {
      return original;
      }
      public boolean equals(Object x, Object y) throws HibernateException {
      return x == y;
      }
      public int hashCode(Object o) throws HibernateException {
      return o.hashCode();
      }
      /** 从JDBC的ResultSet读取属性值。这个方法是在从数据库查询数据时用到。 */
      public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
      throws HibernateException, SQLException {
      int value = rs.getInt(names[0]);
      return Gender.getGender(value);
      }
      /** 将属性的值设置到PreparedStatement。 */
      public void nullSafeSet(PreparedStatement ps, Object value, int index)
      throws HibernateException, SQLException {
      if (value == null) {
      ps.setInt(index, Hibernate.SHORT.sqlType());
      } else {
      ps.setInt(index, ((Gender) value).getValue());
      }
      }
      }
      3.在映射文件中使用:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
      <hibernate-mapping>
      <class name="com.qiujy.domain.Account" table="account">
      <id name="id" column="id">
      <generator class="native"/>
      </id>
      <property name="loginname" column="login_name"/>
      <property name="pwd"/>
      <property name="gender" type="com.qiujy.common.myusertype.GenderType"/>
      <property name="registedTime" type="timestamp" column="registed_time"/>
      </class>
      </hibernate-mapping>
      

  2.   

    我在JPA里面用javax.persistence.Enumerated设过类似:
    @Enumerated(EnumType.ORDINAL)
    private MyEnum foo;
      

  3.   

    hibernate映射枚举类就两种方式
    1.使用注解,如3楼;
    2.自定义映射类型,如2楼