Reputation: 951
I am new working on Hibernate, I need some customized query so I am using SQLQuery the following is the code I am using.
List getResult(String queryString, List<String> bindList, String className) {
SQLQuery sqlQuery = session.createSQLQuery(queryString);
int i=0;
for (String value : bindList) {
query.setString(i, value);
i++;
}
if(className != null)
{
sqlQuery.setResultTransformer(Transformers.aliasToBean(Class.forName(className)));
}
}
The problem is here is that one of the column values are returned as clob type but the type of corresponding DTO's value matching with clob is String so I have to change that clob value into String. is there any good solution for it?
As far as I know that the sqlQuery has list() method which returns object collections including query result rows. So is there any way I can manipulate that content of that list?
The way I tried is the following. I have three column values returned by executing query, boolean, string, and string (which is converted from clob)
List<Object[]> rows = sqlQuery.list();
row = rows.get(0);
java.sql.Clob cValue = (java.sql.Clob)row[2];
String str_value = cValue.getSubString(1, (int)cValue.length());
List<Object[]> rows = new ArrayList<Object[]>();
Object[] obj = new Object[3];
obj[0] = new Boolean(row[0]);
obj[1] = new String(row[1]);
obj[2] = new String(str_value);
rows.add(obj);
return rows;
But the log says java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to SourceDTO. (kind of expected) What I am thinking is that I think I can manipulate resultant list and returns it with matching SourceDTO. Could anyone have some solution about this?
Upvotes: 0
Views: 352
Reputation: 880
get the input stream of your CLOB
object using getAsciiStream()
and pass it to the copy()
method.
InputStream in = clobObject.getAsciiStream();
StringWriter w = new StringWriter();
IOUtils.copy(in, w);
String clobAsString = w.toString();
Upvotes: 0