hibernate3自定义枚举映射类型
来源:优易学  2011-11-25 12:26:11   【优易学:中国教育考试门户网】   资料下载   IT书店
  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>

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训