enum类型映射到hbm.xml配置文件

1. 性别枚举类型类:Gender.java

 

[java]  view plain copy
  1. /**  
  2.  * Filename: ExportDBScript.java  
  3.  * Author: qiujy  
  4.  * Createtime:Nov 22, 2008  
  5.  * Copyrights 2008 qjyong All rights reserved.  
  6.  * EMail: qjyong@gmail.com  
  7.  */   
  8. package com.qiujy.common.myusertype;   
  9.   
  10. import java.io.Serializable;   
  11.   
  12. /**  
  13.  * 性别枚举类型  
  14.  *   
  15.  * @author qiujy  
  16.  */   
  17.   
  18. public enum Gender implements Serializable {   
  19.     Male("男"0), Female("女"1), Other("保密"2);   
  20.   
  21.     private String name;   
  22.     private int value;   
  23.   
  24.     public String getName() {   
  25.         return name;   
  26.     }   
  27.   
  28.     public int getValue() {   
  29.         return value;   
  30.     }   
  31.   
  32.     private Gender(String name, int value) {   
  33.         this.name = name;   
  34.         this.value = value;   
  35.     }   
  36.   
  37.     public static Gender getGender(int value) {   
  38.         if (0 == value){   
  39.             return Male;   
  40.         }else if (1 == value){   
  41.             return Female;   
  42.         }else{   
  43.             return Other;   
  44.         }   
  45.     }   
  46.     @Override   
  47.     public String toString(){   
  48.         return this.name;   
  49.     }   
  50. }   

2.自定义枚举映射类型类:GenderType.java

 

  1. [java]  view plain copy
    1. /**  
    2.  * Filename: ExportDBScript.java  
    3.  * Author: qiujy  
    4.  * Createtime:Nov 22, 2008  
    5.  * Copyrights 2008 qjyong All rights reserved.  
    6.  * EMail: qjyong@gmail.com  
    7.  */   
    8. package com.qiujy.common.myusertype;   
    9.   
    10. import java.io.Serializable;   
    11. import java.sql.PreparedStatement;   
    12. import java.sql.ResultSet;   
    13. import java.sql.SQLException;   
    14.   
    15. import org.hibernate.Hibernate;   
    16. import org.hibernate.HibernateException;   
    17. import org.hibernate.usertype.UserType;   
    18.   
    19. /**  
    20.  * 自定义hibernate的性别枚举映射类型  
    21.  *   
    22.  * @author qiujy  
    23.  */   
    24. public class GenderType implements UserType {   
    25.   
    26.     /** 告诉Hibernate要使用什么SQL列类型生成DDL */   
    27.     public int[] sqlTypes() {   
    28.         return new int[]{Hibernate.SHORT.sqlType()};   
    29.     }   
    30.        
    31.     /** 告诉Hibernate这个UserType用来映射的数据类型。这里是Gender类 */   
    32.     @SuppressWarnings("unchecked")   
    33.     public Class returnedClass() {   
    34.         return Gender.class;   
    35.     }   
    36.        
    37.     /** 告诉hibernate这个类型是不可变的。有微小的性能优化    */   
    38.     public boolean isMutable() {   
    39.         return false;   
    40.     }   
    41.        
    42.     /**这是用于Hibernate缓存生成的快照,由于Gender是不可变的,直接返回就好了。*/   
    43.     public Object deepCopy(Object arg0) throws HibernateException {   
    44.         return arg0;   
    45.     }   
    46.        
    47.     /** hibernate把这个数据放入二级缓存时要调用的方法  */   
    48.     public Serializable disassemble(Object arg0) throws HibernateException {   
    49.         return (Serializable)arg0;   
    50.     }   
    51.        
    52.     /** 从二级缓存中取这个对象数据时要调用的方法 */   
    53.     public Object assemble(Serializable arg0, Object arg1)   
    54.             throws HibernateException {   
    55.         return arg0;   
    56.     }   
    57.        
    58.     /** 处理脱管对象状态的合并。*/   
    59.     public Object replace(Object original, Object target, Object owner)   
    60.             throws HibernateException {   
    61.         return original;   
    62.     }   
    63.        
    64.     public boolean equals(Object x, Object y) throws HibernateException {   
    65.         return x == y;   
    66.     }   
    67.   
    68.     public int hashCode(Object o) throws HibernateException {   
    69.         return o.hashCode();   
    70.     }   
    71.        
    72.     /** 从JDBC的ResultSet读取属性值。这个方法是在从数据库查询数据时用到。 */   
    73.     public Object nullSafeGet(ResultSet rs, String[] names, Object owner)   
    74.             throws HibernateException, SQLException {   
    75.         int value = rs.getInt(names[0]);   
    76.         return Gender.getGender(value);   
    77.     }   
    78.   
    79.     /** 将属性的值设置到PreparedStatement。 */   
    80.     public void nullSafeSet(PreparedStatement ps, Object value, int index)   
    81.             throws HibernateException, SQLException {   
    82.         if (value == null) {   
    83.             ps.setInt(index, Hibernate.SHORT.sqlType());   
    84.         } else {   
    85.             ps.setInt(index, ((Gender) value).getValue());   
    86.         }   
    87.     }   
    88. }   

    3.在映射文件中使用:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE hibernate-mapping PUBLIC
    3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    5. <hibernate-mapping>
    6.     <class name="com.qiujy.domain.Account" table="account">
    7.         <id name="id" column="id">
    8.         <generator class="native"/>
    9.     </id>
    10.      <property name="loginname" column="login_name"/>
    11.      <property name="pwd"/>
    12.      <property name="gender" type="com.qiujy.common.myusertype.GenderType"/>
    13.      <property name="registedTime" type="timestamp" column="registed_time"/>
    14.     </class>
    15. </hibernate-mapping>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值