Hibernate對自定義類型CompositeUserType的用法
系統
2292 0
使用CompositeUserType和使用UserType一樣,但CompositeUserType多了HQL的查詢支持,并相對比較復雜,一般使用UserType就足夠了
使用UserType參考
http://blog.csdn.net/daryl715/archive/2007/12/10/1927502.aspx
?
下面給出UserType不同的AddressType和測試代碼:
?
package
?Search.CompositeUserType;
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.Hibernate;
import
?org.hibernate.HibernateException;
import
?org.hibernate.engine.SessionImplementor;
import
?org.hibernate.type.Type;
import
?org.hibernate.usertype.CompositeUserType;
import
?org.hibernate.usertype.UserType;
public
?
class
?AddressType?
implements
?CompositeUserType,?Serializable?
...
{
????
private
?String?homeAddr;
????
private
?String?workAddr;
?
????
/**/
/*
獲得屬性名數組,屬性名可用于進行HQL查詢??
*/
????
public
?String[]?getPropertyNames()?
...
{
????????
return
?
new
?String[]?
...
{?
"
homeAddr
"
,?
"
schoolAddr
"
?}
;
????}
????
/**/
/*
獲得對應的屬性類型,必須和屬性名數組元素的順序相對應??
*/
????
public
?Type[]?getPropertyTypes()?
...
{
????????
return
?
new
?Type[]?
...
{?Hibernate.STRING,?Hibernate.STRING?}
;
????}
????
/**/
/*
通過index取得對象的屬性值,index從0開始,順序和配置文件中定義屬性的順序一致???
*/
????
public
?Object?getPropertyValue(Object?component,?
int
?property)
????????????
throws
?HibernateException?
...
{
????????AddressType?address?
=
?(AddressType)?component;
????????
if
?(property?
==
?
0
)
????????????
return
?address.getHomeAddr();
????????
else
????????????
return
?address.getWorkAddr();
????}
????
/**/
/*
根據index設置對象屬性??
*/
????
public
?
void
?setPropertyValue(Object?component,?
int
?property,?Object?value)
????????????
throws
?HibernateException?
...
{
????????AddressType?address?
=
?(AddressType)?component;
????????String?add_value?
=
?(String)value;
????????
if
?(property?
==
?
0
)
????????????address.setHomeAddr(add_value);
????????
else
????????????address.setWorkAddr(add_value);
????????
????}
????
/**/
/*
返回對應的映射類
*/
????
public
?Class?returnedClass()?
...
{
????????
return
?AddressType.
class
;
????}
????
/**/
/*
?兩個對象是否相等,使用了apache的common工具包來進行屬性比對?
*/
????
public
?
boolean
?equals(Object?x,?Object?y)?
throws
?HibernateException?
...
{
????????
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,
????????????SessionImplementor?session,?Object?owner)
????????????
throws
?HibernateException,?SQLException?
...
{
????????
if
?(rs.wasNull())
????????????
return
?
null
;
????????String?homeAddr?
=
?rs.getString(names[
0
]);
????????String?schoolAddr?
=
?rs.getString(names[
1
]);
????????AddressType?address?
=
?
new
?AddressType(homeAddr,?schoolAddr);
????????
return
?address;
????}
????
/**/
/*
?保存數據,index的順序按照映射文件定義的順序,從0開始。?
*/
?
????
public
?
void
?nullSafeSet(PreparedStatement?st,?Object?value,?
int
?index,
????????????SessionImplementor?session)?
throws
?HibernateException,?SQLException?
...
{
????????AddressType?address?
=
?(AddressType)?value;
????????
if
?(value?
==
?
null
)?
...
{
??????????????st.setNull(index,?Types.VARCHAR);
??????????????st.setNull(index
+
1
,?Types.VARCHAR);
????????}
?
else
?
...
{
????????????st.setString(index,?address.getHomeAddr());
????????????st.setString(index?
+
?
1
,?address.getWorkAddr());
????????}
?????????System.out.println(
"
Data?has?been?saved!?
"
);
????}
????
/**/
/*
完整拷貝一個對象,而不是直接返回它的引用?????
*/
????
public
?Object?deepCopy(Object?value)?
throws
?HibernateException?
...
{
????????
if
?(value?
==
?
null
)
????????????
return
?
null
;
????????AddressType?address?
=
?(AddressType)?value;
????????
return
?
new
?AddressType(address.getHomeAddr(),?address.getWorkAddr());
????}
????
/**/
/*
設置類可變,可以通過屬性的set方法改變屬性值
*/
????
public
?
boolean
?isMutable()?
...
{
?????????
return
?
true
;
????}
????
/**/
/*
?當把AddressType類型數據寫入二級緩存時,此方法被調用?
*/
????
public
?Serializable?disassemble(Object?value,?SessionImplementor?session)
????????????
throws
?HibernateException?
...
{
????????
return
?(Serializable)?deepCopy(value);
????}
Hibernate對自定義類型CompositeUserType的用法
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元