Hibernate 学习笔记 (1) -- UserType 分析

来源:互联网 发布:算法导论视频教程 编辑:程序博客网 时间:2024/05/06 04:41
 让我们先来看一下UserType的接口定义,

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

import org.hibernate.HibernateException;

public interface UserType {

    
/**
     *返回:映射于这个类型的字段的SQL类型,
     *这种被返回的类型已经被定义在 java.sql.Types中
     *一个类型可以映射在多个字段, 也就是说可返回多个SQL类型
     *
@see java.sql.Types
     *
@return int[]    the typecodes
     
*/

    
public int[] sqlTypes();

    
    
/**
     *返回的class是由 nullSafeGet()    方法调用
     *
@return Class
     
*/

    
public Class returnedClass();

    
/**
     *比较两个实例是否相等
     * 
     *
     * 
@param x 实例 x
     * 
@param y 实例 y
     * 
@return boolean 两个实例相等返回true
     
*/

    
public boolean equals(Object x, Object y) throws HibernateException;

    
/**
     * 返回x的hashcode
     * 
@param x 
     * 
@return int 对象x的hashcode
     
*/

    
public int hashCode(Object x) throws HibernateException;

    
/**
     * 获得一个映射于一条 JDBC Resultset 的实例, 并且要考虑到返回null的情况
     * 
@param rs a JDBC result set
     * 
@param names 与本类型相关的所有字段名称的数组
     * 
@param owner the containing entity???
     * 
@return Object
     * 
@throws HibernateException
     * 
@throws SQLException
     
*/

    
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException;

    
/**
     *把一个映射类型的实例写入 prepared statement
     *  A multi-column type should be written
     * to parameters starting from <tt>index</tt>.
     *
     * 
@param st a JDBC prepared statement
     * 
@param value the object to write
     * 
@param index statement parameter index
     * 
@throws HibernateException
     * 
@throws SQLException
     
*/

    
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException;

    
/**
     * Return a deep copy of the persistent state, stopping at entities and at
     * collections. It is not necessary to copy immutable objects, or null
     * values, in which case it is safe to simply return the argument.
     *
     * 
@param value the object to be cloned, which may be null
     * 
@return Object a copy
     
*/

    
public Object deepCopy(Object value) throws HibernateException;

    
/**
     * Are objects of this type mutable?
     *
     * 
@return boolean
     
*/

    
public boolean isMutable();

    
/*
     *以下两个方法是针对该对象在缓存中的存取, 因为该类型是不可变类型, 因此在存取过程中不作改变。
     *
     
*/

    
public Serializable disassemble(Object value) throws HibernateException;
    
public Object assemble(Serializable cached, Object owner) throws HibernateException;

    
/*
     *不详
     
*/

    
public Object replace(Object original, Object target, Object owner) throws HibernateException;
}

1, 首先定义该类型在数据库中的 sql 类型。 sqlType()
2, 在定义该类型的 java 类型 returnedClass()
3,  定义该类型是否可变 isMutable() 直接影响下面的其它的方法的实现
4, 若该类型可比较, (主要用于排序, 搜索) equals(Object x, Object y)
5,  定义该类型的唯一编码 hashCode(Object obj)
6,  定义该类型的深考贝方法 deepCopy(Object obj)
7,  定义如何向数据库中存储该类型 nullSafeSet(PreparedStatement st, Object value, int index)
8,  定义如何从数据库中取得该类型 nullSafeSet(ResultSet rs, String[] names, Object owner)
9, 其它三种方法针对二级存储, 以后再讨论。
原创粉丝点击