日期:2014-05-20  浏览次数:20786 次

hibernate中clob类型值
hibernate怎么取不到clob类型的字段值,取出来为空(数据库中是存在的)
知道的说下,很急

------解决方案--------------------
存和取否要用这个方法:Hibernate.createClob();
他是创立Clob对象的;
------解决方案--------------------
Oracle中插入和读取CLOB型数据时需要用到IO流,MSSQL不用。
至于怎么用,自己搜下吧,网上很多例子的。
------解决方案--------------------
下面是我们结合spring以后hibernate读取clob字段的两个方法,在正式项目中使用过,希望能对你有所帮助


public Object readClob(final Class cls, final String fieldname, final Serializable id) throws DAOException {
HibernateCallback callback = new HibernateCallback() {

public Object doInHibernate(Session session) throws HibernateException, SQLException {

session.connection().setAutoCommit(false);

Object o = session.load(cls, id, LockMode.READ);

StringBuffer result = new StringBuffer();
try {

String prop = Character.toUpperCase(fieldname.charAt(0)) + fieldname.substring(1);

Method method = cls.getMethod("get" + prop, new Class[0]);
Clob clob = (Clob) method.invoke(o, new Object[0]);
if (clob == null) {
return "";
}
BufferedReader in = new BufferedReader(clob.getCharacterStream());
String c;

while ((c = in.readLine()) != null) {
result.append(c);
}
in.close();

} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
throw new DAOException("读取数据时发生错误", e);
}
return result.toString();
}

};

Object obj = getHibernateTemplate().execute(callback);
return obj == null ? "" : obj;
}

//读CLOB字段
public Clob readClob2(Class cls, String fieldname, Serializable id) throws DAOException {

Object o = getHibernateTemplate().load(cls, id);

try {

Method method = cls.getMethod("get" + fieldname, new Class[0]);
return (Clob) method.invoke(o, new Object[0]);
} catch (Exception e) {
log.error(e.getMessage());
throw new DAOException("读取数据时发生错误", e);
}

}
------解决方案--------------------
http://www.javaeye.com/topic/5029

------解决方案--------------------
mapping映射改成text

如果你用了spring
可以
 type="org.springframework.orm.hibernate3.support.ClobStringType"
在实体里那字段上面加
@Type(type = " org.springframework.orm.hibernate3.support.ClobStringType " )
------解决方案--------------------
Java code

@Lob
byte[] content

------解决方案--------------------

clob转换成 byte[]