软件世界网 购物 网址 三丰软件 | 小说 美女秀 图库大全 游戏 笑话 | 下载 开发知识库 新闻 开发 图片素材
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
移动开发 架构设计 编程语言 Web前端 互联网
开发杂谈 系统运维 研发管理 数据库 云计算 Android开发资料
  软件世界网 -> 数据库 -> Hibernate中的自定义类型——UserType、CompositeUserType -> 正文阅读

[数据库]Hibernate中的自定义类型——UserType、CompositeUserType


一、UserType
Hibernate拥有自定义映射表属性的机制,主要通过实现接口UserType,具体的UserType:
import java.sql.PreparedStatement;     
import java.sql.ResultSet;     
import java.sql.SQLException;     
     
import net.sf.hibernate.HibernateException;     
     
/**   
 * @author hy-he   
 *   
 */     
public interface UserType {     
     
 /**   
  * 返回UserType所映射字段的SQL类型(java.sql.Types)   
  * 返回类型为int[],其中包含了映射个字段的SQL类型代码   
  * (UserType可以映射到一个或者多个字段)   
  * @return   
  */     
 public int[]sqlTypes();     
     
     
 /**   
  * UserType.nullSafeGet()所返回的自定义数据类型   
  * @return   
  */     
 public Class returnedClass();     
     
     
 /**   
  * 自定义数据类型的比对方法   
  * 此方法将用作脏数据检查,参数x、y分别为数据的两个副本   
  * 如果equals方法返回false,则Hibernate将认为数据发生变化,并将变化更新到数据库表中   
  * @param x   
  * @param y   
  * @return   
  * @throws HibernateException   
  */     
 public boolean equals(Object x,Object y)throws HibernateException;     
     
     
 /**   
  * 从JDBC ResultSet读取数据,将其转换为自定义类型后返回   
  * (此方法要求对克能出现null值进行处理)   
  * names中包含了当前自定义类型的映射字段名称   
  * @param rs   
  * @param names   
  * @param owner   
  * @return   
  * @throws HibernateException   
  * @throws SQLException   
  */     
 public Object nullSafeGet(ResultSet rs,String[] names,Object owner)throws HibernateException,SQLException;     
     
     
 /**   
  * 本方法将在Hibernate进行数据保存时被调用   
  * 我们可以通过PreparedStateme将自定义数据写入到对应的数据库表字段   
  * @param st   
  * @param value   
  * @param index   
  * @throws HibernateException   
  * @throws SQLException   
  */     
 public void nullSafeSet(PreparedStatement st,Object value,int index)throws HibernateException,SQLException;     
     
     
 /**   
  * 提供自定义类型的完全复制方法   
  * 本方法将用构造返回对象   
  * 当nullSafeGet方法调用之后,我们获得了自定义数据对象,在向用户返回自定义数据之前,   
  * deepCopy方法将被调用,它将根据自定义数据对象构造一个完全拷贝,并将此拷贝返回给用户   
  * 此时我们就得到了自定义数据对象的两个版本,第一个是从数据库读出的原始版本,其二是我们通过   
  * deepCopy方法构造的复制版本,原始的版本将有Hibernate维护,复制版由用户使用。原始版本用作   
  * 稍后的脏数据检查依据;Hibernate将在脏数据检查过程中将两个版本的数据进行对比(通过调用   
  * equals方法),如果数据发生了变化(equals方法返回false),则执行对应的持久化操作   
  *   
  * @param value   
  * @return   
  * @throws HibernateException   
  */     
 public Object deppCopy(Object value)throws HibernateException;     
     
     
 /**   
  * 本类型实例是否可变   
  * @return   
  */     
 public boolean isMutable();     
}     
例子:一个学生有联系地址,而联系地址又分为家庭地址和工作地址,我们可以把两个地址信息抽象成一个新的Address类,作为Student的成员变量。

