`

* hibernate3 自定义枚举映射类型

阅读更多

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

 

  1. package com.qiujy.common.myusertype;
  2. import java.io.Serializable;
  3. /**
  4.  * 性别枚举类型
  5.  * 
  6.  * @author qiujy
  7.  */
  8. public enum Gender implements Serializable {
  9.     Male("男"0), Female("女"1), Other("保密"2);
  10.     private String name;
  11.     private int value;
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public int getValue() {
  16.         return value;
  17.     }
  18.     private Gender(String name, int value) {
  19.         this.name = name;
  20.         this.value = value;
  21.     }
  22.     public static Gender getGender(int value) {
  23.         if (0 == value){
  24.             return Male;
  25.         }else if (1 == value){
  26.             return Female;
  27.         }else{
  28.             return Other;
  29.         }
  30.     }
  31.     @Override
  32.     public String toString(){
  33.         return this.name;
  34.     }
  35. }

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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics