hibernate映射:* hibernate3 自定义枚举映射类型来源: 发布时间:星期四, 2009年2月12日 浏览:270次 评论:0
1. 性别枚举类型类:Gender.java /** * Filename: ExportDBScript.java * Author: qiujy * Createtime:Nov 22, 2008 * Copyrights 2008 qjyong All rights reserved. * EMail: [email protected] */ package com.qiujy.common.myusertype; import java.io.Serializable; /** * 性别枚举类型 * * @author qiujy */ public enum Gender implements Serializable { Male("男", 0), Female("女", 1), Other("保密", 2); private String name; private value; public String getName { name; } public getValue { value; } private Gender(String name, value) { this.name = name; this.value = value; } public Gender getGender( value) { (0 value){ Male; } (1 value){ Female; }{ Other; } } @Override public String toString{ this.name; } } 2.自定义枚举映射类型类:GenderType.java /** * Filename: ExportDBScript.java * Author: qiujy * Createtime:Nov 22, 2008 * Copyrights 2008 qjyong All rights reserved. * EMail: [email protected] */ 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性别枚举映射类型 * * @author qiujy */ public GenderType implements UserType { /** 告诉Hibernate要使用什么SQL列类型生成DDL */ public sqlTypes { {Hibernate.SHORT.sqlType}; } /** 告诉Hibernate这个UserType用来映射数据类型这里是Gender类 */ @SuppressWarnings("unchecked") public Class edClass { Gender.; } /** 告诉hibernate这个类型是不可变有微小性能优化 */ public boolean isMutable { false; } /**这是用于Hibernate缓存Cache生成快照由于Gender是不可变直接返回就好了*/ public Object deepCopy(Object arg0) throws HibernateException { arg0; } /** hibernate把这个数据放入 2级缓存Cache时要思路方法 */ public Serializable disassemble(Object arg0) throws HibernateException { (Serializable)arg0; } /** 从 2级缓存Cache中取这个对象数据时要思路方法 */ public Object assemble(Serializable arg0, Object arg1) throws HibernateException { arg0; } /** 处理脱管对象状态合并*/ public Object replace(Object original, Object target, Object owner) throws HibernateException { original; } public boolean equals(Object x, Object y) throws HibernateException { x y; } public hashCode(Object o) throws HibernateException { o.hashCode; } /** 从JDBCResultSet读取属性值这个思路方法是在从数据库查询数据时用到 */ public Object nullSafeGet(ResultSet rs, String names, Object owner) throws HibernateException, SQLException { value = rs.getInt(names[0]); Gender.getGender(value); } /** 将属性值设置到PreparedStatement */ public void nullSafeSet(PreparedStatement ps, Object value, index) throws HibernateException, SQLException { (value null) { ps.Int(index, Hibernate.SHORT.sqlType); } { ps.Int(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> < name="com.qiujy.do.Account" table="account"> <id name="id" column="id"> <generator ="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"/> </> </hibernate-mapping> 0
相关文章
读者评论发表评论 |