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
-
- 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;
-
- public class GenderType implements UserType {
-
-
public int[] sqlTypes() {
-
return new int[]{Hibernate.SHORT.sqlType()};
- }
-
-
-
@SuppressWarnings("unchecked")
-
public Class returnedClass() {
-
return Gender.class;
- }
-
-
-
public boolean isMutable() {
-
return false;
- }
-
-
-
public Object deepCopy(Object arg0) throws HibernateException {
-
return arg0;
- }
-
-
-
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();
- }
-
-
-
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
-
throws HibernateException, SQLException {
-
int value = rs.getInt(names[0]);
-
return Gender.getGender(value);
- }
-
-
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>
-
分享到:
相关推荐
hibernate映射枚举类型,hibernate如何映射枚举类型
在Hibernate中如何使用枚举映射,Hibernate中映射枚举类型
struts+hibernate+自定义标签 做的分页模板
NULL 博文链接:https://gwoham-163-com.iteye.com/blog/1895101
JSP 中Hibernate实现映射枚举类型 问题: Java BO类Gender是枚举类型,想在数据库中存成字符串格式,如何编写hbm.xml? public enum Gender{ UNKNOWN(Unknown), MALE(Male), FEMALE(Female); private String ...
hibernate中使用自定义类型映射Oracle LONG类型
hibernate 对象关系映射总结hibernate 对象关系映射总结hibernate 对象关系映射总结
NULL 博文链接:https://chenyi-dt.iteye.com/blog/666611
Hibernate 映射类型与Java 类型的对照表
hibernate自定义查询
hibernate 无主键表映射资源说明: 1. 简单Demo 2. Demo对应的sql语句 3. 对应博客文档
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的...
Hibernate XML配置表映射实例
Hibernate映射解析 七种映射关系
Hibernate3.x关联映射示例,详细教程参考我的Blog:http://blog.csdn.net/boyazuo
Hibernate_关联关系映射配置详解,希望能帮助广大java爱好者
Hibernate4中映射关系图解。
hibernate和java类型转换,hibernate和java类型转换
个人的很详细的Hibernate一对一映射配置详解,对初学者有帮助!