数据库结构:
 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifcreate table typestu (id varchar(32primary key,name varchar(32),homeaddr varchar(32),workaddr varchar(32));
Hibernate.cfg.xml
 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<?xml version='1.0' encoding='UTF-8'?>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
<!DOCTYPE hibernate-configuration PUBLIC
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
<!-- Generated by MyEclipse Hibernate Tools.                   -->
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
<hibernate-configuration>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
<session-factory>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="connection.username">root</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="connection.url">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312
&amp;useUnicode=true
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="dialect">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        org.hibernate.dialect.MySQLDialect
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="myeclipse.connection.profile">mysql</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="connection.password">1234</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="connection.driver_class">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        com.mysql.jdbc.Driver
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="hibernate.dialect">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        org.hibernate.dialect.MySQLDialect
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="hibernate.show_sql">true</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<property name="current_session_context_class">thread</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<mapping resource="Search/UserType/Student.hbm.xml" />
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
</session-factory>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
</hibernate-configuration>
 Pojo
 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackage Search.UserType;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif
public class Student {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
private String id; //标识id
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
    private String name; //学生姓名
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
    private AddressType address;//地址
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif
    public String getId() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return id;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public void setId(String id) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
this.id = id;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public String getName() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return name;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public void setName(String name) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
this.name = name;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public AddressType getAddress() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return address;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public void setAddress(AddressType address) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
this.address = address;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif

 
自定义类型
 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
package Search.UserType;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.io.Serializable;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.sql.PreparedStatement;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.sql.ResultSet;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.sql.SQLException;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.sql.Types;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.apache.commons.lang.builder.EqualsBuilder;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.apache.commons.lang.builder.HashCodeBuilder;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.hibernate.HibernateException;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.hibernate.usertype.UserType;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif
public class AddressType implements UserType, Serializable {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
private String homeAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
private String workAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 有几个字段就有几个值,这里容易出错,要多注意 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
private static final int[] SQL_TYPES = { Types.VARCHAR, Types.VARCHAR };
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 这个方法告诉Hibernate在成生DDL时对列采用什么样的SQL语法 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public int[] sqlTypes() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return SQL_TYPES;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/*
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * Hibernate返回什么样的映射类型,与 <property name="address" type="model.AddressType">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 指定的类一致。事实上也可以把AddressType拆分为两个类,一个类是只携带信息的JavaBean,它里面
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 没有逻辑操作也没有实现UserType(比如AddressBean);而另一个类实现了UserType,它所面对的就不是现在这个
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * AddressType类的homeAddr和homeAddr属性,它面对的是AddressBean。在本例中为了简洁方便,只用了一个类。
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     
*/

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public Class returnedClass() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return AddressType.class;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/*
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 表明这个类的实例在创建以后就不可以改变属性。Hibernate能为不可改变的类作一些性能优化。
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     
*/

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public boolean isMutable() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return false;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/*
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 由于AddressType是不可变的,所以深拷贝可以直接返回对象引用。拷贝的对象由应用程序使用, 而原版对象由Hibernate维护以做脏数据检查
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     
*/

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public Object deepCopy(Object value) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return value; // Address is immutable
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif
    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 两个对象是否相等,使用了apache的common工具包来进行属性比对 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public boolean equals(Object x, Object y) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
if (x == y)
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            
return true;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
if (x == null || y == null)
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            
return false;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        AddressType add1 
= (AddressType) x;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        AddressType add2 
= (AddressType) y;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return new EqualsBuilder() //使用EqualsBuilder类来方便地进行比对
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
                .append(add1.getHomeAddr(), add2.getHomeAddr()).append(
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                        add2.getWorkAddr(), add2.getWorkAddr()).isEquals();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 得到hash码 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public int hashCode(Object x) throws HibernateException {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        AddressType address 
= (AddressType) x;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return new HashCodeBuilder()//使用HashCodeBuilder类来方便地进行比对
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
                .append(address.getHomeAddr()).append(address.getWorkAddr())
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                .toHashCode();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 读取数据并组装成一个AddressType对象。names[]中的参数顺序依照映射文件中定义的顺序 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif            
throws HibernateException, SQLException {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
if (rs.wasNull())
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            
return null;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        String homeAddr 
= rs.getString(names[0]);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        String schoolAddr 
= rs.getString(names[1]);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        AddressType address 
= new AddressType(homeAddr, schoolAddr);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return address;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 保存数据,index的顺序按照映射文件定义的顺序,从0开始。 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
public void nullSafeSet(PreparedStatement st, Object value, int index)
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif            
throws HibernateException, SQLException {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        AddressType address 
= (AddressType) value;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        
if (value == null{
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif              st.setNull(index, Types.VARCHAR);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif              st.setNull(index
+1, Types.VARCHAR);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif        }
 else {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            st.setString(index, address.getHomeAddr());
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            st.setString(index 
+ 1, address.getWorkAddr());
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif         System.out.println(
"Data has been saved! ");
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 当把AddressType类型数据写入二级缓存时,此方法被调用 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public Serializable disassemble(Object value) throws HibernateException {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return null;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/* 当从二级缓存中读取AddressType类型数据时,此方法被调用 */
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
public Object assemble(Serializable cached, Object owner)
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif            
throws HibernateException {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
// TODO 自动生成方法存根
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
        return null;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    
public Object replace(Object original, Object target, Object owner)
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif            
throws HibernateException {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
// TODO 自动生成方法存根
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
        return null;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public AddressType() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
super();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public AddressType(String homeAddr, String workAddr) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
super();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
this.homeAddr = homeAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
this.workAddr = workAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/**
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 
@return 返回 sQL_TYPES。
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     
*/

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public static int[] getSQL_TYPES() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return SQL_TYPES;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/**
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 
@return 返回 homeAddr。
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     
*/

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public String getHomeAddr() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return homeAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
/**
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     * 
@param homeAddr
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif     *            要设置的 homeAddr。
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif     
*/

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
private void setHomeAddr(String homeAddr) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
this.homeAddr = homeAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
private String getWorkAddr() {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
return workAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
private void setWorkAddr(String workAddr) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
this.workAddr = workAddr;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif

 
Student.hbm.xml
 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<?xml version="1.0" encoding="utf-8"?>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
<!-- 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    Mapping file autogenerated by MyEclipse - Hibernate Tools
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
-->
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
<hibernate-mapping>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
<class name="Search.UserType.Student" table="typestu" lazy="false">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        
<id name="id" unsaved-value="null" type="string" column="id">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif            
<generator class="uuid.hex" />
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        
</id>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        
<property name="name" type="string" />
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        
<property name="address" type="Search.UserType.AddressType">
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif            
<column name="homeaddr"/>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif            
<column name="workaddr"/>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif        
</property>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    
</class>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
</hibernate-mapping>
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif

 
测试代码:
 
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackage Search.UserType;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.io.File;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.io.FileInputStream;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.io.FileNotFoundException;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.io.IOException;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.sql.Blob;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import java.sql.Clob;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.hibernate.Hibernate;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.hibernate.Session;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.hibernate.SessionFactory;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.hibernate.Transaction;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
import org.hibernate.cfg.Configuration;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif
public class Test {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif    
public static void main(String[] args) {
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        String filePath
=System.getProperty("user.dir")+File.separator+"src/Search/UserType"+File.separator+"hibernate.cfg.xml";
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        File file
=new File(filePath);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        SessionFactory sessionFactory
=new Configuration().configure(file).buildSessionFactory();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Session session
=sessionFactory.openSession();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Transaction tx
=session.beginTransaction();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        AddressType address
=new AddressType("home","work");
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        Student stu
=new Student();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        stu.setName(
"tome1");
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        stu.setAddress(address);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        session.save(stu);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        tx.commit();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif    }

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}

[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif



二、CompositeUserType
CompositeUserType拥有UserType所有的接口,并提供了更加丰富的功能,可以实现更加复杂的功能。
待续...

......显示全文...
    点击查看全文


上一篇文章      下一篇文章      查看所有文章
2016-03-26 16:27:14  
数据库 最新文章
Python&MySQL&PyQt
最新用python来操作mysql完全解析
mongodb的安装详解
1.PDO简介
《MySQL必知必会学习笔记》:高级联结
【翻译自mos文章】怎么对Microsoft(Office)
MyCAT全局表描述及示例
ocp
关于SQL数据表存储过程表名前缀换成dbo代码
数据库调优教程(二)慢查询数据准备
360图书馆 软件开发资料 文字转语音 购物精选 软件下载 新闻资讯 小游戏 Chinese Culture 股票 三丰软件 开发 中国文化 网文精选 阅读网 看图 日历 万年历 2018年11日历
2018-11-16 19:36:46
多播视频美女直播
↓电视,电影,美女直播,迅雷资源↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  软件世界网 --