Hibernate允许我们自定义映射属性的类型,比如一个学生有联系地址,而联系地址又分为家庭地址和工作地址,我们可以把两个地址信息抽象成一个新的Address类,作为Student的成员变量
数据库结构:
create
table
typestu (id
varchar
(
32
)
primary
key
,name
varchar
(
32
),homeaddr
varchar
(
32
),workaddr
varchar
(
32
));
Hibernate.cfg.xml
<?
xml version='1.0' encoding='UTF-8'
?>
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" > root </ property >
< property name ="connection.url" >
jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312 & useUnicode=true
</ property >
< property name ="dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="myeclipse.connection.profile" > mysql </ property >
< property name ="connection.password" > 1234 </ property >
< property name ="connection.driver_class" >
com.mysql.jdbc.Driver
</ property >
< property name ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="hibernate.show_sql" > true </ property >
< property name ="current_session_context_class" > thread </ property >
< mapping resource ="Search/UserType/Student.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
<! DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!-- Generated by MyEclipse Hibernate Tools. -->
< hibernate-configuration >
< session-factory >
< property name ="connection.username" > root </ property >
< property name ="connection.url" >
jdbc:mysql://localhost:3306/schoolproject?characterEncoding=gb2312 & useUnicode=true
</ property >
< property name ="dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="myeclipse.connection.profile" > mysql </ property >
< property name ="connection.password" > 1234 </ property >
< property name ="connection.driver_class" >
com.mysql.jdbc.Driver
</ property >
< property name ="hibernate.dialect" >
org.hibernate.dialect.MySQLDialect
</ property >
< property name ="hibernate.show_sql" > true </ property >
< property name ="current_session_context_class" > thread </ property >
< mapping resource ="Search/UserType/Student.hbm.xml" />
</ session-factory >
</ hibernate-configuration >
Pojo
package
Search.UserType;
public class Student {
private String id; // 标识id
private String name; // 学生姓名
private AddressType address; // 地址
public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public AddressType getAddress() {
return address;
}
public void setAddress(AddressType address) {
this .address = address;
}
}
public class Student {
private String id; // 标识id
private String name; // 学生姓名
private AddressType address; // 地址
public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public AddressType getAddress() {
return address;
}
public void setAddress(AddressType address) {
this .address = address;
}
}
自定义类型
package Search.UserType;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
public class AddressType implements UserType, Serializable {
private String homeAddr;
private String workAddr;
/* 有几个字段就有几个值,这里容易出错,要多注意 */
private static final int [] SQL_TYPES = { Types.VARCHAR, Types.VARCHAR } ;
/* 这个方法告诉Hibernate在成生DDL时对列采用什么样的SQL语法 */
public int [] sqlTypes() {
return SQL_TYPES;
}
/*
* Hibernate返回什么样的映射类型,与 <property name="address" type="model.AddressType">
* 指定的类一致。事实上也可以把AddressType拆分为两个类,一个类是只携带信息的JavaBean,它里面
* 没有逻辑操作也没有实现UserType(比如AddressBean);而另一个类实现了UserType,它所面对的就不是现在这个
* AddressType类的homeAddr和homeAddr属性,它面对的是AddressBean。在本例中为了简洁方便,只用了一个类。
*/
public Class returnedClass() {
return AddressType. class ;
}
/*
* 表明这个类的实例在创建以后就不可以改变属性。Hibernate能为不可改变的类作一些性能优化。
*/
public boolean isMutable() {
return false ;
}
/*
* 由于AddressType是不可变的,所以深拷贝可以直接返回对象引用。拷贝的对象由应用程序使用, 而原版对象由Hibernate维护以做脏数据检查
*/
public Object deepCopy(Object value) {
return value; // Address is immutable
}
/* 两个对象是否相等,使用了apache的common工具包来进行属性比对 */
public boolean equals(Object x, Object y) {
if (x == y)
return true ;
if (x == null || y == null )
return false ;
AddressType add1 = (AddressType) x;
AddressType add2 = (AddressType) y;
return new EqualsBuilder() // 使用EqualsBuilder类来方便地进行比对
.append(add1.getHomeAddr(), add2.getHomeAddr()).append(
add2.getWorkAddr(), add2.getWorkAddr()).isEquals();
}
/* 得到hash码 */
public int hashCode(Object x) throws HibernateException {
AddressType address = (AddressType) x;
return new HashCodeBuilder() // 使用HashCodeBuilder类来方便地进行比对
.append(address.getHomeAddr()).append(address.getWorkAddr())
.toHashCode();
}
/* 读取数据并组装成一个AddressType对象。names[]中的参数顺序依照映射文件中定义的顺序 */
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
if (rs.wasNull())
return null ;
String homeAddr = rs.getString(names[ 0 ]);
String schoolAddr = rs.getString(names[ 1 ]);
AddressType address =