* hibernate3 自定义枚举映射类型

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

 

  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. import java.io.Serializable;
  10. /**
  11.  * 性别枚举类型
  12.  * 
  13.  * @author qiujy
  14.  */
  15. public enum Gender implements Serializable {
  16.     Male("男"0), Female("女"1), Other("保密"2);
  17.     private String name;
  18.     private int value;
  19.     public String getName() {
  20.         return name;
  21.     }
  22.     public int getValue() {
  23.         return value;
  24.     }
  25.     private Gender(String name, int value) {
  26.         this.name = name;
  27.         this.value = value;
  28.     }
  29.     public static Gender getGender(int value) {
  30.         if (0 == value){
  31.             return Male;
  32.         }else if (1 == value){
  33.             return Female;
  34.         }else{
  35.             return Other;
  36.         }
  37.     }
  38.     @Override
  39.     public String toString(){
  40.         return this.name;
  41.     }
  42. }

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

 

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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值