日期:2014-05-16  浏览次数:20394 次

Hibernate 客户化映射类型--UserType


Hibernate 的客户化映射类型接口,允许用户以编程的方式创建自定义的映射类型,以实现把持久化类的任意类型的属性映射到数据库

以下例子可以把User类的Integer类型的phone属性映射到user表的varchar类型的phone字段

User.java

package com.wudasong.pojo;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

public class User implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5767515928671889957L;
	
	private Long id;
	private String name;
	private String email;
	private Integer phone;
	private byte sex;
	private Date birthday;
	private String mark;
	private String password;
	private Timestamp registerTime;
	
	public User(){}
	
	public Long getId() {
		return id;
	}
	@SuppressWarnings("unused")
	private void setId(Long id) {
		this.id = id;
	}
	//此处省略User类的其他属性的getXXX()和setXXX()方法

}

自定义UserType类

package com.wudasong.pojo;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class PhoneUserType implements UserType{
	
	private static final int[] SQL_TYPES={Types.VARCHAR};
	
	@Override
	public int[] sqlTypes() {
		return SQL_TYPES;
	}

	@Override
	public Class<Integer> returnedClass() {
		return Integer.class;
	}

	@Override
	public boolean equals(Object x, Object y) throws HibernateException {
		if(x==y){
			return true;
		}else if(x==null||y==null){
			return false;
		}else {
			return x.equals(y);
		}
	}

	@Override
	public int hashCode(Object x) throws HibernateException {
		return x.hashCode();
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		return value;
	}

	@Override
	public boolean isMutable() {
		return false;
	}

	@Override
	public Serializable disassemble(Object value) throws HibernateException {
		return (Serializable) value;
	}

	@Override
	public Object assemble(Serializable cached, Object owner)
			throws HibernateException {
		return cached;
	}

	@Override
	public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
			throws HibernateException, SQLException {
		if(resultSet.wasNull()){
			return null;
		}else{
			String phone=resultSet.getString(names[0]);
			return new Integer(phone);
		}
	}

	@Override
	public void nullSafeSet(PreparedStatement statement, Object value, int index)
			throws HibernateException, SQLException {
		if(value==null){
			statement.setNull(index, Types.VARCHAR);
		}else {
			String phone=((Integer)value).toString();
			statement.setString(index, phone);
		}
	}

	@Override
	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

} 

User.hbm.xml

<?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 package="com.wudasong.pojo" schema="wudasong_work">
	<class name="User" table="user" dynamic-insert="true" dynamic-update="true">
		<id name="id" column="id" type="long">
			<generator class="native" />
		</id>
		<property name="name" column="name" type="string" not-null="true" />
		<property name="email" column="email" type="string" not-null="true" />
                <!-- 自定义类型映射 -->
                <property name="phone" column="phone" type="com.wudasong.pojo.PhoneUserType" not-nu