读书人

hibernate UserType施用心得

发布时间: 2012-09-24 13:49:41 作者: rapoo

hibernate UserType使用心得
UserType中其他方法不用多说,主要说明下面两个方法

/**
* 负责将数据库中的数据解析为PO中对应属性的值
* PreparedStatement st:将存储到数据库中的数据集
* Object value:该属性的值。
* int index:不太清除具体作用。目前是直接使用
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException
{
如果是把Object转为String存储,则使用Hibernate提供的方法
String str = assemble((该Field的类型) value);
Hibernate.STRING.nullSafeSet(st, str, index);

但如果想把Object转为Blob存储
// 自己负责将Object转为byte[],这里用String举例
byte[] bytes = string.getBytes();

st.setBytes(index, bytes);
}


/**
* 负责将数据库中的数据解析为PO中对应属性的值
* ResultSet rs:数据库中得到的数据结果集
* String names[]:列名。一般只用到names[0]
* Object owner:未使用到。待更新
*/
public Object nullSafeGet(ResultSet rs, String names[], Object owner)
throws HibernateException, SQLException
{
如果是把Object转为String存储,则使用Hibernate提供的方法
String value = (String) Hibernate.STRING.nullSafeGet(rs, names[0]);

如果把Object转为Blob存储,则使用
SessionImplementor sim = (SessionImplementor)session;
Hibernate.BLOB.nullSafeGet(rs, names, sim, owner);


不知道SessionImplementor 如何赋值时的解决办法:
根据属性名称直接获取对应的Blob值
String value = names[0];
Blob blobs = rs.getBlob(value);

}

读书人网 >其他数据库

热点推荐